4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Loader;
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;
19 class XmlFileLoaderTest extends TestCase
21 public function testSupports()
23 $loader = new XmlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
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');
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');
32 public function testLoadWithRoute()
34 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
35 $routeCollection = $loader->load('validpattern.xml');
36 $route = $routeCollection->get('blog_show');
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());
49 public function testLoadWithNamespacePrefix()
51 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
52 $routeCollection = $loader->load('namespaceprefix.xml');
54 $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
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'));
67 public function testLoadWithImport()
69 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
70 $routeCollection = $loader->load('validresource.xml');
71 $routes = $routeCollection->all();
73 $this->assertCount(2, $routes, 'Two routes are loaded');
74 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
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());
87 * @expectedException \InvalidArgumentException
88 * @dataProvider getPathsToInvalidFiles
90 public function testLoadThrowsExceptionWithInvalidFile($filePath)
92 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
93 $loader->load($filePath);
97 * @expectedException \InvalidArgumentException
98 * @dataProvider getPathsToInvalidFiles
100 public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
102 $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
103 $loader->load($filePath);
106 public function getPathsToInvalidFiles()
108 return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
112 * @expectedException \InvalidArgumentException
113 * @expectedExceptionMessage Document types are not allowed.
115 public function testDocTypeIsNotAllowed()
117 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
118 $loader->load('withdoctype.xml');
121 public function testNullValues()
123 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
124 $routeCollection = $loader->load('null_values.xml');
125 $route = $routeCollection->get('blog_show');
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'));
135 public function testScalarDataTypeDefaults()
137 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
138 $routeCollection = $loader->load('scalar_defaults.xml');
139 $route = $routeCollection->get('blog');
143 '_controller' => 'AcmeBlogBundle:Blog:index',
154 $route->getDefaults()
158 public function testListDefaults()
160 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
161 $routeCollection = $loader->load('list_defaults.xml');
162 $route = $routeCollection->get('blog');
166 '_controller' => 'AcmeBlogBundle:Blog:index',
167 'values' => array(true, 1, 3.5, 'foo'),
169 $route->getDefaults()
173 public function testListInListDefaults()
175 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
176 $routeCollection = $loader->load('list_in_list_defaults.xml');
177 $route = $routeCollection->get('blog');
181 '_controller' => 'AcmeBlogBundle:Blog:index',
182 'values' => array(array(true, 1, 3.5, 'foo')),
184 $route->getDefaults()
188 public function testListInMapDefaults()
190 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
191 $routeCollection = $loader->load('list_in_map_defaults.xml');
192 $route = $routeCollection->get('blog');
196 '_controller' => 'AcmeBlogBundle:Blog:index',
197 'values' => array('list' => array(true, 1, 3.5, 'foo')),
199 $route->getDefaults()
203 public function testMapDefaults()
205 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
206 $routeCollection = $loader->load('map_defaults.xml');
207 $route = $routeCollection->get('blog');
211 '_controller' => 'AcmeBlogBundle:Blog:index',
219 $route->getDefaults()
223 public function testMapInListDefaults()
225 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
226 $routeCollection = $loader->load('map_in_list_defaults.xml');
227 $route = $routeCollection->get('blog');
231 '_controller' => 'AcmeBlogBundle:Blog:index',
232 'values' => array(array(
239 $route->getDefaults()
243 public function testMapInMapDefaults()
245 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
246 $routeCollection = $loader->load('map_in_map_defaults.xml');
247 $route = $routeCollection->get('blog');
251 '_controller' => 'AcmeBlogBundle:Blog:index',
252 'values' => array('map' => array(
259 $route->getDefaults()
263 public function testNullValuesInList()
265 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
266 $routeCollection = $loader->load('list_null_values.xml');
267 $route = $routeCollection->get('blog');
269 $this->assertSame(array(null, null, null, null, null, null), $route->getDefault('list'));
272 public function testNullValuesInMap()
274 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
275 $routeCollection = $loader->load('map_null_values.xml');
276 $route = $routeCollection->get('blog');
287 $route->getDefault('map')