]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/routing/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
9f038c1611a7c74152f8edd1f3d0295f807b88e1
[github/wallabag/wallabag.git] / vendor / symfony / routing / Symfony / Component / 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 Symfony\Component\Config\FileLocator;
15 use Symfony\Component\Routing\Loader\XmlFileLoader;
16 use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader;
17
18 class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
19 {
20 protected function setUp()
21 {
22 if (!class_exists('Symfony\Component\Config\FileLocator')) {
23 $this->markTestSkipped('The "Config" component is not available');
24 }
25 }
26
27 public function testSupports()
28 {
29 $loader = new XmlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
30
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');
33
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');
36 }
37
38 public function testLoadWithRoute()
39 {
40 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
41 $routeCollection = $loader->load('validpattern.xml');
42 $routes = $routeCollection->all();
43
44 $this->assertCount(2, $routes, 'Two routes are loaded');
45 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
46
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());
55 }
56 }
57
58 public function testLoadWithNamespacePrefix()
59 {
60 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
61 $routeCollection = $loader->load('namespaceprefix.xml');
62
63 $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
64
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'));
73 }
74
75 public function testLoadWithImport()
76 {
77 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
78 $routeCollection = $loader->load('validresource.xml');
79 $routes = $routeCollection->all();
80
81 $this->assertCount(2, $routes, 'Two routes are loaded');
82 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
83
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());
90 }
91 }
92
93 /**
94 * @expectedException \InvalidArgumentException
95 * @dataProvider getPathsToInvalidFiles
96 */
97 public function testLoadThrowsExceptionWithInvalidFile($filePath)
98 {
99 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
100 $loader->load($filePath);
101 }
102
103 /**
104 * @expectedException \InvalidArgumentException
105 * @dataProvider getPathsToInvalidFiles
106 */
107 public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
108 {
109 $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
110 $loader->load($filePath);
111 }
112
113 public function getPathsToInvalidFiles()
114 {
115 return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
116 }
117
118 /**
119 * @expectedException \InvalidArgumentException
120 * @expectedExceptionMessage Document types are not allowed.
121 */
122 public function testDocTypeIsNotAllowed()
123 {
124 $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
125 $loader->load('withdoctype.xml');
126 }
127 }