Expectations¶
MockeryPlugin types Mockery expectation chains, spy verifications, higher-order messages, and argument matchers so fluent calls stay checked from start to finish.
Expectation factories¶
Mockery's docblocks type shouldReceive() as a union of ExpectationInterface, Expectation, and HigherOrderMessage. At runtime, shouldReceive() with arguments returns a Mockery\CompositeExpectation. CompositeExpectationProvider returns a parameterized Mockery\CompositeExpectation<T&Mockery\MockInterface> that carries the mocked type through the chain.
| Call | Return type |
|---|---|
$mock->shouldReceive('greet') |
Mockery\CompositeExpectation<T&MockInterface> |
$mock->shouldNotReceive('greet') |
Mockery\CompositeExpectation<T&MockInterface> |
$mock->allows('greet') |
Mockery\CompositeExpectation<T&MockInterface> |
$mock->allows(['greet' => 'hi']) |
T&MockInterface |
$mock->allows($possiblyEmptyArray) |
Mockery\HigherOrderMessage<Mockery\CompositeExpectation<T&MockInterface>, T&MockInterface>|(T&MockInterface) |
$mock->expects('greet') |
Mockery\CompositeExpectation<T&MockInterface> |
The extension also overrides shouldReceive() and shouldNotReceive() to accept variadic string|array<string, mixed> ...$methodNames.
Composite expectation forwarding¶
Mockery\CompositeExpectation forwards most expectation calls through __call() to Mockery\Expectation. CompositeExpectationProvider resolves those calls as follows:
- Declared methods keep their real return types.
add()returnsvoid,getOrderNumber()returnsint, and__toString()returnsstring. getMock()andmock()unwrap the mock type. Both returnT&Mockery\MockInterface.- Expectation chaining preserves the mock type.
andReturn(),andReturns(),shouldReceive('other'), andshouldNotReceive('other')returnMockery\CompositeExpectation<T&MockInterface>. - Forwarded
Expectationmethods stay checked.CompositeExpectationProviderresolves every public instance method onMockery\Expectation, includingwith(),once(),times(),andReturnUsing(), andandThrow(). Each method keeps its real parameter types and returnsMockery\CompositeExpectation<T&MockInterface>. - Unknown or non-public methods stay unresolved. Calling an unknown method on
CompositeExpectationreports Mago'snon-documented-method.
$mock = Mockery::mock(Greeter::class);
$result = $mock
->shouldReceive('greet')
->with('Ada')
->once()
->andReturn('hello')
->getMock();
// $result has type Greeter&Mockery\MockInterface
Spy verification¶
VerificationProvider types spy verification methods according to Mockery's runtime return values:
| Call | Return type |
|---|---|
$spy->shouldHaveReceived('greet') |
Mockery\VerificationDirector |
$spy->shouldHaveBeenCalled() |
Mockery\VerificationDirector |
$spy->shouldNotHaveReceived('greet') |
void |
$spy->shouldNotHaveBeenCalled() |
void |
shouldHaveReceived() and shouldNotHaveReceived() accept optional string|null $method = null and optional array<int|string, mixed>|Closure|null $args = null.
Calling an expectation method such as andReturn() on a VerificationDirector reports non-existent-method. Chaining any method after shouldNotHaveReceived('greet') or shouldNotHaveBeenCalled() reports invalid-method-access because both return void.
Higher-order messages¶
Zero-argument, explicit null, and empty-array forms return Mockery\HigherOrderMessage<ForwardedReturnType, MockType> or Mockery\ExpectsHigherOrderMessage<ForwardedReturnType, MockType>. The first type parameter is the forwarded call's runtime return type, and the second is the mocked type:
| Call | Intermediate type | Forwarded call return type |
|---|---|---|
$mock->shouldReceive() |
Mockery\HigherOrderMessage<Mockery\CompositeExpectation<T&MockInterface>, T&MockInterface> |
Mockery\CompositeExpectation<T&MockInterface> |
$mock->shouldNotReceive() |
Mockery\HigherOrderMessage<Mockery\CompositeExpectation<T&MockInterface>, T&MockInterface> |
Mockery\CompositeExpectation<T&MockInterface> |
$mock->allows(), allows(null), allows([]) |
Mockery\HigherOrderMessage<Mockery\CompositeExpectation<T&MockInterface>, T&MockInterface> |
Mockery\CompositeExpectation<T&MockInterface> |
$mock->expects(), expects(null) |
Mockery\ExpectsHigherOrderMessage<Mockery\CompositeExpectation<T&MockInterface>, T&MockInterface> |
Mockery\CompositeExpectation<T&MockInterface> |
$spy->shouldHaveReceived(), shouldHaveReceived(null) |
Mockery\HigherOrderMessage<Mockery\VerificationDirector, T&MockInterface> |
Mockery\VerificationDirector |
$spy->shouldNotHaveReceived(), shouldNotHaveReceived(null) |
Mockery\HigherOrderMessage<void, T&MockInterface> |
void |
HigherOrderMessageProvider forwards any non-magic method call on those objects with variadic mixed ...$arguments and returns the forwarded return type. Chaining ->once() after $spy->shouldNotHaveReceived()->greet() reports invalid-method-access.
Refined expectation and matcher signatures¶
ExpectationAndMatcherSignatureProvider overrides inaccurate or overly broad Mockery 1.6 docblock signatures:
| Method | Effective signature or return type |
|---|---|
Mockery\Expectation::andSet() |
(string $name, mixed ...$values) |
Mockery\Expectation::set() |
(string $name, mixed $value, mixed ...$values), positional arguments only |
Mockery\Expectation::andReturnArg() |
(int<0, max> $index) |
Mockery\VerificationDirector::withArgs() |
(array<int|string, mixed>|Closure $args) |
Mockery::type() |
(string $expected) |
Mockery::pattern() |
(string $expected) |
Mockery::ducktype() |
(string ...$args) |
Mockery::hasKey() |
(int|string $key) |
Mockery::isEqual() |
Returns Mockery\Matcher\IsEqual |
Mockery::isSame() |
Returns Mockery\Matcher\IsSame |