BaseMenuTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * Base class for tests checking rendering of twig templates.
  18. */
  19. abstract class BaseMenuTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function setUp()
  25. {
  26. // Adapt to both bundle and project-wide test strategy
  27. $twigPaths = array_filter(array(
  28. __DIR__.'/../../../../../../vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views',
  29. __DIR__.'/../../../vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views',
  30. __DIR__.'/../../../Resources/views',
  31. ), 'is_dir');
  32. $loader = new StubFilesystemLoader($twigPaths);
  33. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
  34. }
  35. abstract protected function getTemplate();
  36. protected function getTranslator()
  37. {
  38. return new StubTranslator();
  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->getMockForAbstractClass('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. }