# Mago for Mockery

`friendsofmago/mago-mockery` is a [Mago](<https://mago.carthage.software>) 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](<https://friendsofmago.github.io/mago-mockery/analyzer/mocks-and-spies/index.md>).
- **Expectations and verification.** Types `shouldReceive()`, `shouldNotReceive()`, `allows()`, `expects()`, forwarded `Expectation` calls, higher-order messages, `VerificationDirector`, and argument matchers. See [Expectations](<https://friendsofmago.github.io/mago-mockery/analyzer/expectations/index.md>).
- **Named mocks.** Resolves `Mockery::fetchMock()` and `Container::fetchMock()` against same-file and trait-lifecycle `namedMock()` registrations. See [Named mocks](<https://friendsofmago.github.io/mago-mockery/analyzer/named-mocks/index.md>).
- **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](<https://friendsofmago.github.io/mago-mockery/analyzer/strict-mode/index.md>) and [Diagnostics](<https://friendsofmago.github.io/mago-mockery/analyzer/diagnostics/index.md>).

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](<https://friendsofmago.github.io/mago-mockery/setup/install/index.md>) walks through installation and verification.
- [Configuration](<https://friendsofmago.github.io/mago-mockery/setup/configuration/index.md>) lists every `mago.toml` option and environment variable.
- [The worker](<https://friendsofmago.github.io/mago-mockery/setup/worker/index.md>) documents `vendor/bin/mago-mockery-worker` and `resources/worker.php`.
- [Composition](<https://friendsofmago.github.io/mago-mockery/setup/composition/index.md>) explains how to load Mockery inside a shared worker with other Mago extensions.
