⚠️ Warning ⚠️
This article was written 4 years and 6 months ago. The informations it contains may be outdated or no longer relevant.
Here is a simple gist for testing all admin menu links possible for a Sylius.
It asserts that all pages respond with a 200 Status code.
Feel free to add it in your test suite, to ensure you never break an admin page 😉
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Tests; | |
use App\Entity\User\AdminUser; | |
use Knp\Menu\MenuItem; | |
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
use Symfony\Component\BrowserKit\Cookie; | |
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | |
/** | |
* @internal | |
*/ | |
final class AdminMenuLinkTest extends WebTestCase | |
{ | |
private KernelBrowser $client; | |
protected function setUp(): void | |
{ | |
$this->client = static::createClient(); | |
} | |
/** | |
* @dataProvider provideMenuLink | |
*/ | |
public function testAccessToMenuLinkInAdmin(string $routeName, array $parameters): void | |
{ | |
/** @var UrlGeneratorInterface $urlGenerator */ | |
$urlGenerator = self::$container->get(UrlGeneratorInterface::class); | |
$url = $urlGenerator->generate($routeName, $parameters); | |
$this->logIn(); | |
$this->client->request('GET', $url); | |
$response = $this->client->getResponse(); | |
self::assertSame(200, $response->getStatusCode(), 'Unable to get successful response on ' . $routeName); | |
} | |
public function provideMenuLink(): \Generator | |
{ | |
static::bootKernel(); | |
/** @var \Sylius\Bundle\AdminBundle\Menu\MainMenuBuilder $menu */ | |
$menu = self::$container->get('sylius.admin.menu_builder.main'); | |
/** @var \Knp\Menu\ItemInterface $menu */ | |
$menu = $menu->createMenu([]); | |
$categories = \iterator_to_array($menu->getIterator()); | |
/** @var \Knp\Menu\MenuItem $category */ | |
foreach ($categories as $category) { | |
if (null !== $category->getUri()) { | |
yield $category->getUri() => $this->getRouteParam($category); | |
} | |
$mainLinks = \iterator_to_array($category->getIterator()); | |
/** @var \Knp\Menu\MenuItem $link */ | |
foreach ($mainLinks as $link) { | |
yield $link->getUri() => $this->getRouteParam($link); | |
} | |
} | |
} | |
private function getRouteParam(MenuItem $item): array | |
{ | |
$routeExtra = $item->getExtra('routes')[0]; | |
return [$routeExtra['route'], $routeExtra['parameters']]; | |
} | |
/** | |
* @see https://symfony.com/doc/4.4/testing/http_authentication.html#creating-the-authentication-token | |
*/ | |
private function logIn(): void | |
{ | |
$session = self::$container->get('session'); | |
/** @var AdminUser $user */ | |
$user = self::$container->get('doctrine') | |
->getRepository(AdminUser::class) | |
->findOneBy(['username' => 'sylius']); | |
$firewallName = 'admin'; | |
$firewallContext = 'admin'; | |
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles()); | |
$session->set('_security_' . $firewallContext, \serialize($token)); | |
$session->save(); | |
$cookie = new Cookie($session->getName(), $session->getId()); | |
$this->client->getCookieJar()->set($cookie); | |
} | |
} |