1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 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: 50: 51:
52: public function testLegacyRouteDefinitionLoading()
53: {
54: $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
55: $routeCollection = $loader->load('legacy_validpattern.xml');
56: $route = $routeCollection->get('blog_show_legacy');
57:
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());
67: }
68:
69: public function testLoadWithNamespacePrefix()
70: {
71: $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
72: $routeCollection = $loader->load('namespaceprefix.xml');
73:
74: $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
75:
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'));
84: }
85:
86: public function testLoadWithImport()
87: {
88: $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
89: $routeCollection = $loader->load('validresource.xml');
90: $routes = $routeCollection->all();
91:
92: $this->assertCount(2, $routes, 'Two routes are loaded');
93: $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
94:
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());
102: }
103: }
104:
105: 106: 107: 108:
109: public function testLoadThrowsExceptionWithInvalidFile($filePath)
110: {
111: $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
112: $loader->load($filePath);
113: }
114:
115: 116: 117: 118:
119: public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
120: {
121: $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
122: $loader->load($filePath);
123: }
124:
125: public function getPathsToInvalidFiles()
126: {
127: return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
128: }
129:
130: 131: 132: 133:
134: public function testDocTypeIsNotAllowed()
135: {
136: $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
137: $loader->load('withdoctype.xml');
138: }
139:
140: public function testNullValues()
141: {
142: $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
143: $routeCollection = $loader->load('null_values.xml');
144: $route = $routeCollection->get('blog_show');
145:
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'));
152: }
153: }
154: