PhpFileLoaderTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Tests\Loader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\Routing\Loader\PhpFileLoader;
  14. class PhpFileLoaderTest extends TestCase
  15. {
  16. public function testSupports()
  17. {
  18. $loader = new PhpFileLoader($this->getMockBuilder('Symfony\Component\Config\FileLocator')->getMock());
  19. $this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
  20. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  21. $this->assertTrue($loader->supports('foo.php', 'php'), '->supports() checks the resource type if specified');
  22. $this->assertFalse($loader->supports('foo.php', 'foo'), '->supports() checks the resource type if specified');
  23. }
  24. public function testLoadWithRoute()
  25. {
  26. $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  27. $routeCollection = $loader->load('validpattern.php');
  28. $routes = $routeCollection->all();
  29. $this->assertCount(1, $routes, 'One route is loaded');
  30. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  31. foreach ($routes as $route) {
  32. $this->assertSame('/blog/{slug}', $route->getPath());
  33. $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
  34. $this->assertSame('{locale}.example.com', $route->getHost());
  35. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  36. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  37. $this->assertEquals(array('https'), $route->getSchemes());
  38. }
  39. }
  40. public function testLoadWithImport()
  41. {
  42. $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  43. $routeCollection = $loader->load('validresource.php');
  44. $routes = $routeCollection->all();
  45. $this->assertCount(1, $routes, 'One route is loaded');
  46. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  47. foreach ($routes as $route) {
  48. $this->assertSame('/prefix/blog/{slug}', $route->getPath());
  49. $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
  50. $this->assertSame('{locale}.example.com', $route->getHost());
  51. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  52. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  53. $this->assertEquals(array('https'), $route->getSchemes());
  54. }
  55. }
  56. public function testThatDefiningVariableInConfigFileHasNoSideEffects()
  57. {
  58. $locator = new FileLocator(array(__DIR__.'/../Fixtures'));
  59. $loader = new PhpFileLoader($locator);
  60. $routeCollection = $loader->load('with_define_path_variable.php');
  61. $resources = $routeCollection->getResources();
  62. $this->assertCount(1, $resources);
  63. $this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
  64. $fileResource = reset($resources);
  65. $this->assertSame(
  66. realpath($locator->locate('with_define_path_variable.php')),
  67. (string) $fileResource
  68. );
  69. }
  70. }