在PHPUnit测试中隔离Zend_Session

时间:2022-10-15 22:47:48

I am testing authentification functionality of my site. Zend_Auth is using as authorization engine. But auth status remains between tests and I need to write 'logout' in every tearDown.

我正在测试我网站的身份验证功能。 Zend_Auth正在使用授权引擎。但是auth状态仍然在测试之间,我需要在每个tearDown中写'logout'。

Now everything is all right. But the problem is following. As for I know Zend_Auth uses Zend_Session for storing auth data. So, session is common for all tests. I am affraid that in future it can cause problems.

现在一切都好。但问题是如下。至于我知道Zend_Auth使用Zend_Session来存储auth数据。因此,会话对于所有测试都很常见。我担心将来它会引起问题。

Can you tell me what is the best practice to make sessions for each test isolated?

你能告诉我为每个测试分离会话的最佳做法是什么?

Now I can imagine only manually starting session in setUp and stop in tearDown. But I have many tests and implementing this can take a lot of time.

现在我可以想象只在setUp中手动启动会话并在tearDown中停止。但是我有很多测试,实现这个可能需要很多时间。

1 个解决方案

#1


1  

PHPUnit allows you to define a shared fixture (a common set up) for a whole test suite. However, this solution just masks a flaw in the design of the tests, since it doesn't address their dependency on a common global state. A better solution is to use test doubles, creating custom stubs of certain classes to control the behaviour of chosen parts of the system.

PHPUnit允许您为整个测试套件定义共享夹具(通用设置)。但是,这个解决方案只是掩盖了测试设计中的一个缺陷,因为它没有解决它们对常见全局状态的依赖性。更好的解决方案是使用测试双精度,创建某些类的自定义存根来控制系统所选部分的行为。

In this case you could try to create a stub of Zend_Auth so that it skips the use of Zend_Session and returns the desired permissions for each unit test.

在这种情况下,您可以尝试创建Zend_Auth的存根,以便它跳过Zend_Session的使用并返回每个单元测试所需的权限。

#1


1  

PHPUnit allows you to define a shared fixture (a common set up) for a whole test suite. However, this solution just masks a flaw in the design of the tests, since it doesn't address their dependency on a common global state. A better solution is to use test doubles, creating custom stubs of certain classes to control the behaviour of chosen parts of the system.

PHPUnit允许您为整个测试套件定义共享夹具(通用设置)。但是,这个解决方案只是掩盖了测试设计中的一个缺陷,因为它没有解决它们对常见全局状态的依赖性。更好的解决方案是使用测试双精度,创建某些类的自定义存根来控制系统所选部分的行为。

In this case you could try to create a stub of Zend_Auth so that it skips the use of Zend_Session and returns the desired permissions for each unit test.

在这种情况下,您可以尝试创建Zend_Auth的存根,以便它跳过Zend_Session的使用并返回每个单元测试所需的权限。