Skip to content

Commit

Permalink
Small doc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosas committed Jul 5, 2024
1 parent 24f657b commit f4c1a5f
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions docs/documentation/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,48 @@ final class ConfigurationTest
}
```

## Reusing rules
## Dynamic Rule Sets

It is possible to dynamically create rules by returning an iterable of Rules from your method:

```php
namespace App\Tests\Architecture;

use PHPat\Selector\Selector;
use PHPat\Test\Builder\Rule;
use PHPat\Test\PHPat;

final class ConfigurationTest
{
private const DOMAINS = [
'App\Domain1',
'App\Domain2',
];

/**
* @return iterable<Rule>
*/
public function test_domain_independence(): iterable
{
foreach(self::DOMAINS as $domain) {
yield PHPat::rule()
->classes(Selector::inNamespace($domain))
->canOnlyDependOn()
->classes(Selector::inNamespace($domain));
}
}
}
```

## Extending test classes

You might want to reuse rules in different tests or parametrize them for a modular scenario.

**Reusing rules is possible by extending a class or using traits.**

Let's see an example:

You are splitting bounded contexts, and you want the domain of each context to be independent from the others.
You are splitting bounded contexts, and you want the domain of each context to be independent of the others.
Your rule should check that classes in the domain of a context do not depend on classes in the domain of another context.

This is what you can do:
Expand Down Expand Up @@ -86,35 +119,3 @@ final class UserDomainTest extends AbstractDomainTest
```

Note that you would only need to register the `UserDomainTest` class as a PHPat test in the PHPStan config file.

## Dynamic Rule Sets
It is possible to dynamically create rules by returning an iterable of Rules from your method:

```php
namespace App\Tests\Architecture;

use PHPat\Selector\Selector;
use PHPat\Test\Builder\Rule;
use PHPat\Test\PHPat;

final class ConfigurationTest
{
private const DOMAINS = [
'App\Domain1',
'App\Domain2',
];

/**
* @return iterable<string, Rule>
*/
public function test_domain_independence(): iterable
{
foreach(self::DOMAINS as $domain) {
yield $domain => PHPat::rule()
->classes(Selector::inNamespace($domain))
->canOnlyDependOn()
->classes(Selector::inNamespace($domain));
}
}
}
```

0 comments on commit f4c1a5f

Please sign in to comment.