I'm writing unit tests for a login system - but having difficulties with interacting with the Zend_Session_Namespace object. I'm using the Zend_Test extensions to the PHPUnit framework (ZF 1.10, PHPUnit 3.4.5)
Test
public function testAuthFailure()
{
$request = $this->getRequest();
$request->setMethod("POST");
$request->setPost("username", "testUser");
$request->setPost("password", "testPassword");
$this->dispatch("/auth/login");
$this->assertModule("auth");
$this->assertController("login");
$this->assertAction("validate");
$loginSession = new Zend_Session_Namespace("login");
$this->assertFalse(is_null($loginSession));
$this->assertObjectHasAttribute("messages", $loginSession);
$this->assertFalse(empty($loginSession->messages));
$this->arrayHasKey("error", $loginSession->messages["error"]);
$this->assertEquals($loginSession->messages["error"], "Could not log you in with that username and/or password");
}
Current controller/action code
public function validateAction()
{
$loginSession = new Zend_Session_Namespace("login");
if (!$this->buildForm()->isValid($this->getRequest()->getPost())) {
$loginSession->messages["error"][] = "Please enter your username and password";
}
$loginSession->messages["error"][] = "Could not contact the authentication server";
$this->getResponse()
->setHttpResponseCode(303)
->setHeader("Location", "/auth/login");
}
My unit test is failing at $this->assertObjectHasAttribute("messages", $loginSession);
- whereas I would expect it to fail on the actual content of the message.
I have these lines in my phpunit bootstrap:
Zend_Session::$_unitTestEnabled = true;
Zend_Session::start();
How do I confirm that the session message is correctly set?