lundi 30 mars 2015

Zend Framework 2: AbstractControllerTestCase and ZfcUser, ZfcRbac


Vote count:

0




at the moment we are testing a ZF2 application with PHPUnit. In this Application we use the modules ZFCUser and ZFCRbac.


We have a backend, protected with ZfcRbac-RouteGuards. We also want to test the "backend"-Action in our Controller with a logged in user. Is there a possibility to mock an user in an AbstractControllerTestCase?


Kind regards,


Cwan


Controller:



<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}

public function backendAction()
{
return new ViewModel();
}
}


ControllerTest:



<?php
namespace ApplicationTest\Controller;

use Zend\Test\PHPUnit\Controller\AbstractControllerTestCase;

class IndexControllerZendTest extends AbstractControllerTestCase
{
public function setUp()
{
$this->setApplicationConfig(include __DIR__ .'/../../../../../config/application.config.php');
parent::setUp();
}

public function testIndexAction()
{
$this->dispatch('/');

$this->assertResponseStatusCode(200);
$this->assertModuleName('Application');
$this->assertControllerName('Application\Controller\IndexController');
$this->assertControllerClass('IndexController');
$this->assertActionName('index');
$this->assertMatchedRouteName('home');
}

public function testBackendAction()
{
$this->dispatch('/backend');

$this->assertResponseStatusCode(403);
$this->assertModuleName('Application');
$this->assertControllerName('Application\Controller\IndexController');
$this->assertControllerClass('IndexController');
$this->assertActionName('backend');
$this->assertMatchedRouteName('backend');
}
}


Guards:



'guards' => [
'ZfcRbac\Guard\RouteGuard' => [
'home' => ['*'],
'backend' => ['admin'],
],
],


Routes:



'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'index'
)
)
),
'backend' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/backend',
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'backend'
)
)
)
)
)


asked 1 min ago







Zend Framework 2: AbstractControllerTestCase and ZfcUser, ZfcRbac

Aucun commentaire:

Enregistrer un commentaire