views:

119

answers:

1

I have an application that is behind a login and utilizes zend_acl and zend_auth.

During pre-dispatch I have an ACL plugin that creates all the rules out for the ACL. I also have an Auth plugin that checks if you're logged in or not and if so if you have access to the requested resource according to the ACL.

As the application is entirely behind a login the ACL is only created if you're logged in.

Unit testing this appears to be impossible, or rather more likely I'm missing something obvious.

In my unit test setup method I simulate a successful login that returns a zend_auth instance. Tests that do pass indicate that this login was successful.

However, if I then through tests attempt to dispatch to another location, or assess if the logged in user has access to a given resource it is always rejected by the plugin as they're still not logged in. I am not sure why this is, can anyone advise?

For example this passes:

 public function testLoggedIn()
 {
  $this->assertTrue( Zend_Auth::getInstance()->hasIdentity() );
 }

This fails as it's rejected by the plugin:

 public function testUserAccess()
 {

   $this->dispatch('/home');
          $this->assertResponseCode(200);
          $this->assertQueryContentContains('#nav_side');  
          $this->resetRequest()
           ->resetResponse();

 }

This, I have found still seems to be redirecting back to the login page as the plugins don't know the user is logged in.

Any help much appreciated.

A: 

The Problem you describe happens a lot with the using of global Variables and the OOP global Variable (the Singleton Pattern).

There is a Article by the Author of PHPUnit that describes how you can avoid that by using Dependency Injection and what other possibilities you've got and since it's more verbose that i could be here i just suggest you read it :) http://sebastian-bergmann.de/archives/882-Testing-Code-That-Uses-Singletons.html

As a ugly alternative (if you need a quick result) you could create a stub of Zend_Auth (describe in the Link) and use the PHP 5.3 reflection API to set the Zend_Auth instance Variable to your stub.

Hope that helps (as the Question lived 4h without an other answer)

edorian
Yes- thank you that appears to be the issue.
Stephen Maher