Skip to content

Instantly share code, notes, and snippets.

@ricbra
Created June 25, 2015 14:00
Show Gist options
  • Save ricbra/f532da538eec7a11f52d to your computer and use it in GitHub Desktop.
Save ricbra/f532da538eec7a11f52d to your computer and use it in GitHub Desktop.
<?php
namespace spec\Wb\PoolBundle\Controller;
use Broadway\CommandHandling\CommandBusInterface;
use Broadway\ReadModel\RepositoryInterface;
use Broadway\UuidGenerator\UuidGeneratorInterface;
use Netvlies\WbBundle\Entity\EntityGroupRepository;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Wb\Bundle\EntityGroupBundle\Namer\EntityGroupNamer;
use Wb\Pool\Model\Pool\PoolRepository;
class PoolControllerSpec extends ObjectBehavior
{
function let(
CommandBusInterface $commandBus,
UuidGeneratorInterface $uuidGenerator,
FormFactory $factory,
\Twig_Environment $twig,
RepositoryInterface $readModelRepository,
PoolRepository $poolRepository,
RouterInterface $router,
RepositoryInterface $memberReadModelRepository,
TokenStorage $securityTokenStorage,
EntityGroupNamer $entityGroupNamer,
AuthorizationCheckerInterface $authorizationChecker,
EntityGroupRepository $entityGroupRepository
) {
$this->beConstructedWith(
$commandBus,
$uuidGenerator,
$factory,
$twig,
$readModelRepository,
$poolRepository,
$router,
$memberReadModelRepository,
$securityTokenStorage,
$entityGroupNamer,
$authorizationChecker,
$entityGroupRepository
);
}
function it_is_initializable()
{
$this->shouldHaveType('Wb\PoolBundle\Controller\PoolController');
}
function its_register_action_should_not_be_accessible_for_role_user(
Request $request,
AuthorizationCheckerInterface $checker
) {
$this->buildTokenStorageWithRoles($checker, [
'ROLE_USER',
], [
'ROLE_POOL_ADMIN',
]);
$this->shouldThrow(new AccessDeniedException())->during('registerAction', [
$request,
'cc12f9d0-9a57-43c5-9ad8-4be630ddc90a',
'Pool Inc'
]);
}
function its_register_action_should_be_accessible_for_role_pool_admin(
Request $request,
AuthorizationCheckerInterface $authorizationChecker
) {
$this->buildTokenStorageWithRoles($authorizationChecker, [
'ROLE_POOL_ADMIN'
], []);
$this->registerAction(
$request,
'cc12f9d0-9a57-43c5-9ad8-4be630ddc90a',
'Pool Inc'
);
}
private function buildTokenStorageWithRoles(
AuthorizationCheckerInterface $authorizationChecker,
array $grantedRoles = [],
array $notGrantedRoles = []
) {
foreach ($grantedRoles as $role) {
$authorizationChecker->isGranted($role)->willReturn(true);
}
foreach ($notGrantedRoles as $role) {
$authorizationChecker->isGranted($role)->willReturn(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment