polishsymfonycommunity/mockery-bundle

此包已被 弃用 且不再维护。未建议替代包。

此 Bundle 提供了 Mockery 集成,以便在 Behat 中进行服务模拟。

v1.0.0 2012-03-02 16:58 UTC

This package is auto-updated.

Last update: 2022-02-01 12:20:04 UTC


README

注意:由于 Behat 2.4 获得了自己的扩展系统,因此此包已被 Symfony2MockerExtension 替换。

Build Status

Symfony2 Mockery 集成包。目前它支持服务模拟。

安装

将 PSSMockeryBundle 添加到您的 composer.json

{
    "require": {
        "polishsymfonycommunity/mockery-bundle": "*"
    }
}

使用方法

app/AppKernel.php 中替换测试环境的基容器类:

/**
 * @return string
 */
protected function getContainerBaseClass()
{
    if ('test' == $this->environment) {
        return '\PSS\Bundle\MockeryBundle\DependencyInjection\MockerContainer';
    }

    return parent::getContainerBaseClass();
}

清除您的缓存

要使用 Behat,请在您的 FeatureContext 类中启用子上下文:

/**
 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $kernel
 *
 * @return null
 */
public function __construct(HttpKernelInterface $kernel)
{
    parent::__construct($kernel);

    $this->useContext('container', new \PSS\Bundle\MockeryBundle\Behat\Context\MockerContainerContext($kernel));
}

示例故事:

Feature: Submitting contact request form
  As a Visitor
  I want to contact sales
  In order to receive more information

  Scenario: Submitting the form
    When I go to "/contact-us"
     And I complete the contact us form with following information
       |First name|Last name|Email                |
       |Jakub     |Zalas    |jzalas+spam@gmail.com|
     And CRM API is available
     And I submit the contact us form
    Then the contact request should be sent to the CRM

步骤定义:

/**
 * @Given /^CRM API is available$/
 *
 * @return null
 */
public function crmApiIsAvailable()
{
    $this->getMainContext()->getSubContext('container')
        ->mockService('crm.client', 'PSS\Crm\Client')
        ->shouldReceive('send')
        ->once()
        ->andReturn(true);
}

/**
 * @Given /^(the )?contact request should be sent to (the )?CRM$/
 *
 * @return null
 */
public function theContactRequestShouldBeSentToCrm()
{
    return new Then(sprintf('the "%s" service should meet my expectations', 'crm.client'));
}

所有期望都会在 @afterScenario 钩子中自动检查。手动执行这只能提高场景的可读性并给出更好的错误信息。

讨论

  • 它必须是一个包吗?目前它更像是一个独立的 Symfony 库。
  • 这是否是正确的做法/实现?
  • 我们需要更多功能吗?