Skip to content

Check runtime links with Larch

Mortise reads the imports of the PHP files, without PHP. It does not see a link that exists only when the app runs: a facade, a key of the container, a listener or a job. For those links, a team can write a test with nwidart/larch in Pest. Larch boots the app and reads the links it makes.

  1. Keep the rules in mortise.json, as in Add boundary rules to an app. Larch’s preset reads the same file, so the team writes each rule once.

  2. Install Larch in the app, as its documentation says.

  3. Add the preset to a Pest test:

    arch()->preset()->mortise();

    The preset reads mortise.json. It runs No circular use and Other modules use only a module’s public surface on the imports, as Mortise does.

  4. For the rule Allowed use, use Larch 0.2.0 or later. The preset then reads rules.allowed-use and checks each module that has a list. With an older Larch, write the rule in a test yourself. This test lets Audit use no module, through every kind of link:

    use Nwidart\Larch\Edge;
    use function Nwidart\Larch\module;
    arch('Audit uses no other module')
    ->expect(module('Audit'))
    ->toOnlyDependOn()
    ->via(Edge::all());

    via(Edge::all()) counts the links that only the booted app shows, not only the imports that Mortise reads. To let a module use other modules, name them in toOnlyDependOn(), for example ->toOnlyDependOn(module('Audit')).

  5. Run the tests in CI with the rest of the suite:

    Terminal window
    vendor/bin/pest

Use both. mortise check is fast and needs no PHP. The Larch test boots the app, so it also sees the links that the imports do not show. A link that Mortise finds is also a link that Larch finds, so a problem can show in both reports.

Larch 0.2.0 counts a module as on when modules_statuses.json has no entry for it. Mortise and laravel-modules count it as off. Give each module an entry in modules_statuses.json, so both tools check the same modules.