PHPUnit Application Architecture Test
Idea: write architecture tests as well as feature and unit tests
Installation
Install via composer
composer require --dev ta-tikoma/phpunit-architecture-test
Add trait to Test class
abstract class TestCase extends BaseTestCase
{
use ArchitectureAsserts;
}
Use
- Create test
- Make layers of application
- Add asserts
public function test_make_layer_from_namespace()
{
$app = $this->layerFromNameStart('PHPUnit\\Architecture');
$tests = $this->layerFromNameStart('tests');
$this->assertDoesNotDependOn($app, $tests);
$this->assertDependOn($tests, $app);
}
Run
./vendor/bin/phpunit
Test files structure
- tests
- Architecture
- SomeTest.php
- Feature
- Unit
- Architecture
How to build Layer
$this->layerFromNameStart($nameStart)
All object which names start from$nameStart
fall in layer.$this->layerFromPath($path)
All object which path start from$path
fall in layer.$this->layer()-> ... ->build()
Custom layer. You can use:includePath
includeNameStart
includeNameRegex
includeObject
includeObjectType
excludePath
excludeNameStart
excludeNameRegex
excludeObjectType
$this->layersFromNameRegex($regex)
Builders multiple layers; regex must return group with name 'layer', it is layer identifier for checked object.$this->layersFromClosure($closure)
Builders multiple layers; Closure take ObjectDescription in param and must to return string (unique module id) or null.
Asserts
Dependencies
Example: Controllers don't use Repositories only via Services
assertDependOn($A, $B)
Layer A must contains dependencies by layer B.assertDoesNotDependOn($A, $B)
Layer A (or layers in array A) must not contains dependencies by layer B (or layers in array B).
Methods
assertIncomingsFrom($A, $B)
Layer A must contains arguments with types from Layer BassertIncomingsNotFrom($A, $B)
Layer A must not contains arguments with types from Layer BassertOutgoingFrom($A, $B)
Layer A must contains methods return types from Layer BassertOutgoingNotFrom($A, $B)
Layer A must not contains methods return types from Layer BassertMethodSizeLessThan($A, $SIZE)
Layer A must not contains methods with size less than SIZE
Properties
assertHasNotPublicProperties($A)
Objects in Layer A must not contains public properties
Alternatives
Advantages
- Dynamic creation of layers by regular expression (not need declare each module)
- Run along with the rest of tests from phpunit
- Asserts to method arguments and return types (for check dependent injection)
- Asserts to properties visibility