views:

33

answers:

1

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.

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.

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.

+1  A: 

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.

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.

nuqqsa
But in both cases I still need to make changes in all my existed tests. And I am looking for some way not to do it. If it is possible.
darja
The question was: Can you tell me what is the best practice to make sessions for each test isolated? Well, these are a couple of options. On the other hand, implementing a shared fixture doesn't really require many changes, you only need to create a test suite for all those tests requiring session authentication and move the fixtures from the tests to a common one in the suite.There's still another option, which is to create a mock of Zend_Auth for testing purposes only. Then it's just a matter of find and replace.
nuqqsa
Hi there, I recalled yet another approach that may work for you (fake the user identity), please see: http://stackoverflow.com/questions/2937387/is-there-any-way-to-force-authentication-with-zend-auth/2939327#2939327
nuqqsa