springernature/behat-zf-extension

将 Zend Framework 集成到 Behat 的扩展

0.1.1 2018-05-25 05:49 UTC

This package is not auto-updated.

Last update: 2021-11-13 14:10:51 UTC


README

Zf-behat-extension 是 Behat 3.0+ 与 Zend Framework 2/3 之间的集成层,它提供以下功能:

  • 由配置驱动的应用程序自动启动
  • ZendBootstrapDictionaryZendBootstrapOverride,为您的上下文提供 Zend\Mvc\Application 的启动实例
  • 参数解析器,用于将容器中的服务传递到您的上下文
  • (如果已安装 MinkExtension)为 Mink 添加额外的 zend 驱动程序

支持的平台

  • PHP 5.4+
  • Behat 3.0+
  • Zend Framework 2.x 和 3.x

如何安装

此扩展需要

  • Behat 3.0+
  • Zend Framework 2.x 或 3.x

建议的安装方法是通过 Composer

    $ composer require --dev springernature/behat-zf-extension

然后您可以在您的 behat.yml 中激活此扩展

        default:
            # ...
            extensions:
                SpringerNature\Behat\ZFExtension: ~

可选地,您可以在 behat.yml 中激活 Mink 驱动程序

        default:
            # ...
            extensions:
                Behat\MinkExtension:
                    default_session: 'zend'
                    sessions:
                        zend:
                            zend: ~

注意

本文档中的大多数示例都显示了通过 vendor/bin/behat 运行的 behat,这是通过 Composer 安装时的默认位置。

许可

此软件可在 MIT 许可证 下使用。

维护

请将问题和 PR 提交给这个 github。

用法

覆盖服务

通常,为了在测试中获得控制权,您可能希望覆盖应用程序中的服务或其他设置。

此扩展将公开并允许按以下方式覆盖应用程序。

<?php

namespace behat\MyApp\Context;

class DefaultContext
    extends \Behat\MinkExtension\Context\MinkContext
    implements \SpringerNature\Behat\ZFExtension\ZendBootstrapOverride
{
    use \SpringerNature\Behat\ZFExtension\ZendBootstrapDictionary;

    public function overrideApplication(\Zend\Mvc\Application $application)
    {
        $serviceManager = $application->getServiceManager();

        $overridenService = new MyService();
        $overridenService->doSomethingDifferent(true);

        $serviceManager->setAllowOverride(true);
        $serviceManager->setService('my_service', $overridenService);
        $serviceManager->setAllowOverride(false);
    }
}

为上下文提供应用程序服务

如果您想将应用程序容器实例化的服务传递到您的上下文,请参阅 behat.yml

    # ...
    suites:
       default:
          contexts:
            - MyAwesomeContext:
                myservice: 'MyApp\Service\MyService' # this is the key registered in the container

在上下文中

<?php

namespace behat\MyApp\Context;

class DefaultContext extends \Behat\MinkExtension\Context\MinkContext
{
    public function __construct(\MyApp\Service\MyService $myservice)
    {
       // ...
    }
}

处理文件上传

Zend 内置了 php 文件上传检查器,这使得在 behat 中测试文件上传变得不可能。如果您尝试注入一个文件,它将给出以下错误消息:文件非法上传。这可能是潜在的攻击

要允许验证在项目 composer.json 文件中通过,将文件的路径添加到 autoload-dev。例如

    "autoload-dev": {
        "files": ["vendor/springernature/behat-zf-extension/src/DisablePhpUploadChecks.php"]
    }

添加 files 行后,执行 composer dumpautoload 以加载文件。

配置

典型的配置如下(behat.yml.dist

extensions:
  SpringerNature\Behat\ZFExtension:
    application_config_path: path/to/custom/behat.config.php
  Behat\MinkExtension:
    default_session: 'zend'
    sessions:
      zend:
        zend: ~

由于扩展启动了 ZF 应用程序,您可以使用上面的示例使用 application_config_path 选项指定应用程序配置。大多数情况下,此文件将包括正确启动应用程序所需的模块(如 application.config.php)。

也可以通过使用选项 bootstrap 来指定一个外部引导文件。

额外内容

这个扩展受到了Konstantin Kudryashov的Symfony2extension的启发。