d24ec79a79c59341bb4dc3b7865accd18ea6c1f0
[yaffs-website] / vendor / symfony / routing / Tests / Loader / XmlFileLoaderTest.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\Routing\Tests\Loader;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\Routing\Loader\XmlFileLoader;
17 use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader;
18
19 class XmlFileLoaderTest extends TestCase
20 {
21     public function testSupports()
22     {
23         $loader = new XmlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
24
25         $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
26         $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
27
28         $this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
29         $this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
30     }
31
32     public function testLoadWithRoute()
33     {
34         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
35         $routeCollection = $loader->load('validpattern.xml');
36         $route = $routeCollection->get('blog_show');
37
38         $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
39         $this->assertSame('/blog/{slug}', $route->getPath());
40         $this->assertSame('{locale}.example.com', $route->getHost());
41         $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
42         $this->assertSame('\w+', $route->getRequirement('locale'));
43         $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
44         $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
45         $this->assertEquals(array('https'), $route->getSchemes());
46         $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
47     }
48
49     public function testLoadWithNamespacePrefix()
50     {
51         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
52         $routeCollection = $loader->load('namespaceprefix.xml');
53
54         $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
55
56         $route = $routeCollection->get('blog_show');
57         $this->assertSame('/blog/{slug}', $route->getPath());
58         $this->assertSame('{_locale}.example.com', $route->getHost());
59         $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
60         $this->assertSame('\w+', $route->getRequirement('slug'));
61         $this->assertSame('en|fr|de', $route->getRequirement('_locale'));
62         $this->assertNull($route->getDefault('slug'));
63         $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
64         $this->assertSame(1, $route->getDefault('page'));
65     }
66
67     public function testLoadWithImport()
68     {
69         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
70         $routeCollection = $loader->load('validresource.xml');
71         $routes = $routeCollection->all();
72
73         $this->assertCount(2, $routes, 'Two routes are loaded');
74         $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
75
76         foreach ($routes as $route) {
77             $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
78             $this->assertSame('123', $route->getDefault('foo'));
79             $this->assertSame('\d+', $route->getRequirement('foo'));
80             $this->assertSame('bar', $route->getOption('foo'));
81             $this->assertSame('', $route->getHost());
82             $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
83         }
84     }
85
86     /**
87      * @expectedException \InvalidArgumentException
88      * @dataProvider getPathsToInvalidFiles
89      */
90     public function testLoadThrowsExceptionWithInvalidFile($filePath)
91     {
92         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
93         $loader->load($filePath);
94     }
95
96     /**
97      * @expectedException \InvalidArgumentException
98      * @dataProvider getPathsToInvalidFiles
99      */
100     public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
101     {
102         $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
103         $loader->load($filePath);
104     }
105
106     public function getPathsToInvalidFiles()
107     {
108         return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
109     }
110
111     /**
112      * @expectedException \InvalidArgumentException
113      * @expectedExceptionMessage Document types are not allowed.
114      */
115     public function testDocTypeIsNotAllowed()
116     {
117         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
118         $loader->load('withdoctype.xml');
119     }
120
121     public function testNullValues()
122     {
123         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
124         $routeCollection = $loader->load('null_values.xml');
125         $route = $routeCollection->get('blog_show');
126
127         $this->assertTrue($route->hasDefault('foo'));
128         $this->assertNull($route->getDefault('foo'));
129         $this->assertTrue($route->hasDefault('bar'));
130         $this->assertNull($route->getDefault('bar'));
131         $this->assertEquals('foo', $route->getDefault('foobar'));
132         $this->assertEquals('bar', $route->getDefault('baz'));
133     }
134
135     public function testScalarDataTypeDefaults()
136     {
137         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
138         $routeCollection = $loader->load('scalar_defaults.xml');
139         $route = $routeCollection->get('blog');
140
141         $this->assertSame(
142             array(
143                 '_controller' => 'AcmeBlogBundle:Blog:index',
144                 'slug' => null,
145                 'published' => true,
146                 'page' => 1,
147                 'price' => 3.5,
148                 'archived' => false,
149                 'free' => true,
150                 'locked' => false,
151                 'foo' => null,
152                 'bar' => null,
153             ),
154             $route->getDefaults()
155         );
156     }
157
158     public function testListDefaults()
159     {
160         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
161         $routeCollection = $loader->load('list_defaults.xml');
162         $route = $routeCollection->get('blog');
163
164         $this->assertSame(
165             array(
166                 '_controller' => 'AcmeBlogBundle:Blog:index',
167                 'values' => array(true, 1, 3.5, 'foo'),
168             ),
169             $route->getDefaults()
170         );
171     }
172
173     public function testListInListDefaults()
174     {
175         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
176         $routeCollection = $loader->load('list_in_list_defaults.xml');
177         $route = $routeCollection->get('blog');
178
179         $this->assertSame(
180             array(
181                 '_controller' => 'AcmeBlogBundle:Blog:index',
182                 'values' => array(array(true, 1, 3.5, 'foo')),
183             ),
184             $route->getDefaults()
185         );
186     }
187
188     public function testListInMapDefaults()
189     {
190         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
191         $routeCollection = $loader->load('list_in_map_defaults.xml');
192         $route = $routeCollection->get('blog');
193
194         $this->assertSame(
195             array(
196                 '_controller' => 'AcmeBlogBundle:Blog:index',
197                 'values' => array('list' => array(true, 1, 3.5, 'foo')),
198             ),
199             $route->getDefaults()
200         );
201     }
202
203     public function testMapDefaults()
204     {
205         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
206         $routeCollection = $loader->load('map_defaults.xml');
207         $route = $routeCollection->get('blog');
208
209         $this->assertSame(
210             array(
211                 '_controller' => 'AcmeBlogBundle:Blog:index',
212                 'values' => array(
213                     'public' => true,
214                     'page' => 1,
215                     'price' => 3.5,
216                     'title' => 'foo',
217                 ),
218             ),
219             $route->getDefaults()
220         );
221     }
222
223     public function testMapInListDefaults()
224     {
225         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
226         $routeCollection = $loader->load('map_in_list_defaults.xml');
227         $route = $routeCollection->get('blog');
228
229         $this->assertSame(
230             array(
231                 '_controller' => 'AcmeBlogBundle:Blog:index',
232                 'values' => array(array(
233                     'public' => true,
234                     'page' => 1,
235                     'price' => 3.5,
236                     'title' => 'foo',
237                 )),
238             ),
239             $route->getDefaults()
240         );
241     }
242
243     public function testMapInMapDefaults()
244     {
245         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
246         $routeCollection = $loader->load('map_in_map_defaults.xml');
247         $route = $routeCollection->get('blog');
248
249         $this->assertSame(
250             array(
251                 '_controller' => 'AcmeBlogBundle:Blog:index',
252                 'values' => array('map' => array(
253                     'public' => true,
254                     'page' => 1,
255                     'price' => 3.5,
256                     'title' => 'foo',
257                 )),
258             ),
259             $route->getDefaults()
260         );
261     }
262
263     public function testNullValuesInList()
264     {
265         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
266         $routeCollection = $loader->load('list_null_values.xml');
267         $route = $routeCollection->get('blog');
268
269         $this->assertSame(array(null, null, null, null, null, null), $route->getDefault('list'));
270     }
271
272     public function testNullValuesInMap()
273     {
274         $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
275         $routeCollection = $loader->load('map_null_values.xml');
276         $route = $routeCollection->get('blog');
277
278         $this->assertSame(
279             array(
280                 'boolean' => null,
281                 'integer' => null,
282                 'float' => null,
283                 'string' => null,
284                 'list' => null,
285                 'map' => null,
286             ),
287             $route->getDefault('map')
288         );
289     }
290 }