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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\Config\Resource\FileResource;
17 use Symfony\Component\Routing\Loader\PhpFileLoader;
18 use Symfony\Component\Routing\Route;
19 use Symfony\Component\Routing\RouteCollection;
21 class PhpFileLoaderTest extends TestCase
23 public function testSupports()
25 $loader = new PhpFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
27 $this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
28 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
30 $this->assertTrue($loader->supports('foo.php', 'php'), '->supports() checks the resource type if specified');
31 $this->assertFalse($loader->supports('foo.php', 'foo'), '->supports() checks the resource type if specified');
34 public function testLoadWithRoute()
36 $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
37 $routeCollection = $loader->load('validpattern.php');
38 $routes = $routeCollection->all();
40 $this->assertCount(1, $routes, 'One route is loaded');
41 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
43 foreach ($routes as $route) {
44 $this->assertSame('/blog/{slug}', $route->getPath());
45 $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
46 $this->assertSame('{locale}.example.com', $route->getHost());
47 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
48 $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
49 $this->assertEquals(array('https'), $route->getSchemes());
53 public function testLoadWithImport()
55 $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
56 $routeCollection = $loader->load('validresource.php');
57 $routes = $routeCollection->all();
59 $this->assertCount(1, $routes, 'One route is loaded');
60 $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
62 foreach ($routes as $route) {
63 $this->assertSame('/prefix/blog/{slug}', $route->getPath());
64 $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
65 $this->assertSame('{locale}.example.com', $route->getHost());
66 $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
67 $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
68 $this->assertEquals(array('https'), $route->getSchemes());
72 public function testThatDefiningVariableInConfigFileHasNoSideEffects()
74 $locator = new FileLocator(array(__DIR__.'/../Fixtures'));
75 $loader = new PhpFileLoader($locator);
76 $routeCollection = $loader->load('with_define_path_variable.php');
77 $resources = $routeCollection->getResources();
78 $this->assertCount(1, $resources);
79 $this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
80 $fileResource = reset($resources);
82 realpath($locator->locate('with_define_path_variable.php')),
83 (string) $fileResource
87 public function testRoutingConfigurator()
89 $locator = new FileLocator(array(__DIR__.'/../Fixtures'));
90 $loader = new PhpFileLoader($locator);
91 $routeCollection = $loader->load('php_dsl.php');
93 $expectedCollection = new RouteCollection();
95 $expectedCollection->add('foo', (new Route('/foo'))
96 ->setOptions(array('utf8' => true))
99 $expectedCollection->add('buz', (new Route('/zub'))
100 ->setDefaults(array('_controller' => 'foo:act'))
102 $expectedCollection->add('c_bar', (new Route('/sub/pub/bar'))
103 ->setRequirements(array('id' => '\d+'))
105 $expectedCollection->add('c_pub_buz', (new Route('/sub/pub/buz'))
107 ->setRequirements(array('id' => '\d+'))
109 $expectedCollection->add('ouf', (new Route('/ouf'))
110 ->setSchemes(array('https'))
111 ->setMethods(array('GET'))
112 ->setDefaults(array('id' => 0))
115 $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
116 $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
118 $this->assertEquals($expectedCollection, $routeCollection);
121 public function testRoutingConfiguratorCanImportGlobPatterns()
123 $locator = new FileLocator(array(__DIR__.'/../Fixtures/glob'));
124 $loader = new PhpFileLoader($locator);
125 $routeCollection = $loader->load('php_dsl.php');
127 $route = $routeCollection->get('bar_route');
128 $this->assertSame('AppBundle:Bar:view', $route->getDefault('_controller'));
130 $route = $routeCollection->get('baz_route');
131 $this->assertSame('AppBundle:Baz:view', $route->getDefault('_controller'));