I have a test file to test services instantiation and i have made a custom menu with KnpMenuBundle. Everything is working expect phpunit who return an error when testing my MenuBuilder.
There is the function who test all services instantiation my test file :
class ServiceAvailabilityTest extends KernelTestCase
{
/**
* @dataProvider getServiceIds
*
* @param $serviceId
*/
public function testServiceInstance($serviceId)
{
static::bootKernel();
static::$kernel->getContainer()->get($serviceId);
}
}
On my MenuBuilder i use authorizationChecker to know if the user is granted or not, like this.
if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
$menu->addChild('sidebar.front.administration', ['route' => 'sonata_admin_redirect'])
->setExtra('translation_domain', 'navigation')
->setAttribute('icon', 'fa fa-eye');
}
When i'm removing all this, tests are ok
$this->authorizationChecker->isGranted('ROLE_ADMIN')
There is the error i get when i run phpunit
1) Tests\ServiceAvailabilityTest::testServiceInstance with data set #423 ('menu.main') Symfony\Component\Security\Core\ExceptionAuthenticationCredentialsNotFoundException: The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL. /code/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authorization/AuthorizationChecker.php:57 /code/src/AppBundle/Menu/MenuBuilder.php:192 /code/src/AppBundle/Menu/MenuBuilder.php:101 /code/app/cache/test/appTestDebugProjectContainer.php:8311 /code/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /code/tests/ServiceAvailabilityTest.php:3
There is my menu services if you have to check them
menu.builder:
class: AppBundle\Menu\MenuBuilder
arguments: [ '@knp_menu.factory', '@doctrine', '@manager.server','@security.authorization_checker', '@request_stack' ]
menu.main:
class: Knp\Menu\MenuItem
factory: [ '@menu.builder', 'createMainMenu' ]
arguments: [ '@request_stack' ]
tags:
- { name: knp_menu.menu, alias: sidebar }
I already search on the internet but they fix this by adding an access control on security.yml like this
- { path: ^/, role: ROLE_USER }
But i don't have any route for a menu.
Does someone already had this phpunit error ? Thanks,
Try this:
/**
* @param string $firewallName
* @param UserInterface $user
* @param array $options
* @param array $server
*/
protected function loginUser($firewallName, UserInterface $user, array $options = array(), array $server = array())
{
$this->client = static::createClient();
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
static::$kernel->getContainer()->get('security.token_storage')->setToken($token);
// <2.8 this may be usefull
//$request = new Request();
//$event = new InteractiveLoginEvent($request, $token);
//static::$kernel->getContainer()->get('event_dispatcher')->dispatch('security.interactive_login', $event);
$session = $this->client->getContainer()->get('session');
$session->set('_security_'.$firewallName, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());
$this->client->getCookieJar()->set($cookie);
}
in your testCase/setUp for example::
static::bootKernel();
$this->loginUser('admin', $testUser);
$this->assertNotFalse(static::$kernel->getContainer()->get('security.authorization_checker')->isGranted('ROLE_ADMIN'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With