2

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?

4

1 回答 1

0

今天重新审视这个问题时,有一个完全令人震惊的时刻:

我的 Zend_Session_Namespace 上的“消息”属性实际上是一个神奇的属性(使用 __get 和 __set 访问),并且无法通过反射识别(这是 PHPUnit 在调用 assertObjectHasAttribute 时测试该属性是否存在的方式)。

从测试中删除断言允许它继续,我的测试现在在预期的点失败,而不是过早地失败。

于 2012-10-02T07:30:06.867 回答