mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
parent
80dee1e7e7
commit
ca2766bb38
@ -3,6 +3,7 @@
|
|||||||
namespace CL\Chill\MainBundle\DependencyInjection\Services;
|
namespace CL\Chill\MainBundle\DependencyInjection\Services;
|
||||||
|
|
||||||
use Symfony\Component\Routing\RouterInterface;
|
use Symfony\Component\Routing\RouterInterface;
|
||||||
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class permit to build menu from the routing information
|
* This class permit to build menu from the routing information
|
||||||
@ -12,7 +13,8 @@ use Symfony\Component\Routing\RouterInterface;
|
|||||||
*
|
*
|
||||||
* @author julien
|
* @author julien
|
||||||
*/
|
*/
|
||||||
class MenuComposer {
|
class MenuComposer
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -20,33 +22,55 @@ class MenuComposer {
|
|||||||
*/
|
*/
|
||||||
private $routeCollection;
|
private $routeCollection;
|
||||||
|
|
||||||
|
public function __construct(RouterInterface $router)
|
||||||
public function __construct(RouterInterface $router) {
|
{
|
||||||
$this->routeCollection = $router->getRouteCollection();
|
//see remark in MenuComposer::setRouteCollection
|
||||||
|
$this->setRouteCollection($router->getRouteCollection());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRoutesFor($menuId, array $parameters = array()) {
|
/**
|
||||||
|
* Set the route Collection
|
||||||
|
* This function is needed for testing purpose: routeCollection is not
|
||||||
|
* available as a service (RouterInterface is provided as a service and
|
||||||
|
* added to this class as paramater in __construct)
|
||||||
|
*
|
||||||
|
* @param RouteCollection $routeCollection
|
||||||
|
*/
|
||||||
|
public function setRouteCollection(RouteCollection $routeCollection)
|
||||||
|
{
|
||||||
|
$this->routeCollection = $routeCollection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRoutesFor($menuId, array $parameters = array())
|
||||||
|
{
|
||||||
$routes = array();
|
$routes = array();
|
||||||
|
|
||||||
foreach ($this->routeCollection->all() as $key => $route) {
|
foreach ($this->routeCollection->all() as $key => $route) {
|
||||||
if ($route->getOption('menu') === $menuId) {
|
if ($route->hasOption('menus')) {
|
||||||
$a['route'] = $key;
|
foreach ($route->getOption('menus') as $key => $params) {
|
||||||
$a['label'] = $route->getOption('label');
|
if ($menuId === $key) {
|
||||||
|
$route = array();
|
||||||
|
$route['route'] = $key;
|
||||||
|
$route['label'] = $params['label'];
|
||||||
|
$route['helper'] =
|
||||||
|
(isset($params['helper'])) ? $params['helper'] : '';
|
||||||
|
|
||||||
if ($route->hasOption('helper')) {
|
//add route to the routes array, avoiding duplicate 'order'
|
||||||
$a['helper'] = $route->getOption('helper');
|
// to erase previously added
|
||||||
|
if (!isset($routes[$params['order']])) {
|
||||||
|
$routes[$params['order']] = $route;
|
||||||
} else {
|
} else {
|
||||||
$a['helper'] = '';
|
$routes[$params['order'] + 1 ] = $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
$routes[$route->getOption('order')] = $a;
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ksort($routes);
|
ksort($routes);
|
||||||
|
|
||||||
return $routes;
|
return $routes;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,11 @@ root:
|
|||||||
_controller: FrameworkBundle:Redirect:urlRedirect
|
_controller: FrameworkBundle:Redirect:urlRedirect
|
||||||
path: /hello
|
path: /hello
|
||||||
permanent: true
|
permanent: true
|
||||||
|
options:
|
||||||
|
menus:
|
||||||
|
main:
|
||||||
|
order: 10
|
||||||
|
label: homepage
|
||||||
|
|
||||||
cl_chill_main_homepage:
|
cl_chill_main_homepage:
|
||||||
pattern: /hello
|
pattern: /hello
|
||||||
@ -12,3 +17,30 @@ cl_chill_main_homepage:
|
|||||||
chill_main_admin_central:
|
chill_main_admin_central:
|
||||||
pattern: /admin
|
pattern: /admin
|
||||||
defaults: { _controller: CLChillMainBundle:Admin:index }
|
defaults: { _controller: CLChillMainBundle:Admin:index }
|
||||||
|
options:
|
||||||
|
menus:
|
||||||
|
main:
|
||||||
|
order: 20
|
||||||
|
label: homepage
|
||||||
|
|
||||||
|
|
||||||
|
chill_main_dummy_0:
|
||||||
|
pattern: /dummy
|
||||||
|
defaults: { _controller: CLChillMainBundle:Default:index }
|
||||||
|
options:
|
||||||
|
menus:
|
||||||
|
dummy0:
|
||||||
|
order: 50
|
||||||
|
label: 'test dummy 0'
|
||||||
|
dummy1:
|
||||||
|
order: 50
|
||||||
|
label: test dummy 1
|
||||||
|
|
||||||
|
chill_main_dummy_1:
|
||||||
|
pattern: /dummy1
|
||||||
|
defaults: { _controller: CLChillMainBundle:Default:index }
|
||||||
|
options:
|
||||||
|
menus:
|
||||||
|
dummy0:
|
||||||
|
order: 51
|
||||||
|
label: 'test dummy 1'
|
||||||
|
68
Tests/Services/MenuComposer.php
Normal file
68
Tests/Services/MenuComposer.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace CL\Chill\MainBundle\Tests\Services;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||||
|
use Symfony\Component\Routing\Loader\YamlFileLoader;
|
||||||
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provide functional test for MenuComposer
|
||||||
|
*
|
||||||
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*/
|
||||||
|
class MenuComposer extends KernelTestCase
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var \Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader;
|
||||||
|
*/
|
||||||
|
private $loader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var \CL\Chill\MainBundle\DependencyInjection\Services\MenuComposer;
|
||||||
|
*/
|
||||||
|
private $menuComposer;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
self::bootKernel();
|
||||||
|
$this->loader = static::$kernel->getContainer()
|
||||||
|
->get('routing.loader');
|
||||||
|
$this->menuComposer = static::$kernel->getContainer()
|
||||||
|
->get('chill_main.menu_composer');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMenuComposer()
|
||||||
|
{
|
||||||
|
$collection = new RouteCollection();
|
||||||
|
$collection->add($this->loader->load(__DIR__.'dummy_menu_composer.yml', 'yaml'));
|
||||||
|
|
||||||
|
$routes = $this->menuComposer->getRoutesFor('dummy0');
|
||||||
|
|
||||||
|
$this->assertInternalType('array', $routes);
|
||||||
|
$this->assertCount(2, $routes);
|
||||||
|
//check that the keys are sorted
|
||||||
|
$orders = array_keys($routes);
|
||||||
|
foreach ($orders as $key => $order){
|
||||||
|
if (array_key_exists($key + 1, $orders)) {
|
||||||
|
$this->assertGreaterThan($order, $orders[$key + 1],
|
||||||
|
'Failing to assert that routes are ordered');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->assertSame(array(
|
||||||
|
50 => array(
|
||||||
|
'key' => 'chill_main_dummy_0',
|
||||||
|
'label' => 'test0',
|
||||||
|
),
|
||||||
|
51 => array(
|
||||||
|
'key' => 'chill_main_dummy_1',
|
||||||
|
'label' => 'test1',
|
||||||
|
'helper'=> 'great helper'
|
||||||
|
)
|
||||||
|
|
||||||
|
), $routes);
|
||||||
|
}
|
||||||
|
}
|
21
Tests/Services/dummy_menu_composer.yml
Normal file
21
Tests/Services/dummy_menu_composer.yml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
chill_main_dummy_0:
|
||||||
|
pattern: /dummy
|
||||||
|
defaults: { _controller: CLChillMainBundle:Default:index }
|
||||||
|
options:
|
||||||
|
menus:
|
||||||
|
dummy0:
|
||||||
|
order: 50
|
||||||
|
label: 'test0'
|
||||||
|
dummy1:
|
||||||
|
order: 50
|
||||||
|
label: test dummy 1
|
||||||
|
|
||||||
|
chill_main_dummy_1:
|
||||||
|
pattern: /dummy1
|
||||||
|
defaults: { _controller: CLChillMainBundle:Default:index }
|
||||||
|
options:
|
||||||
|
menus:
|
||||||
|
dummy0:
|
||||||
|
order: 50
|
||||||
|
label: 'test1'
|
||||||
|
helper: 'great helper'
|
8
Tests/bootstrap.php
Normal file
8
Tests/bootstrap.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) {
|
||||||
|
throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?');
|
||||||
|
}
|
||||||
|
|
||||||
|
require $autoloadFile;
|
||||||
|
|
19
phpunit.xml.dist
Normal file
19
phpunit.xml.dist
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="ChillMain test suite">
|
||||||
|
<directory suffix="Test.php">./Tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
<filter>
|
||||||
|
<whitelist>
|
||||||
|
<directory>./</directory>
|
||||||
|
<exclude>
|
||||||
|
<directory>./Resources</directory>
|
||||||
|
<directory>./Tests</directory>
|
||||||
|
<directory>./vendor</directory>
|
||||||
|
</exclude>
|
||||||
|
</whitelist>
|
||||||
|
</filter>
|
||||||
|
</phpunit>
|
Loading…
x
Reference in New Issue
Block a user