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());
52 public function testLegacyRouteDefinitionLoading()
54 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
55 $routeCollection = $loader->load('legacy_validpattern.xml');
56 $route = $routeCollection->get('blog_show_legacy');
58 $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
59 $this->assertSame('/blog/{slug}', $route->getPath());
60 $this->assertSame('{locale}.example.com', $route->getHost());
61 $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
62 $this->assertSame('\w+', $route->getRequirement('locale'));
63 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
64 $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
65 $this->assertEquals(array('https'), $route->getSchemes());
66 $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
69 public function testLoadWithNamespacePrefix()
71 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
72 $routeCollection = $loader->load('namespaceprefix.xml');
74 $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
76 $route = $routeCollection->get('blog_show');
77 $this->assertSame('/blog/{slug}', $route->getPath());
78 $this->assertSame('{_locale}.example.com', $route->getHost());
79 $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
80 $this->assertSame('\w+', $route->getRequirement('slug'));
81 $this->assertSame('en|fr|de', $route->getRequirement('_locale'));
82 $this->assertNull($route->getDefault('slug'));
83 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
86 public function testLoadWithImport()
88 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
89 $routeCollection = $loader->load('validresource.xml');
90 $routes = $routeCollection->all();
92 $this->assertCount(2, $routes, 'Two routes are loaded');
93 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
95 foreach ($routes as $route) {
96 $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
97 $this->assertSame('123', $route->getDefault('foo'));
98 $this->assertSame('\d+', $route->getRequirement('foo'));
99 $this->assertSame('bar', $route->getOption('foo'));
100 $this->assertSame('', $route->getHost());
101 $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
106 * @expectedException \InvalidArgumentException
107 * @dataProvider getPathsToInvalidFiles
109 public function testLoadThrowsExceptionWithInvalidFile($filePath)
111 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
112 $loader->load($filePath);
116 * @expectedException \InvalidArgumentException
117 * @dataProvider getPathsToInvalidFiles
119 public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
121 $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
122 $loader->load($filePath);
125 public function getPathsToInvalidFiles()
127 return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
131 * @expectedException \InvalidArgumentException
132 * @expectedExceptionMessage Document types are not allowed.
134 public function testDocTypeIsNotAllowed()
136 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
137 $loader->load('withdoctype.xml');
140 public function testNullValues()
142 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
143 $routeCollection = $loader->load('null_values.xml');
144 $route = $routeCollection->get('blog_show');
146 $this->assertTrue($route->hasDefault('foo'));
147 $this->assertNull($route->getDefault('foo'));
148 $this->assertTrue($route->hasDefault('bar'));
149 $this->assertNull($route->getDefault('bar'));
150 $this->assertEquals('foo', $route->getDefault('foobar'));
151 $this->assertEquals('bar', $route->getDefault('baz'));