Version 1
[yaffs-website] / vendor / symfony / http-kernel / Tests / Fragment / EsiFragmentRendererTest.php
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\Component\HttpKernel\Tests\Fragment;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpKernel\Controller\ControllerReference;
16 use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
17 use Symfony\Component\HttpKernel\HttpCache\Esi;
18 use Symfony\Component\HttpFoundation\Request;
19 use Symfony\Component\HttpKernel\UriSigner;
20
21 class EsiFragmentRendererTest extends TestCase
22 {
23     public function testRenderFallbackToInlineStrategyIfEsiNotSupported()
24     {
25         $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(true));
26         $strategy->render('/', Request::create('/'));
27     }
28
29     public function testRender()
30     {
31         $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
32
33         $request = Request::create('/');
34         $request->setLocale('fr');
35         $request->headers->set('Surrogate-Capability', 'ESI/1.0');
36
37         $this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
38         $this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
39         $this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
40     }
41
42     public function testRenderControllerReference()
43     {
44         $signer = new UriSigner('foo');
45         $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy(), $signer);
46
47         $request = Request::create('/');
48         $request->setLocale('fr');
49         $request->headers->set('Surrogate-Capability', 'ESI/1.0');
50
51         $reference = new ControllerReference('main_controller', array(), array());
52         $altReference = new ControllerReference('alt_controller', array(), array());
53
54         $this->assertEquals(
55             '<esi:include src="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller&_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D" alt="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dalt_controller&_hash=iPJEdRoUpGrM1ztqByiorpfMPtiW%2FOWwdH1DBUXHhEc%3D" />',
56             $strategy->render($reference, $request, array('alt' => $altReference))->getContent()
57         );
58     }
59
60     /**
61      * @expectedException \LogicException
62      */
63     public function testRenderControllerReferenceWithoutSignerThrowsException()
64     {
65         $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
66
67         $request = Request::create('/');
68         $request->setLocale('fr');
69         $request->headers->set('Surrogate-Capability', 'ESI/1.0');
70
71         $strategy->render(new ControllerReference('main_controller'), $request);
72     }
73
74     /**
75      * @expectedException \LogicException
76      */
77     public function testRenderAltControllerReferenceWithoutSignerThrowsException()
78     {
79         $strategy = new EsiFragmentRenderer(new Esi(), $this->getInlineStrategy());
80
81         $request = Request::create('/');
82         $request->setLocale('fr');
83         $request->headers->set('Surrogate-Capability', 'ESI/1.0');
84
85         $strategy->render('/', $request, array('alt' => new ControllerReference('alt_controller')));
86     }
87
88     private function getInlineStrategy($called = false)
89     {
90         $inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock();
91
92         if ($called) {
93             $inline->expects($this->once())->method('render');
94         }
95
96         return $inline;
97     }
98 }