From 2a8559f6409fc03f36241b7b2833cc6f94631a5b Mon Sep 17 00:00:00 2001 From: Carlos Alandete Sastre Date: Fri, 24 Nov 2023 19:09:59 +0100 Subject: [PATCH] Rename rule attribute to TestRule --- docs/CHANGELOG.md | 6 ++++++ docs/documentation/rules.md | 12 +++++++----- src/Test/Attributes/{Test.php => TestRule.php} | 2 +- src/Test/TestParser.php | 4 ++-- tests/architecture/ConfigurationTest.php | 6 +++--- 5 files changed, 19 insertions(+), 11 deletions(-) rename src/Test/Attributes/{Test.php => TestRule.php} (81%) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9add4e77..95b231aa 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog All notable changes to this project will be documented in this file. +## 0.10.11 +* Add `#[TestRule]` attribute to mark test's methods as rules +* Add `shouldApplyAttribute()` assertion +* Deprecate `hasAttribute()` selector in favor of `appliesAttribute()` +* Allow extending test files + ## 0.10.10 * Add `shouldOnlyHaveOnePublicMethod()` assertion. * Fix return type of target excludes builder step. diff --git a/docs/documentation/rules.md b/docs/documentation/rules.md index 0d3709fc..ea3c8db4 100644 --- a/docs/documentation/rules.md +++ b/docs/documentation/rules.md @@ -1,16 +1,18 @@ # Rules -A rule is a statement, consisting in Selector and Assertions, that must be true for the test to pass. Each test class can contain one or more rules. +A rule is a statement, consisting in Selectors and Assertions, that must be true for the test to pass. + +Each test class can contain one or more rules. ![image-layered](../assets/rules.png) -Your rules must be public methods that start with `test_` or apply the `Test` attribute. You can build rules using the `\PHPat\Test\PHPat::rule()` method: +Your rules must be public methods that start with `test_` or apply the `#[TestRule]` attribute. You can build rules using the `\PHPat\Test\PHPat::rule()` method: ```php namespace App\Tests\Architecture; use PHPat\Selector\Selector; -use PHPat\Test\Attributes\Test; +use PHPat\Test\Attributes\TestRule; use PHPat\Test\Builder\Rule; use PHPat\Test\PHPat; @@ -25,7 +27,7 @@ final class ConfigurationTest ; } - #[Test] + #[TestRule] public function entities_are_final(): Rule { return PHPat::rule() @@ -83,4 +85,4 @@ final class UserDomainTest extends AbstractDomainTest } ``` -Note that you only would need to register the `UserDomainTest` class in the PHPStan config file. \ No newline at end of file +Note that you would only need to register the `UserDomainTest` class as a PHPat test in the PHPStan config file. diff --git a/src/Test/Attributes/Test.php b/src/Test/Attributes/TestRule.php similarity index 81% rename from src/Test/Attributes/Test.php rename to src/Test/Attributes/TestRule.php index ff1819da..6edb8ec7 100644 --- a/src/Test/Attributes/Test.php +++ b/src/Test/Attributes/TestRule.php @@ -3,4 +3,4 @@ namespace PHPat\Test\Attributes; #[\Attribute(\Attribute::TARGET_METHOD)] -final class Test {} +final class TestRule {} diff --git a/src/Test/TestParser.php b/src/Test/TestParser.php index 5a64ebd9..3d52034f 100644 --- a/src/Test/TestParser.php +++ b/src/Test/TestParser.php @@ -2,7 +2,7 @@ namespace PHPat\Test; -use PHPat\Test\Attributes\Test; +use PHPat\Test\Attributes\TestRule; use PHPat\Test\Builder\Rule as RuleBuilder; class TestParser @@ -45,7 +45,7 @@ private function parse(): array $object = $reflected->newInstanceWithoutConstructor(); foreach ($reflected->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { if ( - !empty($method->getAttributes(Test::class)) + !empty($method->getAttributes(TestRule::class)) || preg_match('/^(test)[A-Za-z0-9_\x80-\xff]*/', $method->getName()) ) { $ruleBuilder = $object->{$method->getName()}(); diff --git a/tests/architecture/ConfigurationTest.php b/tests/architecture/ConfigurationTest.php index ddb844c8..352d0a0b 100644 --- a/tests/architecture/ConfigurationTest.php +++ b/tests/architecture/ConfigurationTest.php @@ -4,13 +4,13 @@ use PHPat\Configuration; use PHPat\Selector\Selector; -use PHPat\Test\Attributes\Test; +use PHPat\Test\Attributes\TestRule; use PHPat\Test\Builder\Rule; use PHPat\Test\PHPat; final class ConfigurationTest { - #[Test] + #[TestRule] public function configuration_does_not_have_dependencies(): Rule { return PHPat::rule() @@ -20,7 +20,7 @@ public function configuration_does_not_have_dependencies(): Rule ; } - #[Test] + #[TestRule] public function configuration_is_final(): Rule { return PHPat::rule()