meritum/testing 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

meritum/testing

Composer 安装命令:

composer require meritum/testing

包简介

Test kernel orchestration for the Meritum ecosystem

README 文档

README

Test kernel orchestration for the Meritum ecosystem — boots and tears down the app kernels under test, applies service overrides and mocks before boot, and manages the test-support dependencies (e.g. a database connection for model factories) needed alongside them.

Requirements

  • PHP 8.4+
  • georgeff/kernel ^1.10
  • mockery/mockery ^1.6

Installation

composer require meritum/testing

Usage

Managing kernels

TestingKernel is a plain Georgeff\Kernel\Kernel that takes custody of one or more already-built, unbooted app kernels via manages(). Booting the testing kernel boots itself first, then boots every managed kernel:

use Georgeff\Kernel\Environment;
use Meritum\Testing\TestingKernel;

$kernel = new TestingKernel(Environment::Testing);

$kernel->manages($httpKernel, 'http');
$kernel->manages($cliKernel, 'cli');

$kernel->boot();

$httpKernel = $kernel->get('http');

manages() requires an id — there's no optional/anonymous form — so a managed kernel can always be looked up later by the same string, regardless of how many kernels are involved. Convention is to key it by the kernel's own contract (e.g. an interface FQCN) rather than an arbitrary label, so anything built on top of meritum/testing has a stable name to resolve against. Both manages() and boot() throw a Georgeff\Kernel\KernelException if the testing kernel has already booted.

Overriding services

instance() and factory() stage a replacement for a service id, applied to every managed kernel when the testing kernel boots:

$queue = new MemoryQueue();

$kernel->instance(QueueInterface::class, $queue);

instance() takes an already-built object — the closure it registers always returns that exact instance, so identity is guaranteed regardless of container caching, which matters for anything a test wants to assert against later (e.g. messages published to $queue). factory() takes a callable instead, for cases that don't need a captured reference back:

$kernel->factory(ClockInterface::class, fn() => new FrozenClock('2026-01-01'));

Both throw if the testing kernel is already booted.

By default an override applies to every managed kernel. Pass one or more managed-kernel ids to scope it to just those:

$kernel->instance(QueueInterface::class, $queue, 'http');

Targeting an id that isn't actually managed throws a KernelException as soon as boot() runs, before any managed kernel starts booting.

Mocking

mock() builds a Mockery mock, registers it as an instance override, and returns it so expectations can be set before boot:

$repository = $kernel->mock(RepositoryInterface::class);
$repository->shouldReceive('find')->once()->andReturn($model);

$kernel->boot();

The class to mock defaults to the id itself, so mock(RepositoryInterface::class) is equivalent to mock(RepositoryInterface::class, RepositoryInterface::class). shutdown() calls Mockery::close() automatically, verifying expectations without needing the MockeryPHPUnitIntegration trait on your own test case.

Environment variables

setEnv() stages a variable to be applied via putenv() when the testing kernel boots:

$kernel->setEnv('DB_DATABASE', 'file::memory:?cache=shared');

shutdown() restores whatever the variable was set to beforehand, or unsets it entirely if it didn't exist before — so environment changes never leak into the next test.

Shutdown

shutdown() shuts down every managed kernel, then itself, then closes Mockery and forces gc_collect_cycles():

$kernel->shutdown();

It's a no-op if the testing kernel was never booted, so it's always safe to call unconditionally in a test's teardown.

The base test case

Meritum\Testing\TestCase extends PHPUnit\Framework\TestCase and constructs a fresh TestingKernel in setUp(), available as $this->kernel. It does not call boot() automatically — that stays an explicit call, so a test can register manages()/instance()/mock() right up until it's actually ready, including inline in a test method body:

use Meritum\Testing\TestCase;

final class ExampleTest extends TestCase
{
    protected function modules(): array
    {
        return [new DatabaseModule()];
    }

    protected function environment(): array
    {
        return ['DB_DATABASE' => 'file::memory:?cache=shared'];
    }

    public function test_something(): void
    {
        $this->kernel->manages($httpKernel, 'http');
        $this->kernel->mock(QueueInterface::class);

        $this->kernel->boot();

        // ...
    }
}

Override modules()/environment() to configure the testing kernel's own dependencies (not the managed app kernels' — those are built and configured independently, then handed to manages()). tearDown() calls $this->kernel->shutdown() automatically. onTeardown(callable $callback) is a shortcut for $this->kernel->onShutdown($callback).

meritum/testing 适用场景与选型建议

meritum/testing 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 meritum/testing 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 meritum/testing 我们能提供哪些服务?
定制开发 / 二次开发

基于 meritum/testing 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-15