/phpat

PHP Architecture Tester - Easy to use architectural testing tool for PHP :heavy_check_mark:

Primary LanguagePHPMIT LicenseMIT

PHP Architecture Tester

Easy to use architecture testing tool for PHP

Packagist Minimum PHP MIT license Contributions welcome Hits


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.yml
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 (called RuleTypes). 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.yml

âš  Launching early stage releases (0.x.x) with a different SemVer strategy. We are using minor for breaking changes. This will change to strict SemVer with the release of 1.0.0. See Semantic Versioning.

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