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\YamlFileLoader;
17: use Symfony\Component\Config\Resource\FileResource;
18:
19: class YamlFileLoaderTest extends TestCase
20: {
21: public function testSupports()
22: {
23: $loader = new YamlFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
24:
25: $this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
26: $this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
27: $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
28:
29: $this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
30: $this->assertTrue($loader->supports('foo.yaml', 'yaml'), '->supports() checks the resource type if specified');
31: $this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
32: }
33:
34: public function testLoadDoesNothingIfEmpty()
35: {
36: $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
37: $collection = $loader->load('empty.yml');
38:
39: $this->assertEquals(array(), $collection->all());
40: $this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
41: }
42:
43: 44: 45: 46:
47: public function testLoadThrowsExceptionWithInvalidFile($filePath)
48: {
49: $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
50: $loader->load($filePath);
51: }
52:
53: public function getPathsToInvalidFiles()
54: {
55: return array(
56: array('nonvalid.yml'),
57: array('nonvalid2.yml'),
58: array('incomplete.yml'),
59: array('nonvalidkeys.yml'),
60: array('nonesense_resource_plus_path.yml'),
61: array('nonesense_type_without_resource.yml'),
62: array('bad_format.yml'),
63: );
64: }
65:
66: public function testLoadSpecialRouteName()
67: {
68: $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
69: $routeCollection = $loader->load('special_route_name.yml');
70: $route = $routeCollection->get('#$péß^a|');
71:
72: $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
73: $this->assertSame('/true', $route->getPath());
74: }
75:
76: public function testLoadWithRoute()
77: {
78: $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
79: $routeCollection = $loader->load('validpattern.yml');
80: $route = $routeCollection->get('blog_show');
81:
82: $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
83: $this->assertSame('/blog/{slug}', $route->getPath());
84: $this->assertSame('{locale}.example.com', $route->getHost());
85: $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
86: $this->assertSame('\w+', $route->getRequirement('locale'));
87: $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
88: $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
89: $this->assertEquals(array('https'), $route->getSchemes());
90: $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
91: }
92:
93: 94: 95:
96: public function testLegacyRouteDefinitionLoading()
97: {
98: $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
99: $routeCollection = $loader->load('legacy_validpattern.yml');
100: $route = $routeCollection->get('blog_show_legacy');
101:
102: $this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
103: $this->assertSame('/blog/{slug}', $route->getPath());
104: $this->assertSame('{locale}.example.com', $route->getHost());
105: $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
106: $this->assertSame('\w+', $route->getRequirement('locale'));
107: $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
108: $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
109: $this->assertEquals(array('https'), $route->getSchemes());
110: $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
111: }
112:
113: public function testLoadWithResource()
114: {
115: $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
116: $routeCollection = $loader->load('validresource.yml');
117: $routes = $routeCollection->all();
118:
119: $this->assertCount(2, $routes, 'Two routes are loaded');
120: $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
121:
122: foreach ($routes as $route) {
123: $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
124: $this->assertSame('123', $route->getDefault('foo'));
125: $this->assertSame('\d+', $route->getRequirement('foo'));
126: $this->assertSame('bar', $route->getOption('foo'));
127: $this->assertSame('', $route->getHost());
128: $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
129: }
130: }
131: }
132: