Skip to content

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) and Mockery::mock(LoggableTrait::class) produce Greeter&Mockery\MockInterface and LoggableTrait&Mockery\MockInterface.
  • Generic class-string<T>. Passing class-string<Box<int>> preserves the generic parameters on Box<int>&Mockery\MockInterface. Passing a generic template class-string<T> returns T&Mockery\MockInterface.
  • Object partial mocks. Passing an object such as Mockery::mock(new GreeterImpl()) returns GreeterImpl&Mockery\MockInterface and keeps generic type arguments and existing intersections on that object.
  • Multiple targets. Passing several targets such as Mockery::mock(Greeter::class, Stringable::class) returns Greeter&Stringable&Mockery\MockInterface.
  • Non-target arguments. MockTypeFactory ignores constructor argument arrays, quick definition maps, Closure callbacks, Mockery\Generator\MockConfigurationBuilder, Mockery\MockInterface, and Mockery\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()