简体   繁体   English

如何操作控制器测试的应用程序配置?

[英]How to manipulate the application configs for controller tests?

I'm writing functional / controller tests for a ZF3 application (driven by PHPUnit and zendframework/zend-test ).我正在为 ZF3 应用程序编写功能/控制器测试(由 PHPUnit 和zendframework/zend-test )。 Like this:像这样:

public function testWhatEver()
{
    $this->dispatch('/');
    $this->assertResponseStatusCode(Response::STATUS_CODE_200);
}

It's working pretty well.它工作得很好。 But now I got a case, where I need to test the application with multiple mutually exclusive configs.但是现在我遇到了一个案例,我需要使用多个互斥的配置来测试应用程序。

Eg, the case "authentication": The application provides multiple authentication methods (let's say: AuthA , AuthB , AuthC ).例如,案例“身份验证”:应用程序提供多种身份验证方法(假设: AuthAAuthBAuthC )。 (That is configurable via setting of the auth.type 's value in the config file.) I want to test each of them. (这可以通过在配置文件中设置auth.type的值来配置。)我想测试它们中的每一个。 That means, it's not enough to have special test configs in the /config/autoload/test/*{local|global}.php .这意味着,在/config/autoload/test/*{local|global}.php有特殊的测试配置是不够的。 I need to be able to manipulate them for every test (before I call the dispatch(...) method).我需要能够为每个测试操作它们(在我调用dispatch(...)方法之前)。

How to manipulate the application configs for / from controller tests (on the fly)?如何操作控制器测试的/来自控制器测试的应用程序配置(动态)?


If no better solution can be found, a possible workaround might be to edit the config file (by using file_put_contents(...) or something like this) before every test.如果找不到更好的解决方案,可能的解决方法可能是在每次测试之前编辑配置文件(通过使用file_put_contents(...)或类似的东西)。 But it's a bit ugly (and slow).但这有点难看(而且很慢)。

In general I see no really nice solution for this problem.一般来说,我认为这个问题没有很好的解决方案。 But there some more or less acceptable workaround:但是有一些或多或少可以接受的解决方法:

Workaround 1: manipulating the according config file for every test解决方法 1:为每个测试操作相应的配置文件

  1. $configs = file_get_contents(...)
  2. searchByRegexAndManipulateConfigs(...)
  3. file_put_contents(...)

It's much effort and would make the testing slower (due to reading from / writing to the filesystem).这很费力,会使测试变慢(由于从文件系统读取/写入)。

Workaround 2: simple files with only one config value解决方法 2:只有一个配置值的简单文件

We can create files like config.auth.type.php or config.auth.type.txt (one per config value t keep the file really simple) and to use inclue or file_get_contents(...) call as value in the config.我们可以创建像config.auth.type.phpconfig.auth.type.txt这样的文件(每个配置值一个以保持文件非常简单)并使用incluefile_get_contents(...)调用作为配置中的值。 The the value in the file needs to be manipulated before the test execution.文件中的值需要在测试执行之前进行操作。

It's a bit less effort (we don't need to write complex RegEx), but might make the test considerably slower, since every application request would start by reading an additional file.它的工作量要少一些(我们不需要编写复杂的 RegEx),但可能会使测试变得相当慢,因为每个应用程序请求都会从读取一个附加文件开始。

Workaround 3: passing configs values through GLOBALS解决方法 3:通过GLOBALS传递配置值

It's the simplest and fastest variant.这是最简单和最快的变体。 We just save the needed value into a global variable and read it in the config (file's) array.我们只是将所需的值保存到一个全局变量中并在配置(文件的)数组中读取它。 After the test we remove the variable:测试后我们删除变量:

AuthBTest

...
protected function setUp() // or setUpBeforeClass()
{
    parent::setUp();
    $GLOBALS['appTestConfigs']['auth.type'] = 'AuthA';
}

protected function tearDown() // or tearDownAfterClass()
{
    parent::tearDown();
    unset($GLOBALS['appTestConfigs']);
}
...

/config/autoload/test/local.php

return [
    'auth' => [
        'type' => isset($GLOBALS['appTestConfigs']['auth.type']) ? $GLOBALS['appTestConfigs']['auth.type'] : 'AuthA',
    ],
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何向现有应用程序添加测试? - How to add tests to existing application? 在zend框架中运行控制器单元测试时获取“没有为此应用程序定义的默认模块”异常 - Getting a “No default module defined for this application” exception while running controller unit tests in zend framework 如何将ZF2单元/应用模块测试整合为一个调用? - How to consolidate ZF2 unit/application module tests into a single call? PHPUnit - 测试路由是否命中控制器 - PHPUnit - tests if route hitting controller Laravel控制器测试,处理重定向 - Laravel controller tests, handling redirect 如何在Zend Framework 2的测试上下文中获取对Config服务的配置? - How to get configs to the Config service in the testing context in Zend Framework 2? 如何在phpunit功能测试中模拟symfony控制器的自动服务? - How can I mock an autowired service to symfony controller in phpunit functional tests? 无法在Laravel中运行多个控制器测试 - Unable to run multiple controller tests in Laravel 在控制器测试中覆盖Symfony 3.3容器服务 - Overwriting Symfony 3.3 container services in controller tests 使用Laravel模型绑定模拟控制器测试 - Mocking in controller tests with Laravel model binding
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM