Skip to content

Commit

Permalink
Rename rule attribute to TestRule
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosas committed Nov 24, 2023
2 parents 93a73e6 + 2a8559f commit 03a1c70
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
12 changes: 7 additions & 5 deletions docs/documentation/rules.md
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -25,7 +27,7 @@ final class ConfigurationTest
;
}

#[Test]
#[TestRule]
public function entities_are_final(): Rule
{
return PHPat::rule()
Expand Down Expand Up @@ -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.
Note that you would only need to register the `UserDomainTest` class as a PHPat test in the PHPStan config file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
namespace PHPat\Test\Attributes;

#[\Attribute(\Attribute::TARGET_METHOD)]
final class Test {}
final class TestRule {}
4 changes: 2 additions & 2 deletions src/Test/TestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()}();
Expand Down
6 changes: 3 additions & 3 deletions tests/architecture/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -20,7 +20,7 @@ public function configuration_does_not_have_dependencies(): Rule
;
}

#[Test]
#[TestRule]
public function configuration_is_final(): Rule
{
return PHPat::rule()
Expand Down

0 comments on commit 03a1c70

Please sign in to comment.