Skip to content

The worker

Mago runs PHP extensions in separate PHP processes called workers. friendsofmago/mago-mockery bundles a worker entrypoint so you do not need to write boilerplate PHP to run the extension.

Entrypoints

Two files start the bundled worker:

  • vendor/bin/mago-mockery-worker, exposed from bin/mago-mockery-worker through Composer's bin field
  • vendor/friendsofmago/mago-mockery/resources/worker.php

bin/mago-mockery-worker loads resources/worker.php. Both paths run (new Worker(MockeryExtension::create()))->run().

Protocol streams

Mago reads stdout from the worker as its binary protocol stream. resources/worker.php sets ini_set('display_errors', 'stderr') and installs an output buffer callback that redirects any stray output to stderr.

If bootstrap fails, the worker writes a message prefixed with mago-mockery: to stderr and exits with code 1.

Pass -d flags in the command array of [extension-hosts.mockery]:

[extension-hosts.mockery]
command = ["php", "-d", "xdebug.mode=off", "-d", "pcov.enabled=0", "-d", "memory_limit=512M", "vendor/bin/mago-mockery-worker"]
workers = 1
  • -d xdebug.mode=off disables Xdebug in the worker. PHP ignores the flag when Xdebug is not installed.
  • -d pcov.enabled=0 disables PCOV in the worker. PHP ignores the flag when PCOV is not installed.
  • -d memory_limit=512M raises the memory limit for large test files with thousands of lines. Small and medium test suites also run within PHP's default 128M limit.

The XDEBUG_MODE environment variable overrides -d xdebug.mode=off. Run XDEBUG_MODE=debug vendor/bin/mago analyze when you want to step through the worker in a debugger.

Composer autoloader resolution

resources/worker.php checks these autoload.php paths in order. It loads the first file whose composer/autoload_psr4.php maps MagoMockery\ to this package's src/ directory:

  1. $GLOBALS['_composer_autoload_path'], when Composer's bin proxy invokes the worker.
  2. dirname(__DIR__, 3) . '/autoload.php', for standard installs under vendor/friendsofmago/mago-mockery/resources/worker.php.
  3. dirname($argv[0], 4) . '/autoload.php', for symlinked Composer path repositories under vendor/.
  4. dirname($argv[0], 2) . '/autoload.php', when Mago runs vendor/bin/mago-mockery-worker.
  5. getcwd() . '/vendor/autoload.php', when the working directory links this package as a path repository.
  6. dirname(__DIR__) . '/vendor/autoload.php', when you run the worker directly from a repository checkout.

If no candidate defines both Mago\Sdk\Worker and MagoMockery\MockeryExtension, the worker writes mago-mockery: could not locate the Composer autoloader. to stderr and exits with code 1.

Passing environment variables

Mago passes variables listed under [extension-hosts.mockery] environment to the worker process:

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

If you want to combine friendsofmago/mago-mockery with other Mago extensions in a single worker pool, see Composition.