ramunasd/symfony-container-mocks
Composer 安装命令:
composer require ramunasd/symfony-container-mocks
包简介
Provides extended Symfony dependency injection container enabling service mocking.
README 文档
README
This container enables you to mock services in the Symfony dependency injection container. It is particularly useful in functional tests.
Features
- Can replace any Symfony service or parameter
- Automatically detects service class from service definition
- Can be used with any mocking framework
- Compatible with Symfony versions 2.7 - 3.4
- Works on all supported version of PHP
OTB supported mocking frameworks
Installation
Add SymfonyContainerMocks using composer:
composer require "ramunasd/symfony-container-mocks"
or edit your composer.json:
{ "require": { "ramunasd/symfony-container-mocks": "*" } }
Replace base container class for test environment in app/AppKernel.php
<?php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; use RDV\SymfonyContainerMocks\DependencyInjection\TestKernelTrait; class AppKernel extends Kernel { // use special container when env=test use TestKernelTrait; public function registerBundles() { return [ // bundles ]; } public function registerContainerConfiguration(LoaderInterface $loader) { } }
And clear application cache.
Examples
Inject mocked service
<?php namespace Acme\Bundle\AcmeBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Acme\Bundle\AcmeBundle\Service\Custom; class AcmeControllerTest extends WebTestCase { /** * @var \Symfony\Bundle\FrameworkBundle\Client $client */ private $client; public function setUp() { parent::setUp(); $this->client = static::createClient(); } public function tearDown() { $this->client->getContainer()->tearDown(); $this->client = null; parent::tearDown(); } public function testSomethingWithMockedService() { $this->client->getContainer()->prophesize('acme.service.custom', Custom::class) ->someMethod([]) ->willReturn(false) ->shouldBeCalledTimes(2); // ... } }
Inject automatically mocked service
feature works only with flag "debug" enabled.
$mock = $this->client->getContainer()->prophesize('acme.service.custom'); $mock ->myMethod() ->willReturn(true);
Other mocking frameworks
// create stub $mock = $this->getMock(Custom::class); // inject service mock self::$kernel->getContainer()->setMock('acme.service.custom', $mock); // reset container state self::$kernel->getContainer()->unMock('acme.service.custom');
Set specific framework parameter
// set custom value during test self::$kernel->getContainer()->setMockedParameter('acme.service.parameter1', 'customValue1'); // trigger service, assert results // reset all parameters to original values self::$kernel->getContainer()->clearMockedParameters();
Things TO DO
- Symfony 4.x support
- PSR-11 adoption
Similar/Related projects
- https://github.com/jakzal/phpunit-injector - inject Symfony services into PHPUnit test cases
ramunasd/symfony-container-mocks 适用场景与选型建议
ramunasd/symfony-container-mocks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97.55k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2016 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「symfony」 「phpunit」 「TDD」 「test」 「prophecy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ramunasd/symfony-container-mocks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ramunasd/symfony-container-mocks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ramunasd/symfony-container-mocks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
The bundle for easy using json-rpc api on your project
A fast and intuitive dependency injection container.
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 97.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 12
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-24