How to PHPUnit-test a Controller under BjyAuthorize?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to PHPUnit-test a Controller under BjyAuthorize?

dennis-fedco
I have a Controller, that does nothing so far, but display a view page.  Well, it also check if the user has identity set up, but that's not as important right now.

The thing that is happening behind the scenes is that I am using BjyAuthorize to allow/deny access to that Controller.  It is not visible from the Controller code itself as to who may have access to it and who can't.  BjyAuthorize takes care of that behind the scenes, leaving the Controller code unaware of the access model.  

Question:  How can I test this Controller?  What does it mean to test this Controller?  Do I test this Controller, and if so, for what?

Dennis
Reply | Threaded
Open this post in threaded view
|

Re: How to PHPUnit-test a Controller under BjyAuthorize?

dennis-fedco
To clarify:  

 * my concern is that Controller is not aware of its access model under BjyAuthorize.  Is that they way it was intended to be?  Is that a good practice to leave it be this way?

* Can I test this Controller with various roles under BjyAuthorize, to see which roles are allowed, and which roles are denied?  How, where do I do this?

*  Aside from the above, there is an article on testing Controllers.   If I want to test my Controller, am I to use that article's way of testing (in addition to any testing above, if that happens to be different)?  The article doesn't go into access models however, so it will be simple testing, not the kind I need.
Reply | Threaded
Open this post in threaded view
|

Re: How to PHPUnit-test a Controller under BjyAuthorize?

Stefano Torresi
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How to PHPUnit-test a Controller under BjyAuthorize?

dennis-fedco
Thanks.

Are there any examples on how I can test my Controller to see how it responds to different roles through BjyAuthorize?

I have written this Controller Test (and it works woo hoo!), but do not pass.

So my questions are:
* how do I authenticate myself in the test, so that my test passes.
* how do I mock the authorization objects (is there an example code, or directions that can get me started)

Reply | Threaded
Open this post in threaded view
|

Re: How to PHPUnit-test a Controller under BjyAuthorize?

sb8244
Hi, I've had success doing this:

$authorizeMock = Mockery::mock('BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider');
$authorizeMock->shouldReceive('getIdentityRoles')->andReturn(array('admin'));

$this->getApplicationServiceLocator()->setAllowOverride(true)
      ->setService('BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider', $authorizeMock);

This lets you mock the identity roles (which is passed to BjyAuthorize to check if you're allowed to be in there). This is obviously a test case I use in my admin controller, but I could change that to "guest" or "user" and do a test that they get a 404 or 302 response code.

Best,
Steve