BjyAuthorize and PHPUnit

579 views Asked by At

I'm starting working on unit tests for a Zend Framework 2 project using ZfcUser + BjyAuthorize.

I'm not a unit test expert, so I found how to mock ZfcUser here.

Now I also have to mock BjyAuthorize. Has anyone managed to do it before?

1

There are 1 answers

1
SmasherHell On

Well it is surely late to respond to this.

I've come to a solution using Mock object (well I don't like using mock...), I could not find a way to initialize properly BjyAuthorize...

Well my test controller extends from AbstractHttpControllerTestCase

in testController::setUp() I made the mock object like that :

    // Creating mock
    $mockBjy = $this->getMock("BjyAuthorize\Service\Authorize", array("isAllowed"), array($this->getApplicationConfig(), $this->getApplication()->getServiceManager()));

    // Bypass auth, force true
    $mockBjy->expects($this->any())
            ->method('isAllowed')
            ->will($this->returnValue(true)); 

    // Overriding BjyAuthorize\Service\Authorize service
    $this->getApplication()
         ->getServiceManager()
         ->setAllowOverride(true)
         ->setService('BjyAuthorize\Service\Authorize', $mockBjy);

I don't like this solution though, I find it really ugly but I can't find out an other way to do it.