BaseMenuTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /**
  24. * {@inheritdoc}
  25. */
  26. public function setUp()
  27. {
  28. // Adapt to both bundle and project-wide test strategy
  29. $twigPaths = array_filter(array(
  30. __DIR__.'/../../../../../../vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views',
  31. __DIR__.'/../../../vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views',
  32. __DIR__.'/../../../Resources/views',
  33. ), 'is_dir');
  34. $loader = new StubFilesystemLoader($twigPaths);
  35. $this->environment = new \Twig_Environment($loader, array('strict_variables' => true));
  36. }
  37. abstract protected function getTemplate();
  38. protected function getTranslator()
  39. {
  40. return new StubTranslator();
  41. }
  42. protected function renderMenu(ItemInterface $item, array $options = array())
  43. {
  44. $this->environment->addExtension(new TranslationExtension($this->getTranslator()));
  45. $this->renderer = new TwigRenderer(
  46. $this->environment,
  47. $this->getTemplate(),
  48. $this->getMock('Knp\Menu\Matcher\MatcherInterface')
  49. );
  50. return $this->renderer->render($item, $options);
  51. }
  52. /**
  53. * Helper method to strip newline and space characters from html string to make comparing easier.
  54. *
  55. * @param string $html
  56. *
  57. * @return string
  58. */
  59. protected function cleanHtmlWhitespace($html)
  60. {
  61. $html = preg_replace_callback('/>([^<]+)</', function ($value) {
  62. return '>'.trim($value[1]).'<';
  63. }, $html);
  64. return $html;
  65. }
  66. }