Skip to content

Guard module boundaries

Write module rules in Pest’s arch() tests, for example in tests/Arch/ModulesTest.php. Define your modules first.

To list the modules a module may use, use toOnlyDependOn():

use function Nwidart\Larch\module;
arch('billing uses only shared and catalog')
->expect(module('Billing'))
->toOnlyDependOn(module('Shared'), module('Catalog'));

To allow only the public API of a module, pass module('Name')->publicApi():

arch('billing uses only shared, and the public API of catalog')
->expect(module('Billing'))
->toOnlyDependOn(module('Shared'), module('Catalog')->publicApi());

To forbid a module to use any other module, call toOnlyDependOn() with no target:

arch('shared uses no other module')
->expect(module('Shared'))
->toOnlyDependOn();

To make every module use the others only through their public API, use toOnlyUsePublicApis() on modules():

use function Nwidart\Larch\modules;
arch('modules use each other only through their public API')
->expect(modules())
->toOnlyUsePublicApis();

To check one module only, give module('Name') as the subject.

To fail when modules use each other, directly or through other modules, use toHaveNoCycles():

arch('no module is in a cycle')
->expect(modules())
->toHaveNoCycles();

These rules check the classes the code names. To check facades, container keys, events and jobs too, add via(). See Check links the app makes.