Skip to content

Mago for Mockery

friendsofmago/mago-mockery is a Mago analyzer extension for Mockery 1.6. It types mocks as the mocked class intersected with Mockery\MockInterface, so mago analyze checks calls on a mock like calls on the real class.

  • Mocks and spies. Types Mockery::mock(), Mockery::spy(), Mockery::namedMock(), Mockery::instanceMock(), Container::mock(), and the global helpers mock(), spy(), and namedMock(). See Mocks and spies.
  • Expectations and verification. Types shouldReceive(), shouldNotReceive(), allows(), expects(), forwarded Expectation calls, higher-order messages, VerificationDirector, and argument matchers. See Expectations.
  • Named mocks. Resolves Mockery::fetchMock() and Container::fetchMock() against same-file and trait-lifecycle namedMock() registrations. See Named mocks.
  • Strict method checks. Reports literal and higher-order method names that do not exist on the mocked class when you set MAGO_MOCKERY_STRICT_METHODS=1. See Strict mode and Diagnostics.

Without the extension, Mago sees a mock as plain Mockery\MockInterface. With it, Mago catches a wrong mock type before PHPUnit runs.

$api = Mockery::mock(Greeter::class);
$api->shouldReceive('greet')->andReturn('hello');
$this->acceptApiClient($api); // invalid-argument: Greeter&MockInterface is not ApiClient

Quick start

You need PHP 8.1 or newer, Mago 1.60.2 or newer, and Mockery 1.6 or newer.

Install the package with Composer:

composer require --dev friendsofmago/mago-mockery

Add Mockery's library path and the bundled worker to mago.toml:

[source]
includes = ["vendor/mockery/mockery/library"]

[extension-hosts.mockery]
command = ["php", "-d", "xdebug.mode=off", "-d", "pcov.enabled=0", "-d", "memory_limit=512M", "vendor/bin/mago-mockery-worker"]
workers = 1

Validate the worker and run the analyzer:

vendor/bin/mago extension validate
vendor/bin/mago analyze

Next steps

Read the setup pages to finish configuring your project:

  • Install walks through installation and verification.
  • Configuration lists every mago.toml option and environment variable.
  • The worker documents vendor/bin/mago-mockery-worker and resources/worker.php.
  • Composition explains how to load Mockery inside a shared worker with other Mago extensions.