Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
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 :
[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 )); } ...
[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
Pour info :
[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 .
[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