Skip to content

Commit

Permalink
Add framework for extending test methods/classes with attributes (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher authored Oct 25, 2024
1 parent 1cd7774 commit 074f143
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/mantle/testing/class-test-case.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions src/mantle/testing/concerns/trait-interacts-with-attributes.php
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;
}
}
14 changes: 7 additions & 7 deletions src/mantle/testing/concerns/trait-wordpress-authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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 ),
);
}

/**
Expand Down

0 comments on commit 074f143

Please sign in to comment.