Named mocks¶
NamedFetchMockPlugin resolves Mockery::fetchMock() and Mockery\Container::fetchMock() calls to (T&Mockery\MockInterface)|null, including mocks registered under an alias with namedMock().
Direct class and spec lookups¶
When the argument to fetchMock() is a known class name, interface name, trait name, class-string<T>, or Mockery spec string, MockTypeFactory::fromFetchMock() resolves it directly:
$mock = Mockery::fetchMock(Greeter::class);
if ($mock !== null) {
$mock->greet('Ada');
}
Both Mockery::fetchMock(string $name) and Mockery\Container::fetchMock(string $reference) have callable signature overrides requiring a string argument.
Named mock alias resolution¶
When you register a mock under an arbitrary alias name with Mockery::namedMock() or namedMock(), fetchMock() looks up the alias by exact call span in NamedMockIndex:
Mockery::namedMock('GreeterAlias', Greeter::class);
$fetched = Mockery::fetchMock('GreeterAlias');
if ($fetched !== null) {
$fetched->greet('Ada');
}
NamedFetchMockPlugin uses the plugin identifier mockery-named-fetch and runs separately from MockeryPlugin. This split keeps provider memoization active on MockeryPlugin while fetchMock() resolves by call span.
Scope and control-flow rules¶
NamedMockFileParser and NamedMockIndex resolve aliases according to scope and control flow:
- Enclosing callable scopes. Registrations inside a function, method, closure, or arrow function stay scoped to that callable and do not leak to unrelated methods in the same file.
- Branch dominance. If a branch registers a second target for an alias,
fetchMock()returns both targets plusnull, such as(Greeter&MockInterface)|(ApiClient&MockInterface)|null. An unconditional registration beforefetchMock()overrides earlier lifecycle registrations. - Trait lifecycle hooks. If no local registration dominates
fetchMock(),NamedMockIndexinspects traits that the test class uses. It checkssetUp(),#[Before]methods,startMockery(), andmockeryTestSetUp(). It follows transitive trait uses and respects method overrides andinsteadofexclusions. - Safe fallback on ambiguity. If two definitions conflict or an alias does not resolve,
fetchMock()falls back to Mockery's default return type.
Lazy indexing behavior¶
NamedMockIndex does not scan files at startup. It builds its index on demand when analysis encounters an unresolved literal fetchMock() call.
Candidate discovery scales with project size:
- Up to 2000 user classes.
NamedMockIndexqueriesCodebasemetadata for non-vendor user classes and functions, and includes sibling PHP files in those directories. - More than 2000 user classes.
NamedMockIndexlocates the project root and streams PHP files from disk. It skipsSKIPPED_DIRECTORIES:vendor,.git,node_modules,var,cache,storage,build, anddist.
In both paths, NamedMockIndex reads one candidate file at a time and parses only files that contain fetchMock. It then loads the trait files referenced by classes that call fetchMock().
Disabling named fetch resolution¶
If you compose plugins programmatically and your codebase never uses namedMock() with fetchMock(), pass namedFetch: false to omit NamedFetchMockPlugin:
MockeryExtension::plugins(namedFetch: false);
See Composition for custom worker setup.