Mocks and spies¶
MockeryPlugin types Mockery factory calls, fluent mock builders, trait mocks, and PHPUnit lifecycle hooks.
Factory methods and global helpers¶
The extension types these static methods, instance methods, and global helper functions as an intersection of every mocked target and Mockery\MockInterface:
| Call | Return type |
|---|---|
Mockery::mock(Foo::class) |
Foo&Mockery\MockInterface |
Mockery::spy(Foo::class) |
Foo&Mockery\MockInterface |
Mockery::namedMock('Alias', Foo::class) |
Foo&Mockery\MockInterface |
Mockery::instanceMock(Foo::class) |
Foo&Mockery\MockInterface |
Mockery\Container::mock(Foo::class) |
Foo&Mockery\MockInterface |
mock(Foo::class), spy(Foo::class), namedMock('Alias', Foo::class) |
Foo&Mockery\MockInterface |
The extension overrides callable signatures for all factory methods and helpers. namedMock() requires a string $name first argument and variadic string|object|array<int|string, mixed> ...$args. The other factories accept variadic string|object|array<int|string, mixed> ...$args. If you pass an unnarrowed mixed value to a factory, Mago reports mixed-argument.
Supported mock targets¶
MockTypeFactory inspects target arguments starting at index 1 for namedMock() and index 0 for other factories. It keeps only targets that exist in the codebase as a class, interface, or trait.
- Class, interface, and trait literals.
Mockery::mock(Greeter::class)andMockery::mock(LoggableTrait::class)produceGreeter&Mockery\MockInterfaceandLoggableTrait&Mockery\MockInterface. - Generic
class-string<T>. Passingclass-string<Box<int>>preserves the generic parameters onBox<int>&Mockery\MockInterface. Passing a generic templateclass-string<T>returnsT&Mockery\MockInterface. - Object partial mocks. Passing an object such as
Mockery::mock(new GreeterImpl())returnsGreeterImpl&Mockery\MockInterfaceand keeps generic type arguments and existing intersections on that object. - Multiple targets. Passing several targets such as
Mockery::mock(Greeter::class, Stringable::class)returnsGreeter&Stringable&Mockery\MockInterface. - Non-target arguments.
MockTypeFactoryignores constructor argument arrays, quick definition maps,Closurecallbacks,Mockery\Generator\MockConfigurationBuilder,Mockery\MockInterface, andMockery\LegacyMockInterface.
Mockery string specs¶
MockTypeFactory parses Mockery 1.6 spec strings into their target types:
| Spec form | Example | Inferred type |
|---|---|---|
alias: prefix |
Mockery::mock('alias:' . Greeter::class) |
Greeter&Mockery\MockInterface |
overload: prefix |
Mockery::mock('overload:' . Greeter::class) |
Greeter&Mockery\MockInterface |
| Partial method list | Mockery::mock(Greeter::class . '[greet]') |
Greeter&Mockery\MockInterface |
| Excluded partial list | Mockery::mock(Greeter::class . '[!greet]') |
Greeter&Mockery\MockInterface |
| Comma-separated list | Mockery::mock(Greeter::class . ', ' . Logger::class) |
Greeter&Logger&Mockery\MockInterface |
| Pipe-separated spec | Mockery::mock('alias:' . Greeter::class . '|' . Logger::class) |
Greeter&Logger&Mockery\MockInterface |
MockTypeFactory drops unknown class names from spec strings. If no target resolves to an existing class, interface, or trait, Mago uses Mockery's declared return type.
Fluent mock builders¶
Calling fluent builder methods on a mock keeps the full T&Mockery\MockInterface receiver type instead of collapsing to Mockery\LegacyMockInterface:
makePartial()shouldIgnoreMissing($returnValue = null, $recursive = false)asUndefined()byDefault()shouldAllowMockingProtectedMethods()shouldAllowMockingMethod()shouldDeferMissing()
shouldIgnoreMissing() accepts optional mixed $returnValue and optional bool $recursive.
Mockery\Mock declares asUndefined(), while Mockery\MockInterface does not. AsUndefinedMethodProvider resolves asUndefined() on mock receivers. If the mocked class declares its own asUndefined() method, the provider keeps that method.
Trait mocks and constants¶
Mocking a trait produces MyTrait&Mockery\MockInterface. PHP allows reading trait constants through any class that uses the trait, including a Mockery mock. Mago normally reports direct-trait-constant-access on trait constant reads.
TraitMockConstantIssueFilter removes direct-trait-constant-access only when flow analysis proves that the receiver is a Mockery mock on every reachable path. Reads on a non-mock variable or a literal trait name keep the diagnostic.
PHPUnit lifecycle integration¶
MockeryPlugin registers Mockery's PHPUnit setup methods as class initializers so properties assigned there count as initialized:
| Class or trait | Initializer methods |
|---|---|
Mockery\Adapter\Phpunit\MockeryTestCase |
mockeryTestSetUp(), startMockery(), setUp() |
Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration |
startMockery(), setUp() |
Mockery\Adapter\Phpunit\MockeryTestCaseSetUp |
mockeryTestSetUp(), setUp() |
It also registers five lifecycle methods as analyzer entry points so unused-method checks do not report them:
Mockery\Adapter\Phpunit\MockeryTestCase::mockeryTestSetUp()Mockery\Adapter\Phpunit\MockeryTestCase::mockeryTestTearDown()PHPUnit\Framework\TestCase::mockeryAssertPostConditions()PHPUnit\Framework\TestCase::startMockery()PHPUnit\Framework\TestCase::purgeMockeryContainer()