Skip to content

Composition

This guide shows how to run friendsofmago/mago-mockery inside a custom Mago worker alongside other extensions, so all extensions share one PHP worker pool.

Register the full extension in a shared worker

Create a worker script such as .mago/worker.php in your project and pass MockeryExtension::create() to Mago\Sdk\Worker:

<?php

declare(strict_types=1);

use Mago\Sdk\Worker;
use MagoMockery\MockeryExtension;

ini_set('display_errors', 'stderr');
ob_start(static function (string $output): string {
    if ($output !== '') {
        fwrite(STDERR, $output);
    }

    return '';
}, 1);

try {
    require dirname(__DIR__) . '/vendor/autoload.php';

    (new Worker(
        MockeryExtension::create(),
        // Add other Mago\Sdk\Extension instances here.
    ))->run();
} catch (Throwable $error) {
    fwrite(STDERR, 'mago-worker: ' . $error::class . ': ' . $error->getMessage() . "\n");
    exit(1);
}

Point mago.toml at your custom worker script instead of vendor/bin/mago-mockery-worker:

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

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

Do not keep both [extension-hosts.mockery] and your custom host enabled at the same time. Mago rejects duplicate plugin identifiers across hosts.

Compose plugins inside another extension

If you maintain a wrapper extension that combines plugins from several packages, call MockeryExtension::plugins() and merge the returned list into your Mago\Sdk\Extension:

<?php

declare(strict_types=1);

use Mago\Sdk\Extension;
use MagoMockery\MockeryExtension;

$extension = new Extension(
    identifier: 'acme/mago-extensions',
    name: 'Acme Extensions',
    version: '1.0.0',
    analyzerPlugins: [
        ...MockeryExtension::plugins(),
        // Add your own Analyzer\Plugin instances here.
    ],
);

Configure strict mode and named fetch programmatically

MockeryExtension::plugins() accepts two optional arguments:

MockeryExtension::plugins(
    strictMockedMethods: null,
    namedFetch: true,
);
  • Set strictMockedMethods: true to turn on mockery/unknown-mocked-method regardless of MAGO_MOCKERY_STRICT_METHODS.
  • Set strictMockedMethods: false to keep strict mode off even if the environment sets MAGO_MOCKERY_STRICT_METHODS.
  • Leave strictMockedMethods: null, the default, to read MAGO_MOCKERY_STRICT_METHODS from the worker environment.
  • Set namedFetch: false to omit NamedFetchMockPlugin if your project never uses Mockery::namedMock() with Mockery::fetchMock().

Public API guarantee

Only MagoMockery\MockeryExtension::create() and MagoMockery\MockeryExtension::plugins() are part of the public PHP API. All classes under MagoMockery\Analyzer carry @internal and may change in minor releases. See Limitations for the full compatibility policy.