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