private function bootstrapApp() { $app = new Application(); $app['debug'] = true; $app->register(new TwigServiceProvider(), array('twig.templates' => array('main' => '{{ knp_menu_render("my_menu", {"compressed": true}, renderer) }}'))); $app->register(new KnpMenuServiceProvider(), array('knp_menu.menus' => array('my_menu' => 'test.menu.my'))); $app->register(new UrlGeneratorServiceProvider()); $app['test.menu.my'] = function (Application $app) { /** @var $factory \Knp\Menu\FactoryInterface */ $factory = $app['knp_menu.factory']; $root = $factory->createItem('root', array('childrenAttributes' => array('class' => 'nav'))); $root->addChild('home', array('route' => 'homepage', 'label' => 'Home')); $root->addChild('KnpLabs', array('uri' => 'http://knplabs.com', 'extras' => array('routes' => 'other_route'))); return $root; }; $app['test.voter'] = $app->share(function (Application $app) { $voter = new RouteVoter(); $voter->setRequest($app['request']); return $voter; }); $app['knp_menu.matcher.configure'] = $app->protect(function (Matcher $matcher) use($app) { $matcher->addVoter($app['test.voter']); }); $app->get('/twig', function (Application $app) { return $app['twig']->render('main', array('renderer' => 'twig')); })->bind('homepage'); $app->get('/other-twig', function (Application $app) { return $app['twig']->render('main', array('renderer' => 'twig')); })->bind('other_route'); $app->get('/list', function (Application $app) { return $app['twig']->render('main', array('renderer' => 'list')); })->bind('list'); return $app; }
/** * @param string $route * @param array $parameters * @param string|array $itemRoutes * @param array $itemsRoutesParameters * @param boolean $expected * @param boolean $deprecatedCall * * @dataProvider provideData */ public function testMatching($route, array $parameters, $itemRoutes, array $itemsRoutesParameters, $expected, $deprecatedCall = false) { $item = $this->getMock('Knp\\Menu\\ItemInterface'); $item->expects($this->any())->method('getExtra')->with($this->logicalOr($this->equalTo('routes'), $this->equalTo('routesParameters')))->will($this->returnCallback(function ($parameter) use($itemRoutes, $itemsRoutesParameters) { switch ($parameter) { case 'routes': return $itemRoutes; case 'routesParameters': return $itemsRoutesParameters; } })); $request = new Request(); $request->attributes->set('_route', $route); $request->attributes->set('_route_params', $parameters); $voter = new RouteVoter(); $voter->setRequest($request); $deprecatedErrorCatched = false; set_error_handler(function ($errorNumber, $message, $file, $line) use(&$deprecatedErrorCatched) { if ($errorNumber & E_USER_DEPRECATED) { $deprecatedErrorCatched = true; return true; } return \PHPUnit_Util_ErrorHandler::handleError($errorNumber, $message, $file, $line); }); try { $this->assertSame($expected, $voter->matchItem($item)); } catch (\Exception $e) { restore_error_handler(); throw $e; } restore_error_handler(); $this->assertSame($deprecatedCall, $deprecatedErrorCatched); }