TemplateControllerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\Bundle\FrameworkBundle\Tests\Controller;
  11. use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
  12. use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15. * @author Kévin Dunglas <dunglas@gmail.com>
  16. */
  17. class TemplateControllerTest extends TestCase
  18. {
  19. public function testTwig()
  20. {
  21. $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
  22. $twig->expects($this->once())->method('render')->willReturn('bar');
  23. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  24. $container->expects($this->at(0))->method('has')->will($this->returnValue(false));
  25. $container->expects($this->at(1))->method('has')->will($this->returnValue(true));
  26. $container->expects($this->at(2))->method('get')->will($this->returnValue($twig));
  27. $controller = new TemplateController();
  28. $controller->setContainer($container);
  29. $this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
  30. }
  31. public function testTemplating()
  32. {
  33. $templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
  34. $templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar'));
  35. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  36. $container->expects($this->at(0))->method('has')->willReturn(true);
  37. $container->expects($this->at(1))->method('get')->will($this->returnValue($templating));
  38. $controller = new TemplateController();
  39. $controller->setContainer($container);
  40. $this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
  41. }
  42. /**
  43. * @expectedException \LogicException
  44. * @expectedExceptionMessage You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.
  45. */
  46. public function testNoTwigNorTemplating()
  47. {
  48. $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
  49. $container->expects($this->at(0))->method('has')->willReturn(false);
  50. $container->expects($this->at(1))->method('has')->willReturn(false);
  51. $controller = new TemplateController();
  52. $controller->setContainer($container);
  53. $controller->templateAction('mytemplate')->getContent();
  54. }
  55. }