|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Component\HttpKernel\Controller\ControllerReference; |
| 15 | +use Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer; |
| 16 | +use Symfony\Component\HttpKernel\HttpCache\Ssi; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpKernel\UriSigner; |
| 19 | + |
| 20 | +class SsiFragmentRendererTest extends \PHPUnit_Framework_TestCase |
| 21 | +{ |
| 22 | + public function testRenderFallbackToInlineStrategyIfSsiNotSupported() |
| 23 | + { |
| 24 | + $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy(true)); |
| 25 | + $strategy->render('/', Request::create('/')); |
| 26 | + } |
| 27 | + |
| 28 | + public function testRender() |
| 29 | + { |
| 30 | + $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy()); |
| 31 | + |
| 32 | + $request = Request::create('/'); |
| 33 | + $request->setLocale('fr'); |
| 34 | + $request->headers->set('Surrogate-Capability', 'SSI/1.0'); |
| 35 | + |
| 36 | + $this->assertEquals('<!--#include virtual="/" -->', $strategy->render('/', $request)->getContent()); |
| 37 | + $this->assertEquals('<!--#include virtual="/" -->', $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent(), 'Strategy options should not impact the ssi include tag'); |
| 38 | + } |
| 39 | + |
| 40 | + public function testRenderControllerReference() |
| 41 | + { |
| 42 | + $signer = new UriSigner('foo'); |
| 43 | + $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy(), $signer); |
| 44 | + |
| 45 | + $request = Request::create('/'); |
| 46 | + $request->setLocale('fr'); |
| 47 | + $request->headers->set('Surrogate-Capability', 'SSI/1.0'); |
| 48 | + |
| 49 | + $reference = new ControllerReference('main_controller', array(), array()); |
| 50 | + $altReference = new ControllerReference('alt_controller', array(), array()); |
| 51 | + |
| 52 | + $this->assertEquals( |
| 53 | + '<!--#include virtual="/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3Dmain_controller&_hash=Jz1P8NErmhKTeI6onI1EdAXTB85359MY3RIk5mSJ60w%3D" -->', |
| 54 | + $strategy->render($reference, $request, array('alt' => $altReference))->getContent() |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @expectedException \LogicException |
| 60 | + */ |
| 61 | + public function testRenderControllerReferenceWithoutSignerThrowsException() |
| 62 | + { |
| 63 | + $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy()); |
| 64 | + |
| 65 | + $request = Request::create('/'); |
| 66 | + $request->setLocale('fr'); |
| 67 | + $request->headers->set('Surrogate-Capability', 'SSI/1.0'); |
| 68 | + |
| 69 | + $strategy->render(new ControllerReference('main_controller'), $request); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @expectedException \LogicException |
| 74 | + */ |
| 75 | + public function testRenderAltControllerReferenceWithoutSignerThrowsException() |
| 76 | + { |
| 77 | + $strategy = new SsiFragmentRenderer(new Ssi(), $this->getInlineStrategy()); |
| 78 | + |
| 79 | + $request = Request::create('/'); |
| 80 | + $request->setLocale('fr'); |
| 81 | + $request->headers->set('Surrogate-Capability', 'SSI/1.0'); |
| 82 | + |
| 83 | + $strategy->render('/', $request, array('alt' => new ControllerReference('alt_controller'))); |
| 84 | + } |
| 85 | + |
| 86 | + private function getInlineStrategy($called = false) |
| 87 | + { |
| 88 | + $inline = $this->getMockBuilder('Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer')->disableOriginalConstructor()->getMock(); |
| 89 | + |
| 90 | + if ($called) { |
| 91 | + $inline->expects($this->once())->method('render'); |
| 92 | + } |
| 93 | + |
| 94 | + return $inline; |
| 95 | + } |
| 96 | +} |
0 commit comments