upgrade menuComposer according to #179 - #217

This commit is contained in:
Julien Fastré 2014-10-06 23:18:56 +02:00
parent e9925a6dd7
commit 96fada19ef
6 changed files with 195 additions and 23 deletions

View File

@ -3,6 +3,7 @@
namespace CL\Chill\MainBundle\DependencyInjection\Services;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* This class permit to build menu from the routing information
@ -12,41 +13,64 @@ use Symfony\Component\Routing\RouterInterface;
*
* @author julien
*/
class MenuComposer {
class MenuComposer
{
/**
*
* @var \Symfony\Component\Routing\RouteCollection;
*/
private $routeCollection;
public function __construct(RouterInterface $router) {
$this->routeCollection = $router->getRouteCollection();
public function __construct(RouterInterface $router)
{
//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();
foreach ($this->routeCollection->all() as $key => $route) {
if ($route->getOption('menu') === $menuId) {
$a['route'] = $key;
$a['label'] = $route->getOption('label');
if ($route->hasOption('helper')) {
$a['helper'] = $route->getOption('helper');
} else {
$a['helper'] = '';
if ($route->hasOption('menus')) {
foreach ($route->getOption('menus') as $key => $params) {
if ($menuId === $key) {
$route = array();
$route['route'] = $key;
$route['label'] = $params['label'];
$route['helper'] =
(isset($params['helper'])) ? $params['helper'] : '';
//add route to the routes array, avoiding duplicate 'order'
// to erase previously added
if (!isset($routes[$params['order']])) {
$routes[$params['order']] = $route;
} else {
$routes[$params['order'] + 1 ] = $route;
}
}
}
$routes[$route->getOption('order')] = $a;
}
}
ksort($routes);
return $routes;
}
}

View File

@ -4,6 +4,11 @@ root:
_controller: FrameworkBundle:Redirect:urlRedirect
path: /hello
permanent: true
options:
menus:
main:
order: 10
label: homepage
cl_chill_main_homepage:
pattern: /hello
@ -12,3 +17,30 @@ cl_chill_main_homepage:
chill_main_admin_central:
pattern: /admin
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'

View 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);
}
}

View 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
View 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
View 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>