Skip to content

Module rules

Module rules check links between two modules. Links to vendor classes, and to code outside every module, are not checked.

Expectation Subject Takes via() Reads the booted app
toOnlyDependOn() module() Yes With via()
toOnlyUsePublicApis() module(), modules() Yes With via()
toHaveNoCycles() module(), modules() Yes With via()
toIgnoreEventsOf() module() No Always
expect(module('Billing'))->toOnlyDependOn(Module|PublicApi ...$targets)

Fails when the module uses a module that is not a target.

  • A target module('Name') allows the whole module.
  • A target module('Name')->publicApi() allows only the public API of that module.
  • No target allows no other module.

Headline: Module Billing may only depend on Shared, Catalog (public API). Each link: Billing may not depend on Inventory., or, for a class outside an allowed public API, It is internal to Catalog. The public API of a module is: Contracts, Events.

expect(modules())->toOnlyUsePublicApis()

Fails when the module uses a class of another module outside the public API of that module.

Headline: Modules use other modules only through their public API., or Module Billing uses other modules only through their public API. Each link: It is internal to Catalog. The public API of a module is: Contracts, Events.

expect(modules())->toHaveNoCycles()

Fails when modules use each other, directly or through other modules. With module('Name'), fails only on the cycles that hold that module.

The message lists each cycle, then the links of each step. See Failure messages.

expect(module('Inventory'))->toIgnoreEventsOf(Module ...$modules)

Fails when a listener of the module listens to an event of one of the modules. Takes at least one module. Reads the listeners the booted app registers.

Headline: Module Inventory ignores the events of Billing. Each link: Inventory may not listen to events of Billing.

->via(Edge ...$kinds)

The kinds of link the rule checks. Comes right after toOnlyDependOn(), toOnlyUsePublicApis() or toHaveNoCycles(), before and(). Takes at least one kind. Without via(), a rule checks Edge::imports().

The kinds are listed in Link kinds.

Rules that forbid something have positive names, such as toHaveNoCycles(). Pest’s not-> replaces the failure message, and the list of links would be lost.