Skip to content
PHP Architecture Tester - Easy to use architectural testing tool for PHP ✔️
PHP
Branch: master
Clone or download

README.md

PHP Architecture Tester

Easy to use architecture testing tool for PHP

Version PHP Version Downloads
License Contributions welcome


Introduction 📜

PHP Architecture Tester is a static analysis tool to verify architectural requirements.

It provides a natural language abstraction to define your own architectural rules and test them against your software. You can also integrate phpat easily into your toolchain.

There are four groups of supported assertions: Dependency, Inheritance, Composition and Mixin.

Installation 💽

Just require phpat with Composer:

composer require --dev carlosas/phpat

Configuration 🔧

You might want to setup a basic configuration:

# phpat.yaml
src:
  path: src/
tests:
  path: tests/architecture/

This is the complete list of options:

  • src path: The root path of your application.
  • src include: Files you want to be tested excluding the rest (default=all).
  • src exclude: Files you want to be excluded in the tests (default=none).
  • tests path: The path where your tests are.
  • options verbosity: 0/1/2 output verbosity level (default=1).
  • options dry-run: true/false report failed suite without error exit code (default=false).
  • options dependency ignore_docblocks: true/false ignore dependencies on docblocks (default=false).

Test definition 📓

There are different ways to choose which classes will intervene in a rule (called Selectors) and many different possibles Assertions. Check the complete list of both here:

This could be a test with a couple of rules:

<?php

use PhpAT\Rule\Rule;
use PhpAT\Selector\Selector;
use PhpAT\Test\ArchitectureTest;
use App\Domain\BlackMagicInterface;

class ExampleTest extends ArchitectureTest
{
    public function testDomainDoesNotDependOnOtherLayers(): Rule
    {
        return $this->newRule
            ->classesThat(Selector::haveClassName('App\Domain\*'))
            ->excludingClassesThat(Selector::implementInterface(BlackMagicInterface::class))
            ->canOnlyDependOn()
            ->classesThat(Selector::havePath('Domain/*'))
            ->andClassesThat(Selector::haveClassName('App\Application\Shared\Service\KnownBadApproach'))
            ->build();
    }
    
    public function testAllHandlersExtendAbstractCommandHandler(): Rule
    {
        return $this->newRule
            ->classesThat(Selector::havePath('Application/*/UseCase/*Handler.php'))
            ->excludingClassesThat(Selector::extendClass('App\Application\Shared\UseCase\DifferentHandler'))
            ->andExcludingClassesThat(Selector::includeTrait('App\Legacy\LegacyTrait'))
            ->andExcludingClassesThat(Selector::haveClassName(\App\Application\Shared\UseCase\AbstractCommandHandler::class))
            ->mustExtend()
            ->classesThat(Selector::haveClassName('App\Application\Shared\UseCase\AbstractCommandHandler'))
            ->build();
    }
}

Usage 🚀

Run the bin with your configuration file:

vendor/bin/phpat phpat.yaml

⚠ Launching early stage releases (0.x.x) could break the API according to Semantic Versioning 2.0. We are using minor for breaking changes. This will change with the release of the stable 1.0.0 version.

PHP Architecture Tester is in a very early stage, contributions are welcome. Please take a look to the Contribution docs.

You can’t perform that action at this time.