PHPUnit Application Architecture Test

Overview

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

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 B
  • assertIncomingsNotFrom($A, $B) Layer A must not contains arguments with types from Layer B
  • assertOutgoingFrom($A, $B) Layer A must contains methods return types from Layer B
  • assertOutgoingNotFrom($A, $B) Layer A must not contains methods return types from Layer B
  • assertMethodSizeLessThan($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
You might also like...
PHPUnit to Pest Converter

PestConverter PestConverter is a PHP library for converting PHPUnit tests to Pest tests. Before use Before using this converter, make sure your files

Allows the running of PHPUnit within ExpressionEngine

EE Unit Tests EE Unit Tests is an Add-on for ExpressionEngine that allows developers to execute unit tests from the Command Line. EE Unit Tests uses P

PHP libraries that makes Selenium WebDriver + PHPUnit functional testing easy and robust
PHP libraries that makes Selenium WebDriver + PHPUnit functional testing easy and robust

Steward: easy and robust testing with Selenium WebDriver + PHPUnit Steward is a set of libraries made to simplify writing and running robust functiona

PHPUnit extension for database interaction testing.

This extension is no longer maintained DbUnit PHPUnit extension for database interaction testing. Installation Composer If you use Composer to manage

Mock implementation of the Translation package, for testing with PHPUnit

PoP Translation - Mock Mock implementation of the Translation package, for testing with PHPUnit Install Via Composer composer require getpop/translati

Magento PHPUnit Integration

Magento PHPUnit Integration Magento is a quite complex platform without built in unit test suite, so the code is not oriented on running tests over it

The objective of ParaTest is to support parallel testing in PHPUnit

The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in your project and start using it with no additional bootstrap or configurations!

Report high memory usage PHPUnit tests: Managed by opg-org-infra & Terraform

phpunit-memory-usage Report high memory usage PHPUnit tests: Managed by opg-org-infra & Terraform Configuration Add into the phpunit.xml extensions se

PHPStan PHPUnit extensions and rules

PHPStan PHPUnit extensions and rules PHPStan PHPUnit This extension provides following features: createMock(), getMockForAbstractClass() and getMockFr

Comments
  • Fixes PHP 8.1 deprecation

    Fixes PHP 8.1 deprecation

    This pull request fixes PHP 8.1 deprecation around the getIterator method:

    Fatal error: During inheritance of IteratorAggregate: Uncaught Whoops\Exception\ErrorException: Return type of PHPUnit\Architecture\Elements\Layer\Layer::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /Users/nunomaduro/Work/Code/phpunit-architecture-test/src/Elements/Layer/Layer.php:30
    
    opened by nunomaduro 0
  • Non imported `uses` don't get considered as dependencies

    Non imported `uses` don't get considered as dependencies

    It seems that non-imported uses don't get consider as dependencies. Here is an example that fails, but should pass:

    namespace Foo;
    class B {}
    
    namespace Foo;
    class B extends A {}
    
    $a = $this->layer()->leaveByNameStart('Foo\\A');
    $b = $this->layer()->leaveByNameStart('Foo\\B');
    
    $this->assertDependOn($b, $a);
    

    The reason is that there is no "use" statement at the begging of the script.

    opened by nunomaduro 0
Owner
null
Qase-phpunit - Qase TMS PHPUnit reporter.

Qase TMS PHPUnit reporter Publish results simple and easy. How to integrate composer require qase/phpunit-reporter Example of usage The PHPUnit report

Qase TMS 6 Nov 24, 2022
Mockery - Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library

Mockery Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its c

Mockery 10.3k Jan 1, 2023
vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

vfsStream vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with

null 1.4k Dec 23, 2022
A sample RESTful API in Laravel with PHPunit test.

Laravel PHP Framework URL | URI | Action |

Fasil 9 Jul 11, 2020
Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slowness of constantly restarting the testing environment.

Magic Test for Laravel Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slownes

null 400 Jan 5, 2023
The most powerful and flexible mocking framework for PHPUnit / Codeception.

AspectMock AspectMock is not an ordinary PHP mocking framework. With the power of Aspect Oriented programming and the awesome Go-AOP library, AspectMo

Codeception Testing Framework 777 Dec 12, 2022
:computer: Parallel testing for PHPUnit

ParaTest The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in y

null 2k Dec 31, 2022
Rector upgrades rules for PHPUnit

Rector Rules for PHPUnit See available PHPUnit rules Install composer require rector/rector-phpunit Use Sets To add a set to your config, use Rector\P

RectorPHP 34 Dec 27, 2022
Add mocking capabilities to Pest or PHPUnit

This repository contains the Pest Plugin Mock. The Mocking API can be used in regular PHPUnit projects. For that, you just have to run the following c

PEST 16 Dec 3, 2022