Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 02-03-2016 11:37:12

Nirzol
Membre
Date d'inscription: 11-01-2013
Messages: 65

[résolu]ZF2 + zfcRbac + phpunit

Bonjour

Pas vraiment en rapport avec ZF2 mais peut être quelqu'un l'a déjà fait.
Je suis en train de faire mes test unitaire.

dans mon controller :

Code:

[lang=php]
...
public function listAction()
    {
        if (!$this->isGranted('list_action')) {
            throw new \ZfcRbac\Exception\UnauthorizedException('You are not allowed !');
        }
        
        $listActions = $this->actionService->getAll();

        return new ViewModel(array(
            'listActions' => $listActions
        ));
    }
...

Code:

[lang=php]
...
public function testListActionIsAccessible()
    {
$userMock = new \Ent\Entity\EntUser();

        $authService = $this->getMockBuilder(\ZfcRbac\Service\AuthorizationService::class)->disableOriginalConstructor()->getMock();
        $authService->expects($this->once())->method('isGranted')->will($this->returnValue(true));
        
        $this->dispatch('/api/action');

        $this->assertResponseStatusCode(200);
//        $this->assertModuleName('ent');
//        $this->assertControllerName('ent\controller\action');
//        $this->assertActionName('list');
//        $this->assertMatchedRouteName('zfcadmin/action');
    }
...

Quelqu'un a t'il une idée ? A déjà un mock avec zfcRbac ?

Dernière modification par Nirzol (02-03-2016 15:45:21)

Hors ligne

 

#2 02-03-2016 15:44:09

Nirzol
Membre
Date d'inscription: 11-01-2013
Messages: 65

Re: [résolu]ZF2 + zfcRbac + phpunit

Pour info :

Code:

[lang=php]
protected function mockAuthorizationService()
    {
        $this->serviceManager = $this->getApplicationServiceLocator();
        $this->serviceManager->setAllowOverride(true);

        $authService = $this->getMockBuilder(\ZfcRbac\Service\AuthorizationService::class)->disableOriginalConstructor()->getMock();
        $authService->expects($this->any())
                ->method('isGranted')
                ->will($this->returnValue(true));

        $this->serviceManager->setService(\ZfcRbac\Service\AuthorizationService::class, $authService);
    }

    public function testListActionIsAccessible()
    {

        $this->mockAuthorizationService();

        $this->dispatch('/api/action');

        $this->assertResponseStatusCode(200);
        $this->assertModuleName('ent');
        $this->assertControllerName('ent\controller\action');
        $this->assertActionName('list');
        $this->assertMatchedRouteName('zfcadmin/action');
    }

Et pour une version PHPunit_framework_testcase : (et non zend/test)
J'ai commencé à utiliser prophesize au lieu des Mock .

Code:

[lang=php]
    protected function setUp()
    {
        $this->actionDoctrineService = $this->prophesize(\Ent\Service\ActionDoctrineService::class);

        $this->actionForm = $this->prophesize(\Ent\Form\ActionForm::class);

        $controller = new \Ent\Controller\ActionController(
            $this->actionDoctrineService->reveal(),
            $this->actionForm->reveal()
        );
        $this->controller = $controller;

        $this->zfcRbacAuthorizationService = $this->prophesize(\ZfcRbac\Service\AuthorizationService::class);

        $pluginManager = $this->getMock('Zend\Mvc\Controller\PluginManager', array('get'));
        $pluginManager->expects($this->any())
                ->method('get')
                ->will($this->returnCallback(array($this, 'helperMockCallbackPluginManagerGet')));

//        $pluginManager = $this->prophesize(\Zend\Mvc\Controller\PluginManager::class)->addMethodProphecy(array('get'));
//        $pluginManager->get()->willReturn(array($this, 'helperMockCallbackPluginManagerGet'))->shouldBeCalled();

        $this->pluginManager = $pluginManager;

        $controller->setPluginManager($pluginManager);
    }

    public function setUpZfcRbacAuthorizationService($option)
    {
        if (array_key_exists('isGranted', $option)) {
            $return = (is_callable($option['isGranted'])) ? $this->returnCallback($option['isGranted']) : $this->returnValue($option['isGranted']);
            $this->zfcRbacAuthorizationService->isGranted()->willReturn(true)->shouldBeCalledTimes(0);
        }
        $this->pluginManagerPlugins['isGranted'] = $this->zfcRbacAuthorizationService;
        return $this->zfcRbacAuthorizationService;
    }

    public function testListActionIsAccessible()
    {
        /* @var $controller \Ent\Controller\ActionController */
        $controller = $this->controller;

        $this->setUpZfcRbacAuthorizationService(array(
            'isGranted' => true
        ));

        $result = $controller->listAction();

        $this->assertInstanceOf('Zend\View\Model\ViewModel', $result);
    }

    public function helperMockCallbackPluginManagerGet($key)
    {
        if ($key == "flashMessenger" && !array_key_exists($key, $this->pluginManagerPlugins)) {
//             echo "\n\n";
//             echo '$key: ' . $key . "\n";
//             var_dump(array_key_exists($key, $this->pluginManagerPlugins), array_keys($this->pluginManagerPlugins));
//             exit;
        }
        return (array_key_exists($key, $this->pluginManagerPlugins)) ? $this->pluginManagerPlugins[$key] : null;
    }

Dernière modification par Nirzol (07-03-2016 09:38:56)

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages