cwan
cwan

Reputation: 11

Zend Framework 2: AbstractControllerTestCase and ZfcUser, ZfcRbac

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

Further information:

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'
                )
            )
        )
    )
)

Upvotes: 0

Views: 253

Answers (1)

Sebastian Bergmann
Sebastian Bergmann

Reputation: 8326

https://github.com/ZF-Commons/zfc-rbac/pull/298 is a patch that can help with testing Zend Framework 2 controllers that are protected by zfcrbac route guards.

Upvotes: 1

Related Questions