3a040dedd6e54051b77545555583568f8e6cdd92
[yaffs-website] / vendor / symfony / http-kernel / Tests / Fragment / RoutableFragmentRendererTest.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\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Controller\ControllerReference;
17
18 class RoutableFragmentRendererTest extends TestCase
19 {
20     /**
21      * @dataProvider getGenerateFragmentUriData
22      */
23     public function testGenerateFragmentUri($uri, $controller)
24     {
25         $this->assertEquals($uri, $this->callGenerateFragmentUriMethod($controller, Request::create('/')));
26     }
27
28     /**
29      * @dataProvider getGenerateFragmentUriData
30      */
31     public function testGenerateAbsoluteFragmentUri($uri, $controller)
32     {
33         $this->assertEquals('http://localhost'.$uri, $this->callGenerateFragmentUriMethod($controller, Request::create('/'), true));
34     }
35
36     public function getGenerateFragmentUriData()
37     {
38         return array(
39             array('/_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())),
40             array('/_fragment?_path=_format%3Dxml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())),
41             array('/_fragment?_path=foo%3Dfoo%26_format%3Djson%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())),
42             array('/_fragment?bar=bar&_path=foo%3Dfoo%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))),
43             array('/_fragment?foo=foo&_path=_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))),
44             array('/_fragment?_path=foo%255B0%255D%3Dfoo%26foo%255B1%255D%3Dbar%26_format%3Dhtml%26_locale%3Den%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => array('foo', 'bar')), array())),
45         );
46     }
47
48     public function testGenerateFragmentUriWithARequest()
49     {
50         $request = Request::create('/');
51         $request->attributes->set('_format', 'json');
52         $request->setLocale('fr');
53         $controller = new ControllerReference('controller', array(), array());
54
55         $this->assertEquals('/_fragment?_path=_format%3Djson%26_locale%3Dfr%26_controller%3Dcontroller', $this->callGenerateFragmentUriMethod($controller, $request));
56     }
57
58     /**
59      * @expectedException \LogicException
60      * @dataProvider      getGenerateFragmentUriDataWithNonScalar
61      */
62     public function testGenerateFragmentUriWithNonScalar($controller)
63     {
64         $this->callGenerateFragmentUriMethod($controller, Request::create('/'));
65     }
66
67     public function getGenerateFragmentUriDataWithNonScalar()
68     {
69         return array(
70             array(new ControllerReference('controller', array('foo' => new Foo(), 'bar' => 'bar'), array())),
71             array(new ControllerReference('controller', array('foo' => array('foo' => 'foo'), 'bar' => array('bar' => new Foo())), array())),
72         );
73     }
74
75     private function callGenerateFragmentUriMethod(ControllerReference $reference, Request $request, $absolute = false)
76     {
77         $renderer = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer');
78         $r = new \ReflectionObject($renderer);
79         $m = $r->getMethod('generateFragmentUri');
80         $m->setAccessible(true);
81
82         return $m->invoke($renderer, $reference, $request, $absolute);
83     }
84 }
85
86 class Foo
87 {
88     public $foo;
89
90     public function getFoo()
91     {
92         return $this->foo;
93     }
94 }