From 31779fec4eb840b8f148de9c9c2839c0018c7b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 7 Oct 2014 00:24:50 +0200 Subject: [PATCH] create tests (with app/kernel fixtures) for menu composer #217 --- .gitignore | 2 + DependencyInjection/Services/MenuComposer.php | 10 ++--- Tests/Fixtures/App/AppKernel.php | 39 +++++++++++++++++++ Tests/Fixtures/App/config/config.yml | 11 ++++++ Tests/Fixtures/App/config/config_test.yml | 7 ++++ Tests/Fixtures/App/config/routing.yml | 24 ++++++++++++ ...{MenuComposer.php => MenuComposerTest.php} | 13 ++++--- composer.json | 6 ++- phpunit.xml.dist | 3 ++ 9 files changed, 102 insertions(+), 13 deletions(-) create mode 100644 Tests/Fixtures/App/AppKernel.php create mode 100644 Tests/Fixtures/App/config/config.yml create mode 100644 Tests/Fixtures/App/config/config_test.yml create mode 100644 Tests/Fixtures/App/config/routing.yml rename Tests/Services/{MenuComposer.php => MenuComposerTest.php} (81%) diff --git a/.gitignore b/.gitignore index 132d96bac..5e609fec5 100644 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,7 @@ web/bundles/* app/config/parameters.ini app/config/parameters.yml +#composer +composer.lock #sass-cache Resources/assets/gumpy/.sass-cache diff --git a/DependencyInjection/Services/MenuComposer.php b/DependencyInjection/Services/MenuComposer.php index 8b50b9eac..224a29083 100644 --- a/DependencyInjection/Services/MenuComposer.php +++ b/DependencyInjection/Services/MenuComposer.php @@ -45,15 +45,15 @@ class MenuComposer { $routes = array(); - foreach ($this->routeCollection->all() as $key => $route) { + foreach ($this->routeCollection->all() as $routeKey => $route) { if ($route->hasOption('menus')) { - foreach ($route->getOption('menus') as $key => $params) { - if ($menuId === $key) { + foreach ($route->getOption('menus') as $menuKey => $params) { + if ($menuId === $menuKey) { $route = array(); - $route['route'] = $key; + $route['route'] = $routeKey; $route['label'] = $params['label']; $route['helper'] = - (isset($params['helper'])) ? $params['helper'] : ''; + (isset($params['helper'])) ? $params['helper'] : null; //add route to the routes array, avoiding duplicate 'order' // to erase previously added diff --git a/Tests/Fixtures/App/AppKernel.php b/Tests/Fixtures/App/AppKernel.php new file mode 100644 index 000000000..60c98921f --- /dev/null +++ b/Tests/Fixtures/App/AppKernel.php @@ -0,0 +1,39 @@ +load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml'); + } + + /** + * @return string + */ + public function getCacheDir() + { + return sys_get_temp_dir().'/AcmeHelloBundle/cache'; + } + + /** + * @return string + */ + public function getLogDir() + { + return sys_get_temp_dir().'/AcmeHelloBundle/logs'; + } +} diff --git a/Tests/Fixtures/App/config/config.yml b/Tests/Fixtures/App/config/config.yml new file mode 100644 index 000000000..9522a26e4 --- /dev/null +++ b/Tests/Fixtures/App/config/config.yml @@ -0,0 +1,11 @@ +framework: + secret: Not very secret + router: { resource: "%kernel.root_dir%/config/routing.yml" } + form: true + csrf_protection: true + session: ~ + default_locale: fr + translator: { fallback: fr } + profiler: { only_exceptions: false } + templating: + engines: ['twig'] \ No newline at end of file diff --git a/Tests/Fixtures/App/config/config_test.yml b/Tests/Fixtures/App/config/config_test.yml new file mode 100644 index 000000000..a7d7c2a4e --- /dev/null +++ b/Tests/Fixtures/App/config/config_test.yml @@ -0,0 +1,7 @@ +imports: + - { resource: config.yml } + +framework: + test: ~ + session: + storage_id: session.storage.filesystem \ No newline at end of file diff --git a/Tests/Fixtures/App/config/routing.yml b/Tests/Fixtures/App/config/routing.yml new file mode 100644 index 000000000..64f4b9f27 --- /dev/null +++ b/Tests/Fixtures/App/config/routing.yml @@ -0,0 +1,24 @@ +hello_bundle: + resource: "@CLChillMainBundle/Resources/config/routing.yml" + +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' \ No newline at end of file diff --git a/Tests/Services/MenuComposer.php b/Tests/Services/MenuComposerTest.php similarity index 81% rename from Tests/Services/MenuComposer.php rename to Tests/Services/MenuComposerTest.php index 66be61954..3d5074e85 100644 --- a/Tests/Services/MenuComposer.php +++ b/Tests/Services/MenuComposerTest.php @@ -11,7 +11,7 @@ use Symfony\Component\Routing\RouteCollection; * * @author Julien Fastré */ -class MenuComposer extends KernelTestCase +class MenuComposerTest extends KernelTestCase { /** @@ -28,17 +28,17 @@ class MenuComposer extends KernelTestCase public function setUp() { - self::bootKernel(); + self::bootKernel(array('environment' => 'test')); $this->loader = static::$kernel->getContainer() ->get('routing.loader'); $this->menuComposer = static::$kernel->getContainer() - ->get('chill_main.menu_composer'); + ->get('chill.main.menu_composer'); } public function testMenuComposer() { $collection = new RouteCollection(); - $collection->add($this->loader->load(__DIR__.'dummy_menu_composer.yml', 'yaml')); + //$collection->add($this->loader->load(__DIR__.'dummy_menu_composer.yml', 'yaml')); $routes = $this->menuComposer->getRoutesFor('dummy0'); @@ -54,11 +54,12 @@ class MenuComposer extends KernelTestCase } $this->assertSame(array( 50 => array( - 'key' => 'chill_main_dummy_0', + 'route' => 'chill_main_dummy_0', 'label' => 'test0', + 'helper'=> null ), 51 => array( - 'key' => 'chill_main_dummy_1', + 'route' => 'chill_main_dummy_1', 'label' => 'test1', 'helper'=> 'great helper' ) diff --git a/composer.json b/composer.json index 4cec0fd5c..a5cf513c1 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,9 @@ "require": { "twig/extensions": "~1.0", "symfony/assetic-bundle": "~2.3", - "symfony/monolog-bundle": "~2.4" + "symfony/monolog-bundle": "~2.4", + "symfony/framework-bundle": "2.5.*", + "symfony/yaml": "2.5.*", + "symfony/symfony": "2.5.*" } } - diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3b8a2a23b..3358647e3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,4 +16,7 @@ + + +