-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add framework for extending test methods/classes with attributes (#595)
- Loading branch information
Showing
3 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/mantle/testing/concerns/trait-interacts-with-attributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Interacts_With_Attributes trait file | ||
* | ||
* @package Mantle | ||
*/ | ||
|
||
namespace Mantle\Testing\Concerns; | ||
|
||
/** | ||
* Trait to allow simple callbacks when a registered attribute is added to a | ||
* test class/method. | ||
*/ | ||
trait Interacts_With_Attributes { | ||
use Reads_Annotations; | ||
|
||
/** | ||
* Attribute callbacks to interact with. | ||
* | ||
* @var array<class-string, callable(\ReflectionAttribute $attribute): mixed> | ||
*/ | ||
private array $attribute_callbacks = []; | ||
|
||
/** | ||
* Interact with attributes set up. | ||
*/ | ||
protected function interacts_with_attributes_set_up(): void { | ||
foreach ( $this->attribute_callbacks as $attribute_class => $callback ) { | ||
foreach ( $this->get_attributes_for_method( $attribute_class ) as $attribute ) { | ||
$callback( $attribute ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Interact with attributes tear down. | ||
*/ | ||
protected function interacts_with_attributes_tear_down(): void { | ||
$this->attribute_callbacks = []; | ||
} | ||
|
||
/** | ||
* Register an attribute to interact with. | ||
* | ||
* @param class-string $attribute Attribute class to interact. | ||
* @param callable $callback Callback to interact with the attribute. | ||
* @phpstan-param callable(\ReflectionAttribute $attribute): mixed $callback Callback to interact with the attribute | ||
*/ | ||
protected function register_attribute( string $attribute, callable $callback ): void { | ||
$this->attribute_callbacks[ $attribute ] = $callback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters