mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-30 10:29:42 +00:00
Refactor MenuComposer
to improve type safety and simplify local menu builder integration
This commit is contained in:
@@ -11,44 +11,107 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Tests\Services;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
|
||||
use Chill\MainBundle\Routing\MenuComposer;
|
||||
use Knp\Menu\MenuFactory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* This class provide functional test for MenuComposer.
|
||||
* Tests for MenuComposer methods.
|
||||
*
|
||||
* We only verify that items provided by local menu builders are present
|
||||
* when getRoutesFor() yields no routes, and that hasLocalMenuBuilder behaves
|
||||
* as expected with the configured builders.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @coversNothing
|
||||
*/
|
||||
final class MenuComposerTest extends KernelTestCase
|
||||
final class MenuComposerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader;
|
||||
*/
|
||||
private $loader;
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @var \Chill\MainBundle\DependencyInjection\Services\MenuComposer;
|
||||
*/
|
||||
private $menuComposer;
|
||||
|
||||
protected function setUp(): void
|
||||
private function buildMenuComposerWithDefaultBuilder(): array
|
||||
{
|
||||
self::bootKernel(['environment' => 'test']);
|
||||
$this->menuComposer = self::getContainer()
|
||||
->get('chill.main.menu_composer');
|
||||
// Router: returns an empty RouteCollection so getRoutesFor() yields []
|
||||
$routerProphecy = $this->prophesize(RouterInterface::class);
|
||||
$routerProphecy->getRouteCollection()->willReturn(new RouteCollection());
|
||||
$router = $routerProphecy->reveal();
|
||||
|
||||
// Menu factory from Knp\Menu
|
||||
$menuFactory = new MenuFactory();
|
||||
|
||||
// Translator: identity translator
|
||||
$translator = new class () implements TranslatorInterface {
|
||||
public function trans(string $id, array $parameters = [], ?string $domain = null, ?string $locale = null): string
|
||||
{
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function getLocale(): string
|
||||
{
|
||||
return 'en';
|
||||
}
|
||||
};
|
||||
|
||||
// Local builder that adds two items to the requested menu
|
||||
$builder = new class () implements LocalMenuBuilderInterface {
|
||||
public static function getMenuIds(): array
|
||||
{
|
||||
return ['main'];
|
||||
}
|
||||
|
||||
public function buildMenu($menuId, \Knp\Menu\MenuItem $menu, array $parameters)
|
||||
{
|
||||
// Ensure we can use parameters passed to getMenuFor
|
||||
$suffix = $parameters['suffix'] ?? '';
|
||||
$menu->addChild('local_item_one', [
|
||||
'label' => 'Local Item One'.$suffix,
|
||||
])->setExtras(['order' => 1]);
|
||||
$menu->addChild('local_item_two', [
|
||||
'label' => 'Local Item Two'.$suffix,
|
||||
])->setExtras(['order' => 2]);
|
||||
}
|
||||
};
|
||||
|
||||
$composer = new MenuComposer(
|
||||
$router,
|
||||
$menuFactory,
|
||||
$translator,
|
||||
[$builder]
|
||||
);
|
||||
|
||||
return [$composer, $builder];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Chill\MainBundle\Routing\MenuComposer
|
||||
*/
|
||||
public function testMenuComposer()
|
||||
public function testGetMenuForReturnsItemsFromLocalBuildersOnly(): void
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
[$composer] = $this->buildMenuComposerWithDefaultBuilder();
|
||||
|
||||
$routes = $this->menuComposer->getRoutesFor('dummy0');
|
||||
$menu = $composer->getMenuFor('main', []);
|
||||
|
||||
$this->assertIsArray($routes);
|
||||
// No routes were added, only local builder items should be present
|
||||
$children = $menu->getChildren();
|
||||
self::assertCount(2, $children, 'Menu should contain exactly the items provided by local builders');
|
||||
|
||||
// Assert the two expected items exist with their names
|
||||
self::assertNotNull($menu->getChild('local_item_one'));
|
||||
self::assertNotNull($menu->getChild('local_item_two'));
|
||||
|
||||
// And that their labels include the parameter suffix
|
||||
self::assertSame('Local Item One', $menu->getChild('local_item_one')->getLabel());
|
||||
self::assertSame('Local Item Two', $menu->getChild('local_item_two')->getLabel());
|
||||
}
|
||||
|
||||
public function testHasLocalMenuBuilder(): void
|
||||
{
|
||||
[$composer] = $this->buildMenuComposerWithDefaultBuilder();
|
||||
|
||||
self::assertTrue($composer->hasLocalMenuBuilder('main'));
|
||||
self::assertFalse($composer->hasLocalMenuBuilder('secondary'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user