No Description

PhpFileLoaderTest.php 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 Symfony\Component\Config\FileLocator;
  12. use Symfony\Component\Routing\Loader\PhpFileLoader;
  13. class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testSupports()
  16. {
  17. $loader = new PhpFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
  18. $this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
  19. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
  20. $this->assertTrue($loader->supports('foo.php', 'php'), '->supports() checks the resource type if specified');
  21. $this->assertFalse($loader->supports('foo.php', 'foo'), '->supports() checks the resource type if specified');
  22. }
  23. public function testLoadWithRoute()
  24. {
  25. $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  26. $routeCollection = $loader->load('validpattern.php');
  27. $routes = $routeCollection->all();
  28. $this->assertCount(2, $routes, 'Two routes are loaded');
  29. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  30. foreach ($routes as $route) {
  31. $this->assertSame('/blog/{slug}', $route->getPath());
  32. $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
  33. $this->assertSame('{locale}.example.com', $route->getHost());
  34. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  35. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  36. $this->assertEquals(array('https'), $route->getSchemes());
  37. }
  38. }
  39. public function testLoadWithImport()
  40. {
  41. $loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  42. $routeCollection = $loader->load('validresource.php');
  43. $routes = $routeCollection->all();
  44. $this->assertCount(2, $routes, 'Two routes are loaded');
  45. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  46. foreach ($routes as $route) {
  47. $this->assertSame('/prefix/blog/{slug}', $route->getPath());
  48. $this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
  49. $this->assertSame('{locale}.example.com', $route->getHost());
  50. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  51. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  52. $this->assertEquals(array('https'), $route->getSchemes());
  53. }
  54. }
  55. public function testThatDefiningVariableInConfigFileHasNoSideEffects()
  56. {
  57. $locator = new FileLocator(array(__DIR__.'/../Fixtures'));
  58. $loader = new PhpFileLoader($locator);
  59. $routeCollection = $loader->load('with_define_path_variable.php');
  60. $resources = $routeCollection->getResources();
  61. $this->assertCount(1, $resources);
  62. $this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
  63. $fileResource = reset($resources);
  64. $this->assertSame(
  65. realpath($locator->locate('with_define_path_variable.php')),
  66. (string) $fileResource
  67. );
  68. }
  69. }