TwigEngineTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bridge\Twig\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\TwigEngine;
  13. use Symfony\Component\Templating\TemplateReference;
  14. use Twig\Environment;
  15. use Twig\Loader\ArrayLoader;
  16. class TwigEngineTest extends TestCase
  17. {
  18. public function testExistsWithTemplateInstances()
  19. {
  20. $engine = $this->getTwig();
  21. $this->assertTrue($engine->exists($this->getMockForAbstractClass('Twig\Template', array(), '', false)));
  22. }
  23. public function testExistsWithNonExistentTemplates()
  24. {
  25. $engine = $this->getTwig();
  26. $this->assertFalse($engine->exists('foobar'));
  27. $this->assertFalse($engine->exists(new TemplateReference('foorbar')));
  28. }
  29. public function testExistsWithTemplateWithSyntaxErrors()
  30. {
  31. $engine = $this->getTwig();
  32. $this->assertTrue($engine->exists('error'));
  33. $this->assertTrue($engine->exists(new TemplateReference('error')));
  34. }
  35. public function testExists()
  36. {
  37. $engine = $this->getTwig();
  38. $this->assertTrue($engine->exists('index'));
  39. $this->assertTrue($engine->exists(new TemplateReference('index')));
  40. }
  41. public function testRender()
  42. {
  43. $engine = $this->getTwig();
  44. $this->assertSame('foo', $engine->render('index'));
  45. $this->assertSame('foo', $engine->render(new TemplateReference('index')));
  46. }
  47. /**
  48. * @expectedException \Twig\Error\SyntaxError
  49. */
  50. public function testRenderWithError()
  51. {
  52. $engine = $this->getTwig();
  53. $engine->render(new TemplateReference('error'));
  54. }
  55. protected function getTwig()
  56. {
  57. $twig = new Environment(new ArrayLoader(array(
  58. 'index' => 'foo',
  59. 'error' => '{{ foo }',
  60. )));
  61. $parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
  62. return new TwigEngine($twig, $parser);
  63. }
  64. }