KnpMenuServiceProviderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Knp\Menu\Tests\Silex;
  3. use Knp\Menu\Matcher\Matcher;
  4. use Knp\Menu\Silex\KnpMenuServiceProvider;
  5. use Knp\Menu\Silex\Voter\RouteVoter;
  6. use Silex\Application;
  7. use Silex\Provider\TwigServiceProvider;
  8. use Silex\Provider\UrlGeneratorServiceProvider;
  9. use Symfony\Component\HttpFoundation\Request;
  10. class KnpMenuServiceProviderTest extends \PHPUnit_Framework_TestCase
  11. {
  12. public function setUp()
  13. {
  14. if (!class_exists('Silex\Application')) {
  15. $this->markTestSkipped('Silex is not available');
  16. }
  17. }
  18. public function testFactoryWithoutRouter()
  19. {
  20. $app = new Application();
  21. $app->register(new KnpMenuServiceProvider());
  22. $this->assertEquals('Knp\Menu\MenuFactory', get_class($app['knp_menu.factory']));
  23. }
  24. public function testTwigRendererNotRegistered()
  25. {
  26. $app = new Application();
  27. $app->register(new KnpMenuServiceProvider());
  28. $this->assertFalse(isset($app['knp_menu.renderer.twig']));
  29. }
  30. public function testTwigRendererRegistered()
  31. {
  32. $app = new Application();
  33. $app->register(new TwigServiceProvider());
  34. $app->register(new KnpMenuServiceProvider());
  35. $this->assertTrue(isset($app['knp_menu.renderer.twig']));
  36. }
  37. public function testRenderNotCurrentWithList()
  38. {
  39. $app = $this->bootstrapApp();
  40. $request = Request::create('/list');
  41. $response = $app->handle($request);
  42. $this->assertEquals('<ul class="nav"><li class="first"><a href="/twig">Home</a></li><li class="last"><a href="http://knplabs.com">KnpLabs</a></li></ul>', $response->getContent());
  43. }
  44. public function testRenderCurrentWithTwig()
  45. {
  46. $app = $this->bootstrapApp();
  47. $request = Request::create('/twig');
  48. $response = $app->handle($request);
  49. $this->assertEquals('<ul class="nav"><li class="current first"><a href="/twig">Home</a></li><li class="last"><a href="http://knplabs.com">KnpLabs</a></li></ul>', $response->getContent());
  50. $app = $this->bootstrapApp();
  51. $request = Request::create('/other-twig');
  52. $response = $app->handle($request);
  53. $this->assertEquals('<ul class="nav"><li class="first"><a href="/twig">Home</a></li><li class="current last"><a href="http://knplabs.com">KnpLabs</a></li></ul>', $response->getContent());
  54. }
  55. private function bootstrapApp()
  56. {
  57. $app = new Application();
  58. $app['debug'] = true;
  59. $app->register(new TwigServiceProvider(), array(
  60. 'twig.templates' => array('main' => '{{ knp_menu_render("my_menu", {"compressed": true}, renderer) }}'),
  61. ));
  62. $app->register(new KnpMenuServiceProvider(), array(
  63. 'knp_menu.menus' => array('my_menu' => 'test.menu.my'),
  64. ));
  65. $app->register(new UrlGeneratorServiceProvider());
  66. $app['test.menu.my'] = function (Application $app) {
  67. /** @var $factory \Knp\Menu\FactoryInterface */
  68. $factory = $app['knp_menu.factory'];
  69. $root = $factory->createItem('root', array('childrenAttributes' => array('class' => 'nav')));
  70. $root->addChild('home', array('route' => 'homepage', 'label' => 'Home'));
  71. $root->addChild('KnpLabs', array('uri' => 'http://knplabs.com', 'extras' => array('routes' => 'other_route')));
  72. return $root;
  73. };
  74. $app['test.voter'] = $app->share(function (Application $app) {
  75. $voter = new RouteVoter();
  76. $voter->setRequest($app['request']);
  77. return $voter;
  78. });
  79. $app['knp_menu.matcher.configure'] = $app->protect(function (Matcher $matcher) use ($app) {
  80. $matcher->addVoter($app['test.voter']);
  81. });
  82. $app->get('/twig', function (Application $app) {
  83. return $app['twig']->render('main', array('renderer' => 'twig'));
  84. })->bind('homepage');
  85. $app->get('/other-twig', function (Application $app) {
  86. return $app['twig']->render('main', array('renderer' => 'twig'));
  87. })->bind('other_route');
  88. $app->get('/list', function (Application $app) {
  89. return $app['twig']->render('main', array('renderer' => 'list'));
  90. })->bind('list');
  91. return $app;
  92. }
  93. }