1

I have ZF1 site and I'm testing it with phpunit.
I want to store cookies from one test(login) and pass it to other tests(post messages, etc).

Or is there any way to setIdentity?

This method doesn't work:

class IntegrationalTest extends ControllerTestCase {

    protected $identity;

    public function test1()
    {
        // some code here
        $this->assertTrue(Zend_Auth::getInstance()->hasIdentity()); // GOOD
        $this->identity = Zend_Auth::getInstance()->getIdentity();
    }

    public function test2() {
        Zend_Auth::getInstance()->getStorage()->write($this->identity);
        $this->assertTrue(Zend_Auth::getInstance()->hasIdentity()); // FAILED!
    }
}
Dmitry
  • 7,457
  • 12
  • 57
  • 83

1 Answers1

2

Doing that is in conflict with unit testing idea. Tests are in isolation. You don't pass any states between them. Use fixtures for setting the environment. If you test a Model don't use ControllerTestCase. Use PHPUnit_Framework_TestCase instead.

Lukasz Kujawa
  • 3,026
  • 1
  • 28
  • 43
  • Agree, so how can I set predefined identity for test2? – Dmitry Sep 29 '12 at 20:45
  • 1
    In this particular case I would take a different approach. I would use a dataProvider (http://www.phpunit.de/manual/3.2/en/writing-tests-for-phpunit.html) and have just one test. The test will have 2 parameters $identity, $expectedStatus. The test will set identity (or not) according to provided data. I would use $this->assertEquals($result, $expectedStatus). I'm aware this answer might be little bit cloudy. Please read the link I pasted here to see an example. – Lukasz Kujawa Sep 29 '12 at 21:01