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
12namespace Symfony\Component\Routing\Tests\Loader;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\Config\FileLocator;
16use Symfony\Component\Config\Resource\FileResource;
17use Symfony\Component\Routing\Loader\PhpFileLoader;
18use Symfony\Component\Routing\Route;
19use Symfony\Component\Routing\RouteCollection;
20
21class PhpFileLoaderTest extends TestCase
22{
23    public function testSupports()
24    {
25        $loader = new PhpFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
26
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');
29
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');
32    }
33
34    public function testLoadWithRoute()
35    {
36        $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
37        $routeCollection = $loader->load('validpattern.php');
38        $routes = $routeCollection->all();
39
40        $this->assertCount(1, $routes, 'One route is loaded');
41        $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
42
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());
50        }
51    }
52
53    public function testLoadWithImport()
54    {
55        $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
56        $routeCollection = $loader->load('validresource.php');
57        $routes = $routeCollection->all();
58
59        $this->assertCount(1, $routes, 'One route is loaded');
60        $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
61
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());
69        }
70    }
71
72    public function testThatDefiningVariableInConfigFileHasNoSideEffects()
73    {
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);
81        $this->assertSame(
82            realpath($locator->locate('with_define_path_variable.php')),
83            (string) $fileResource
84        );
85    }
86
87    public function testRoutingConfigurator()
88    {
89        $locator = new FileLocator(array(__DIR__.'/../Fixtures'));
90        $loader = new PhpFileLoader($locator);
91        $routeCollection = $loader->load('php_dsl.php');
92
93        $expectedCollection = new RouteCollection();
94
95        $expectedCollection->add('foo', (new Route('/foo'))
96            ->setOptions(array('utf8' => true))
97            ->setCondition('abc')
98        );
99        $expectedCollection->add('buz', (new Route('/zub'))
100            ->setDefaults(array('_controller' => 'foo:act'))
101        );
102        $expectedCollection->add('c_bar', (new Route('/sub/pub/bar'))
103            ->setRequirements(array('id' => '\d+'))
104        );
105        $expectedCollection->add('c_pub_buz', (new Route('/sub/pub/buz'))
106            ->setHost('host')
107            ->setRequirements(array('id' => '\d+'))
108        );
109        $expectedCollection->add('ouf', (new Route('/ouf'))
110            ->setSchemes(array('https'))
111            ->setMethods(array('GET'))
112            ->setDefaults(array('id' => 0))
113        );
114
115        $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl_sub.php')));
116        $expectedCollection->addResource(new FileResource(realpath(__DIR__.'/../Fixtures/php_dsl.php')));
117
118        $this->assertEquals($expectedCollection, $routeCollection);
119    }
120}
121