diff --git a/src/mantle/testing/class-test-case.php b/src/mantle/testing/class-test-case.php index 26825b17..abacf69a 100644 --- a/src/mantle/testing/class-test-case.php +++ b/src/mantle/testing/class-test-case.php @@ -20,6 +20,7 @@ use Mantle\Testing\Concerns\Deprecations; use Mantle\Testing\Concerns\Hooks; use Mantle\Testing\Concerns\Incorrect_Usage; +use Mantle\Testing\Concerns\Interacts_With_Attributes; use Mantle\Testing\Concerns\Interacts_With_Console; use Mantle\Testing\Concerns\Interacts_With_Container; use Mantle\Testing\Concerns\Interacts_With_Cron; @@ -53,6 +54,7 @@ abstract class Test_Case extends BaseTestCase { use Deprecations; use Hooks; use Incorrect_Usage; + use Interacts_With_Attributes; use Interacts_With_Console; use Interacts_With_Container; use Interacts_With_Cron; diff --git a/src/mantle/testing/concerns/trait-interacts-with-attributes.php b/src/mantle/testing/concerns/trait-interacts-with-attributes.php new file mode 100644 index 00000000..54aab7c7 --- /dev/null +++ b/src/mantle/testing/concerns/trait-interacts-with-attributes.php @@ -0,0 +1,52 @@ + + */ + 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; + } +} diff --git a/src/mantle/testing/concerns/trait-wordpress-authentication.php b/src/mantle/testing/concerns/trait-wordpress-authentication.php index 4fedadf9..5d00eef2 100644 --- a/src/mantle/testing/concerns/trait-wordpress-authentication.php +++ b/src/mantle/testing/concerns/trait-wordpress-authentication.php @@ -14,6 +14,7 @@ use Mantle\Testing\Attributes\Acting_As; use Mantle\Testing\Exceptions\Exception; use PHPUnit\Framework\Assert; +use ReflectionAttribute; use WP_User; use function Mantle\Support\Helpers\get_user_object; @@ -24,14 +25,12 @@ * @mixin \PHPUnit\Framework\TestCase */ trait WordPress_Authentication { - use Reads_Annotations; + use Interacts_With_Attributes; /** * Backed up global user ID. - * - * @var int */ - protected $backup_user; + protected int $backup_user; /** * Backup the current global user. @@ -40,9 +39,10 @@ public function wordpress_authentication_set_up(): void { $this->backup_user = get_current_user_id(); // Set the test case up using the user from the attribute in descending order (class -> method). - foreach ( $this->get_attributes_for_method( Acting_As::class ) as $attribute ) { - $this->acting_as( $attribute->newInstance()->user ); - } + $this->register_attribute( + Acting_As::class, + fn ( ReflectionAttribute $attribute ) => $this->acting_as( $attribute->newInstance()->user ), + ); } /**