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 Symfony\Component\Config\FileLocator
;
15 use Symfony\Component\Routing\Loader\XmlFileLoader
;
16 use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader
;
18 class XmlFileLoaderTest
extends \PHPUnit_Framework_TestCase
20 protected function setUp()
22 if (!class_exists('Symfony\Component\Config\FileLocator')) {
23 $this->markTestSkipped('The "Config" component is not available');
27 public function testSupports()
29 $loader = new XmlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
31 $this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
32 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
34 $this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
35 $this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
38 public function testLoadWithRoute()
40 $loader = new XmlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
41 $routeCollection = $loader->load('validpattern.xml');
42 $routes = $routeCollection->all();
44 $this->assertCount(2, $routes, 'Two routes are loaded');
45 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
47 foreach ($routes as $route) {
48 $this->assertSame('/blog/{slug}', $route->getPath());
49 $this->assertSame('{locale}.example.com', $route->getHost());
50 $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
51 $this->assertSame('\w+', $route->getRequirement('locale'));
52 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
53 $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
54 $this->assertEquals(array('https'), $route->getSchemes());
58 public function testLoadWithNamespacePrefix()
60 $loader = new XmlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
61 $routeCollection = $loader->load('namespaceprefix.xml');
63 $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
65 $route = $routeCollection->get('blog_show');
66 $this->assertSame('/blog/{slug}', $route->getPath());
67 $this->assertSame('{_locale}.example.com', $route->getHost());
68 $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
69 $this->assertSame('\w+', $route->getRequirement('slug'));
70 $this->assertSame('en|fr|de', $route->getRequirement('_locale'));
71 $this->assertSame(null, $route->getDefault('slug'));
72 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
75 public function testLoadWithImport()
77 $loader = new XmlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
78 $routeCollection = $loader->load('validresource.xml');
79 $routes = $routeCollection->all();
81 $this->assertCount(2, $routes, 'Two routes are loaded');
82 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
84 foreach ($routes as $route) {
85 $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
86 $this->assertSame('123', $route->getDefault('foo'));
87 $this->assertSame('\d+', $route->getRequirement('foo'));
88 $this->assertSame('bar', $route->getOption('foo'));
89 $this->assertSame('', $route->getHost());
94 * @expectedException \InvalidArgumentException
95 * @dataProvider getPathsToInvalidFiles
97 public function testLoadThrowsExceptionWithInvalidFile($filePath)
99 $loader = new XmlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
100 $loader->load($filePath);
104 * @expectedException \InvalidArgumentException
105 * @dataProvider getPathsToInvalidFiles
107 public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
109 $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
110 $loader->load($filePath);
113 public function getPathsToInvalidFiles()
115 return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
119 * @expectedException \InvalidArgumentException
120 * @expectedExceptionMessage Document types are not allowed.
122 public function testDocTypeIsNotAllowed()
124 $loader = new XmlFileLoader(new FileLocator(array(__DIR__
.'/../Fixtures')));
125 $loader->load('withdoctype.xml');