TabMenuTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Tests\Menu\Integration;
  11. use Knp\Menu\MenuFactory;
  12. use Knp\Menu\MenuItem;
  13. use Prophecy\Argument;
  14. class TabMenuTest extends BaseMenuTest
  15. {
  16. protected $translator;
  17. public function getTranslator()
  18. {
  19. if (isset($this->translator)) {
  20. return $this->translator;
  21. }
  22. return parent::getTranslator();
  23. }
  24. public function testLabelTranslationNominalCase()
  25. {
  26. $translatorProphecy = $this->prophesize(
  27. 'Symfony\Component\Translation\TranslatorInterface'
  28. );
  29. $translatorProphecy
  30. ->trans(
  31. 'some-label',
  32. array(),
  33. Argument::any(), //messages or null
  34. null
  35. )
  36. ->willReturn('my-translation');
  37. $this->translator = $translatorProphecy->reveal();
  38. $factory = new MenuFactory();
  39. $menu = new MenuItem('test-menu', $factory);
  40. $menu->addChild('some-label', array('uri' => '/whatever'));
  41. $this->assertContains('my-translation', $this->renderMenu($menu));
  42. }
  43. public function testLabelTranslationWithParameters()
  44. {
  45. $params = array('my' => 'param');
  46. $translatorProphecy = $this->prophesize(
  47. 'Symfony\Component\Translation\TranslatorInterface'
  48. );
  49. $translatorProphecy
  50. ->trans(
  51. 'some-label',
  52. $params,
  53. Argument::any(), // messages or null
  54. null
  55. )
  56. ->willReturn('my-translation');
  57. $this->translator = $translatorProphecy->reveal();
  58. $factory = new MenuFactory();
  59. $menu = new MenuItem('test-menu', $factory);
  60. $menu->addChild('some-label', array('uri' => '/whatever'))
  61. ->setExtra('translation_params', $params);
  62. $this->assertContains('my-translation', $this->renderMenu($menu));
  63. }
  64. public function testLabelTranslationDomainOverride()
  65. {
  66. $translatorProphecy = $this->prophesize(
  67. 'Symfony\Component\Translation\TranslatorInterface'
  68. );
  69. $translatorProphecy
  70. ->trans('some-label', array(), 'my_local_domain', null)
  71. ->willReturn('my-translation');
  72. $translatorProphecy
  73. ->trans('some-other-label', array(), 'my_global_domain', null)
  74. ->willReturn('my-other-translation');
  75. $this->translator = $translatorProphecy->reveal();
  76. $factory = new MenuFactory();
  77. $menu = new MenuItem('test-menu', $factory);
  78. $menu->setExtra('translation_domain', 'my_global_domain');
  79. $menu->addChild('some-label', array('uri' => '/whatever'))
  80. ->setExtra('translation_domain', 'my_local_domain');
  81. $menu->addChild('some-other-label', array('uri' => '/whatever'));
  82. $html = $this->renderMenu($menu);
  83. $this->assertContains('my-translation', $html);
  84. $this->assertContains('my-other-translation', $html);
  85. }
  86. protected function getTemplate()
  87. {
  88. return 'Core/tab_menu_template.html.twig';
  89. }
  90. }