BaseMenuTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\ItemInterface;
  12. use Knp\Menu\Renderer\TwigRenderer;
  13. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  14. use Symfony\Bridge\Twig\Tests\Extension\Fixtures\StubFilesystemLoader;
  15. use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
  16. /**
  17. * Class BaseTemplateTest.
  18. *
  19. * Base class for tests checking rendering of twig templates
  20. */
  21. abstract class BaseMenuTest extends \PHPUnit_Framework_TestCase
  22. {
  23. abstract protected function getTemplate();
  24. protected function getTranslator()
  25. {
  26. return new StubTranslator();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function setUp()
  32. {
  33. $twigPaths = array(
  34. __DIR__.'/../../../vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views',
  35. __DIR__.'/../../../Resources/views',
  36. );
  37. $loader = new StubFilesystemLoader($twigPaths);
  38. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
  39. }
  40. protected function renderMenu(ItemInterface $item, array $options = array())
  41. {
  42. $this->environment->addExtension(new TranslationExtension($this->getTranslator()));
  43. $this->renderer = new TwigRenderer(
  44. $this->environment,
  45. $this->getTemplate(),
  46. $this->getMock('Knp\Menu\Matcher\MatcherInterface')
  47. );
  48. return $this->renderer->render($item, $options);
  49. }
  50. /**
  51. * Helper method to strip newline and space characters from html string to make comparing easier.
  52. *
  53. * @param string $html
  54. *
  55. * @return string
  56. */
  57. protected function cleanHtmlWhitespace($html)
  58. {
  59. $html = preg_replace_callback('/>([^<]+)</', function ($value) {
  60. return '>'.trim($value[1]).'<';
  61. }, $html);
  62. return $html;
  63. }
  64. }