[英]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
).例如,案例“身份验证”:应用程序提供多种身份验证方法(假设:
AuthA
、 AuthB
、 AuthC
)。 (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:为每个测试操作相应的配置文件
$configs = file_get_contents(...)
searchByRegexAndManipulateConfigs(...)
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.php
或config.auth.type.txt
这样的文件(每个配置值一个以保持文件非常简单)并使用inclue
或file_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.