| 3 |
liveuser |
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 |
public function testSupports()
|
|
|
21 |
{
|
|
|
22 |
$loader = new XmlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
|
|
|
23 |
|
|
|
24 |
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
|
|
|
25 |
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
|
|
|
26 |
|
|
|
27 |
$this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
|
|
|
28 |
$this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function testLoadWithRoute()
|
|
|
32 |
{
|
|
|
33 |
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
|
|
34 |
$routeCollection = $loader->load('validpattern.xml');
|
|
|
35 |
$route = $routeCollection->get('blog_show');
|
|
|
36 |
|
|
|
37 |
$this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
|
|
|
38 |
$this->assertSame('/blog/{slug}', $route->getPath());
|
|
|
39 |
$this->assertSame('{locale}.example.com', $route->getHost());
|
|
|
40 |
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
|
|
|
41 |
$this->assertSame('\w+', $route->getRequirement('locale'));
|
|
|
42 |
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
|
|
|
43 |
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
|
|
|
44 |
$this->assertEquals(array('https'), $route->getSchemes());
|
|
|
45 |
$this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function testLoadWithNamespacePrefix()
|
|
|
49 |
{
|
|
|
50 |
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
|
|
51 |
$routeCollection = $loader->load('namespaceprefix.xml');
|
|
|
52 |
|
|
|
53 |
$this->assertCount(1, $routeCollection->all(), 'One route is loaded');
|
|
|
54 |
|
|
|
55 |
$route = $routeCollection->get('blog_show');
|
|
|
56 |
$this->assertSame('/blog/{slug}', $route->getPath());
|
|
|
57 |
$this->assertSame('{_locale}.example.com', $route->getHost());
|
|
|
58 |
$this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
|
|
|
59 |
$this->assertSame('\w+', $route->getRequirement('slug'));
|
|
|
60 |
$this->assertSame('en|fr|de', $route->getRequirement('_locale'));
|
|
|
61 |
$this->assertNull($route->getDefault('slug'));
|
|
|
62 |
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
public function testLoadWithImport()
|
|
|
66 |
{
|
|
|
67 |
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
|
|
68 |
$routeCollection = $loader->load('validresource.xml');
|
|
|
69 |
$routes = $routeCollection->all();
|
|
|
70 |
|
|
|
71 |
$this->assertCount(2, $routes, 'Two routes are loaded');
|
|
|
72 |
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
|
|
|
73 |
|
|
|
74 |
foreach ($routes as $route) {
|
|
|
75 |
$this->assertSame('/{foo}/blog/{slug}', $route->getPath());
|
|
|
76 |
$this->assertSame('123', $route->getDefault('foo'));
|
|
|
77 |
$this->assertSame('\d+', $route->getRequirement('foo'));
|
|
|
78 |
$this->assertSame('bar', $route->getOption('foo'));
|
|
|
79 |
$this->assertSame('', $route->getHost());
|
|
|
80 |
$this->assertSame('context.getMethod() == "POST"', $route->getCondition());
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* @expectedException \InvalidArgumentException
|
|
|
86 |
* @dataProvider getPathsToInvalidFiles
|
|
|
87 |
*/
|
|
|
88 |
public function testLoadThrowsExceptionWithInvalidFile($filePath)
|
|
|
89 |
{
|
|
|
90 |
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
|
|
91 |
$loader->load($filePath);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @expectedException \InvalidArgumentException
|
|
|
96 |
* @dataProvider getPathsToInvalidFiles
|
|
|
97 |
*/
|
|
|
98 |
public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
|
|
|
99 |
{
|
|
|
100 |
$loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
|
|
101 |
$loader->load($filePath);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
public function getPathsToInvalidFiles()
|
|
|
105 |
{
|
|
|
106 |
return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* @expectedException \InvalidArgumentException
|
|
|
111 |
* @expectedExceptionMessage Document types are not allowed.
|
|
|
112 |
*/
|
|
|
113 |
public function testDocTypeIsNotAllowed()
|
|
|
114 |
{
|
|
|
115 |
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
|
|
116 |
$loader->load('withdoctype.xml');
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function testNullValues()
|
|
|
120 |
{
|
|
|
121 |
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
|
|
122 |
$routeCollection = $loader->load('null_values.xml');
|
|
|
123 |
$route = $routeCollection->get('blog_show');
|
|
|
124 |
|
|
|
125 |
$this->assertTrue($route->hasDefault('foo'));
|
|
|
126 |
$this->assertNull($route->getDefault('foo'));
|
|
|
127 |
$this->assertTrue($route->hasDefault('bar'));
|
|
|
128 |
$this->assertNull($route->getDefault('bar'));
|
|
|
129 |
$this->assertEquals('foo', $route->getDefault('foobar'));
|
|
|
130 |
$this->assertEquals('bar', $route->getDefault('baz'));
|
|
|
131 |
}
|
|
|
132 |
}
|