|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Tests\Controller; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Tests\TestCase; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Kévin Dunglas <dunglas@gmail.com> |
| 20 | + */ |
| 21 | +class TemplateControllerTest extends TestCase |
| 22 | +{ |
| 23 | + public function testTwig() |
| 24 | + { |
| 25 | + $twig = $this->getMockBuilder('\Twig_Environment')->disableOriginalConstructor()->getMock(); |
| 26 | + $twig->expects($this->once())->method('render')->willReturn('bar'); |
| 27 | + |
| 28 | + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
| 29 | + $container->expects($this->at(0))->method('has')->will($this->returnValue(false)); |
| 30 | + $container->expects($this->at(1))->method('has')->will($this->returnValue(true)); |
| 31 | + $container->expects($this->at(2))->method('get')->will($this->returnValue($twig)); |
| 32 | + |
| 33 | + $controller = new TemplateController(); |
| 34 | + $controller->setContainer($container); |
| 35 | + |
| 36 | + $this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent()); |
| 37 | + } |
| 38 | + |
| 39 | + public function testTemplating() |
| 40 | + { |
| 41 | + $templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock(); |
| 42 | + $templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar')); |
| 43 | + |
| 44 | + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
| 45 | + $container->expects($this->at(0))->method('has')->willReturn(true); |
| 46 | + $container->expects($this->at(1))->method('get')->will($this->returnValue($templating)); |
| 47 | + |
| 48 | + $controller = new TemplateController(); |
| 49 | + $controller->setContainer($container); |
| 50 | + |
| 51 | + $this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @expectedException \LogicException |
| 56 | + * @expectedExceptionMessage You can not use the TemplateController if the Templating Component or the Twig Bundle are not available. |
| 57 | + */ |
| 58 | + public function testNoTwigNorTemplating() |
| 59 | + { |
| 60 | + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); |
| 61 | + $container->expects($this->at(0))->method('has')->willReturn(false); |
| 62 | + $container->expects($this->at(1))->method('has')->willReturn(false); |
| 63 | + |
| 64 | + $controller = new TemplateController(); |
| 65 | + $controller->setContainer($container); |
| 66 | + |
| 67 | + $controller->templateAction('mytemplate')->getContent(); |
| 68 | + } |
| 69 | +} |
0 commit comments