How do I use PHPUnit to test a Controller under ZFC RBAC
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am pretty new to unit testing and have just started my journey learning how to use tests to make my applications more reliable.
I am using Zend Framework 3 and am following this guide https://docs.zendframework.com/tutorials/unit-testing/
What I want to do is test a route that requires a user to be Authenticated and have the correct ZFR Rbac role.
public function testOverviewActionCanBeAccessed()
{
//Setup a mock user
$user = $this->createMock(User::class);
$user->method('getRoles')->willReturn(['admin']);
//Setup the mock auth identity interface
$identity = $this->createMock('ZendAuthenticationAuthenticationService');
$identity->method('getIdentity')
->willReturn($user);
//Run the following test
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(AgentController::class);
$this->assertControllerClass('AgentController');
$this->assertMatchedRouteName('cp/overview');
}
At the moment when I run the test I get the following error:
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 1.27 seconds, Memory: 16.00MB
There was 1 failure:
1) ControlPanelTestControllerAgentControllerTest::testOverviewActionCanBeAccessed
Failed asserting response code "200", actual status code is "302"
Exceptions raised:
Exception 'ZfcRbacExceptionUnauthorizedException' with message 'You are not authorized to access this resource' in /var/www//public_html/application/vendor/zf-commons/zfc-rbac/src/ZfcRbac/Guard/AbstractGuard.php:66
/var/www//public_html/application/vendor/zendframework/zend-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:482
/var/www/public_html/application/module/ControlPanel/test/Controller/AgentControllerTest.php:40
So my question is how do I setup RBAC in the test?
zend-framework3 phpunit-testing zend-test zfc-rbac
add a comment |
I am pretty new to unit testing and have just started my journey learning how to use tests to make my applications more reliable.
I am using Zend Framework 3 and am following this guide https://docs.zendframework.com/tutorials/unit-testing/
What I want to do is test a route that requires a user to be Authenticated and have the correct ZFR Rbac role.
public function testOverviewActionCanBeAccessed()
{
//Setup a mock user
$user = $this->createMock(User::class);
$user->method('getRoles')->willReturn(['admin']);
//Setup the mock auth identity interface
$identity = $this->createMock('ZendAuthenticationAuthenticationService');
$identity->method('getIdentity')
->willReturn($user);
//Run the following test
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(AgentController::class);
$this->assertControllerClass('AgentController');
$this->assertMatchedRouteName('cp/overview');
}
At the moment when I run the test I get the following error:
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 1.27 seconds, Memory: 16.00MB
There was 1 failure:
1) ControlPanelTestControllerAgentControllerTest::testOverviewActionCanBeAccessed
Failed asserting response code "200", actual status code is "302"
Exceptions raised:
Exception 'ZfcRbacExceptionUnauthorizedException' with message 'You are not authorized to access this resource' in /var/www//public_html/application/vendor/zf-commons/zfc-rbac/src/ZfcRbac/Guard/AbstractGuard.php:66
/var/www//public_html/application/vendor/zendframework/zend-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:482
/var/www/public_html/application/module/ControlPanel/test/Controller/AgentControllerTest.php:40
So my question is how do I setup RBAC in the test?
zend-framework3 phpunit-testing zend-test zfc-rbac
add a comment |
I am pretty new to unit testing and have just started my journey learning how to use tests to make my applications more reliable.
I am using Zend Framework 3 and am following this guide https://docs.zendframework.com/tutorials/unit-testing/
What I want to do is test a route that requires a user to be Authenticated and have the correct ZFR Rbac role.
public function testOverviewActionCanBeAccessed()
{
//Setup a mock user
$user = $this->createMock(User::class);
$user->method('getRoles')->willReturn(['admin']);
//Setup the mock auth identity interface
$identity = $this->createMock('ZendAuthenticationAuthenticationService');
$identity->method('getIdentity')
->willReturn($user);
//Run the following test
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(AgentController::class);
$this->assertControllerClass('AgentController');
$this->assertMatchedRouteName('cp/overview');
}
At the moment when I run the test I get the following error:
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 1.27 seconds, Memory: 16.00MB
There was 1 failure:
1) ControlPanelTestControllerAgentControllerTest::testOverviewActionCanBeAccessed
Failed asserting response code "200", actual status code is "302"
Exceptions raised:
Exception 'ZfcRbacExceptionUnauthorizedException' with message 'You are not authorized to access this resource' in /var/www//public_html/application/vendor/zf-commons/zfc-rbac/src/ZfcRbac/Guard/AbstractGuard.php:66
/var/www//public_html/application/vendor/zendframework/zend-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:482
/var/www/public_html/application/module/ControlPanel/test/Controller/AgentControllerTest.php:40
So my question is how do I setup RBAC in the test?
zend-framework3 phpunit-testing zend-test zfc-rbac
I am pretty new to unit testing and have just started my journey learning how to use tests to make my applications more reliable.
I am using Zend Framework 3 and am following this guide https://docs.zendframework.com/tutorials/unit-testing/
What I want to do is test a route that requires a user to be Authenticated and have the correct ZFR Rbac role.
public function testOverviewActionCanBeAccessed()
{
//Setup a mock user
$user = $this->createMock(User::class);
$user->method('getRoles')->willReturn(['admin']);
//Setup the mock auth identity interface
$identity = $this->createMock('ZendAuthenticationAuthenticationService');
$identity->method('getIdentity')
->willReturn($user);
//Run the following test
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(AgentController::class);
$this->assertControllerClass('AgentController');
$this->assertMatchedRouteName('cp/overview');
}
At the moment when I run the test I get the following error:
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 1.27 seconds, Memory: 16.00MB
There was 1 failure:
1) ControlPanelTestControllerAgentControllerTest::testOverviewActionCanBeAccessed
Failed asserting response code "200", actual status code is "302"
Exceptions raised:
Exception 'ZfcRbacExceptionUnauthorizedException' with message 'You are not authorized to access this resource' in /var/www//public_html/application/vendor/zf-commons/zfc-rbac/src/ZfcRbac/Guard/AbstractGuard.php:66
/var/www//public_html/application/vendor/zendframework/zend-test/src/PHPUnit/Controller/AbstractControllerTestCase.php:482
/var/www/public_html/application/module/ControlPanel/test/Controller/AgentControllerTest.php:40
So my question is how do I setup RBAC in the test?
zend-framework3 phpunit-testing zend-test zfc-rbac
zend-framework3 phpunit-testing zend-test zfc-rbac
edited Jan 4 at 11:45
HappyCoder
asked Jan 3 at 20:22
HappyCoderHappyCoder
3,02833053
3,02833053
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
So this is how I have solved this issue.
This suggestion helped solve the issue
My working code:
<?php
namespace ControlPanelTestController;
use ControlPanelControllerControlPanelController;
use ZendStdlibArrayUtils;
use ZendTestPHPUnitControllerAbstractHttpControllerTestCase;
use ZfcRbacIdentityIdentityInterface;
use ZfcRbacIdentityIdentityProviderInterface;
use ZfcRbacServiceRoleService;
class AgentControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
protected $guard;
public function setUp()
{
$configOverrides = ;
$this->setApplicationConfig(ArrayUtils::merge(
// Grabbing the full application configuration:
include __DIR__ . '/../../../../config/application.config.php',
$configOverrides
));
parent::setUp();
}
public function rbacGuards($roles)
{
/**
* Deal with Rbac Guards
*/
$roleService = $this->getApplicationServiceLocator()->get(RoleService::class);
$identityProvider = $this->prophesize(IdentityProviderInterface::class);
$identity = $this->prophesize(IdentityInterface::class);
// Here you use the setter to inject your mocked identity provider
$roleService->setIdentityProvider($identityProvider->reveal());
$identityProvider->getIdentity()->shouldBeCalled()->willReturn($identity->reveal());
$identity->getRoles()->shouldBeCalled()->willReturn($roles);
}
public function testModuleActionsCanBeAccessed()
{
$this->rbacGuards(['admin']);
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(ControlPanelController::class);
$this->assertControllerClass('ControlPanelController');
$this->assertMatchedRouteName('cp/overview');
}
}
Hope this helps someone running unit tests and needing to set the underlying zfc rbac role.
1
thanks a lot, this helped us! This and changing the auth storage to not use sessions via a custom implementation and config
– nofreeusername
Feb 7 at 11:12
Glad it helped, was a bit of a pain for us...
– HappyCoder
Feb 8 at 20:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029276%2fhow-do-i-use-phpunit-to-test-a-controller-under-zfc-rbac%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So this is how I have solved this issue.
This suggestion helped solve the issue
My working code:
<?php
namespace ControlPanelTestController;
use ControlPanelControllerControlPanelController;
use ZendStdlibArrayUtils;
use ZendTestPHPUnitControllerAbstractHttpControllerTestCase;
use ZfcRbacIdentityIdentityInterface;
use ZfcRbacIdentityIdentityProviderInterface;
use ZfcRbacServiceRoleService;
class AgentControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
protected $guard;
public function setUp()
{
$configOverrides = ;
$this->setApplicationConfig(ArrayUtils::merge(
// Grabbing the full application configuration:
include __DIR__ . '/../../../../config/application.config.php',
$configOverrides
));
parent::setUp();
}
public function rbacGuards($roles)
{
/**
* Deal with Rbac Guards
*/
$roleService = $this->getApplicationServiceLocator()->get(RoleService::class);
$identityProvider = $this->prophesize(IdentityProviderInterface::class);
$identity = $this->prophesize(IdentityInterface::class);
// Here you use the setter to inject your mocked identity provider
$roleService->setIdentityProvider($identityProvider->reveal());
$identityProvider->getIdentity()->shouldBeCalled()->willReturn($identity->reveal());
$identity->getRoles()->shouldBeCalled()->willReturn($roles);
}
public function testModuleActionsCanBeAccessed()
{
$this->rbacGuards(['admin']);
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(ControlPanelController::class);
$this->assertControllerClass('ControlPanelController');
$this->assertMatchedRouteName('cp/overview');
}
}
Hope this helps someone running unit tests and needing to set the underlying zfc rbac role.
1
thanks a lot, this helped us! This and changing the auth storage to not use sessions via a custom implementation and config
– nofreeusername
Feb 7 at 11:12
Glad it helped, was a bit of a pain for us...
– HappyCoder
Feb 8 at 20:24
add a comment |
So this is how I have solved this issue.
This suggestion helped solve the issue
My working code:
<?php
namespace ControlPanelTestController;
use ControlPanelControllerControlPanelController;
use ZendStdlibArrayUtils;
use ZendTestPHPUnitControllerAbstractHttpControllerTestCase;
use ZfcRbacIdentityIdentityInterface;
use ZfcRbacIdentityIdentityProviderInterface;
use ZfcRbacServiceRoleService;
class AgentControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
protected $guard;
public function setUp()
{
$configOverrides = ;
$this->setApplicationConfig(ArrayUtils::merge(
// Grabbing the full application configuration:
include __DIR__ . '/../../../../config/application.config.php',
$configOverrides
));
parent::setUp();
}
public function rbacGuards($roles)
{
/**
* Deal with Rbac Guards
*/
$roleService = $this->getApplicationServiceLocator()->get(RoleService::class);
$identityProvider = $this->prophesize(IdentityProviderInterface::class);
$identity = $this->prophesize(IdentityInterface::class);
// Here you use the setter to inject your mocked identity provider
$roleService->setIdentityProvider($identityProvider->reveal());
$identityProvider->getIdentity()->shouldBeCalled()->willReturn($identity->reveal());
$identity->getRoles()->shouldBeCalled()->willReturn($roles);
}
public function testModuleActionsCanBeAccessed()
{
$this->rbacGuards(['admin']);
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(ControlPanelController::class);
$this->assertControllerClass('ControlPanelController');
$this->assertMatchedRouteName('cp/overview');
}
}
Hope this helps someone running unit tests and needing to set the underlying zfc rbac role.
1
thanks a lot, this helped us! This and changing the auth storage to not use sessions via a custom implementation and config
– nofreeusername
Feb 7 at 11:12
Glad it helped, was a bit of a pain for us...
– HappyCoder
Feb 8 at 20:24
add a comment |
So this is how I have solved this issue.
This suggestion helped solve the issue
My working code:
<?php
namespace ControlPanelTestController;
use ControlPanelControllerControlPanelController;
use ZendStdlibArrayUtils;
use ZendTestPHPUnitControllerAbstractHttpControllerTestCase;
use ZfcRbacIdentityIdentityInterface;
use ZfcRbacIdentityIdentityProviderInterface;
use ZfcRbacServiceRoleService;
class AgentControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
protected $guard;
public function setUp()
{
$configOverrides = ;
$this->setApplicationConfig(ArrayUtils::merge(
// Grabbing the full application configuration:
include __DIR__ . '/../../../../config/application.config.php',
$configOverrides
));
parent::setUp();
}
public function rbacGuards($roles)
{
/**
* Deal with Rbac Guards
*/
$roleService = $this->getApplicationServiceLocator()->get(RoleService::class);
$identityProvider = $this->prophesize(IdentityProviderInterface::class);
$identity = $this->prophesize(IdentityInterface::class);
// Here you use the setter to inject your mocked identity provider
$roleService->setIdentityProvider($identityProvider->reveal());
$identityProvider->getIdentity()->shouldBeCalled()->willReturn($identity->reveal());
$identity->getRoles()->shouldBeCalled()->willReturn($roles);
}
public function testModuleActionsCanBeAccessed()
{
$this->rbacGuards(['admin']);
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(ControlPanelController::class);
$this->assertControllerClass('ControlPanelController');
$this->assertMatchedRouteName('cp/overview');
}
}
Hope this helps someone running unit tests and needing to set the underlying zfc rbac role.
So this is how I have solved this issue.
This suggestion helped solve the issue
My working code:
<?php
namespace ControlPanelTestController;
use ControlPanelControllerControlPanelController;
use ZendStdlibArrayUtils;
use ZendTestPHPUnitControllerAbstractHttpControllerTestCase;
use ZfcRbacIdentityIdentityInterface;
use ZfcRbacIdentityIdentityProviderInterface;
use ZfcRbacServiceRoleService;
class AgentControllerTest extends AbstractHttpControllerTestCase
{
protected $traceError = true;
protected $guard;
public function setUp()
{
$configOverrides = ;
$this->setApplicationConfig(ArrayUtils::merge(
// Grabbing the full application configuration:
include __DIR__ . '/../../../../config/application.config.php',
$configOverrides
));
parent::setUp();
}
public function rbacGuards($roles)
{
/**
* Deal with Rbac Guards
*/
$roleService = $this->getApplicationServiceLocator()->get(RoleService::class);
$identityProvider = $this->prophesize(IdentityProviderInterface::class);
$identity = $this->prophesize(IdentityInterface::class);
// Here you use the setter to inject your mocked identity provider
$roleService->setIdentityProvider($identityProvider->reveal());
$identityProvider->getIdentity()->shouldBeCalled()->willReturn($identity->reveal());
$identity->getRoles()->shouldBeCalled()->willReturn($roles);
}
public function testModuleActionsCanBeAccessed()
{
$this->rbacGuards(['admin']);
$this->dispatch('/cp/overview');
$this->assertResponseStatusCode(200);
$this->assertModuleName('ControlPanel');
$this->assertControllerName(ControlPanelController::class);
$this->assertControllerClass('ControlPanelController');
$this->assertMatchedRouteName('cp/overview');
}
}
Hope this helps someone running unit tests and needing to set the underlying zfc rbac role.
answered Jan 4 at 16:26
HappyCoderHappyCoder
3,02833053
3,02833053
1
thanks a lot, this helped us! This and changing the auth storage to not use sessions via a custom implementation and config
– nofreeusername
Feb 7 at 11:12
Glad it helped, was a bit of a pain for us...
– HappyCoder
Feb 8 at 20:24
add a comment |
1
thanks a lot, this helped us! This and changing the auth storage to not use sessions via a custom implementation and config
– nofreeusername
Feb 7 at 11:12
Glad it helped, was a bit of a pain for us...
– HappyCoder
Feb 8 at 20:24
1
1
thanks a lot, this helped us! This and changing the auth storage to not use sessions via a custom implementation and config
– nofreeusername
Feb 7 at 11:12
thanks a lot, this helped us! This and changing the auth storage to not use sessions via a custom implementation and config
– nofreeusername
Feb 7 at 11:12
Glad it helped, was a bit of a pain for us...
– HappyCoder
Feb 8 at 20:24
Glad it helped, was a bit of a pain for us...
– HappyCoder
Feb 8 at 20:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029276%2fhow-do-i-use-phpunit-to-test-a-controller-under-zfc-rbac%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown