PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

https://gitlab.com/freebird/WebApp
PHP | 138 lines | 96 code | 22 blank | 20 comment | 0 complexity | 548c9163cdc3e8f2352dfcf854b465f0 MD5 | raw file
  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\XmlFileLoader;
  13. use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader;
  14. class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testSupports()
  17. {
  18. $loader = new XmlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
  19. $this->assertTrue($loader->supports('foo.xml'), '->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.xml', 'xml'), '->supports() checks the resource type if specified');
  22. $this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
  23. }
  24. public function testLoadWithRoute()
  25. {
  26. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  27. $routeCollection = $loader->load('validpattern.xml');
  28. $routes = $routeCollection->all();
  29. $this->assertCount(3, $routes, 'Three routes are loaded');
  30. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  31. $identicalRoutes = array_slice($routes, 0, 2);
  32. foreach ($identicalRoutes as $route) {
  33. $this->assertSame('/blog/{slug}', $route->getPath());
  34. $this->assertSame('{locale}.example.com', $route->getHost());
  35. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  36. $this->assertSame('\w+', $route->getRequirement('locale'));
  37. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  38. $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
  39. $this->assertEquals(array('https'), $route->getSchemes());
  40. $this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
  41. }
  42. }
  43. public function testLoadWithNamespacePrefix()
  44. {
  45. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  46. $routeCollection = $loader->load('namespaceprefix.xml');
  47. $this->assertCount(1, $routeCollection->all(), 'One route is loaded');
  48. $route = $routeCollection->get('blog_show');
  49. $this->assertSame('/blog/{slug}', $route->getPath());
  50. $this->assertSame('{_locale}.example.com', $route->getHost());
  51. $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller'));
  52. $this->assertSame('\w+', $route->getRequirement('slug'));
  53. $this->assertSame('en|fr|de', $route->getRequirement('_locale'));
  54. $this->assertNull($route->getDefault('slug'));
  55. $this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
  56. }
  57. public function testLoadWithImport()
  58. {
  59. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  60. $routeCollection = $loader->load('validresource.xml');
  61. $routes = $routeCollection->all();
  62. $this->assertCount(3, $routes, 'Three routes are loaded');
  63. $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
  64. foreach ($routes as $route) {
  65. $this->assertSame('/{foo}/blog/{slug}', $route->getPath());
  66. $this->assertSame('123', $route->getDefault('foo'));
  67. $this->assertSame('\d+', $route->getRequirement('foo'));
  68. $this->assertSame('bar', $route->getOption('foo'));
  69. $this->assertSame('', $route->getHost());
  70. $this->assertSame('context.getMethod() == "POST"', $route->getCondition());
  71. }
  72. }
  73. /**
  74. * @expectedException \InvalidArgumentException
  75. * @dataProvider getPathsToInvalidFiles
  76. */
  77. public function testLoadThrowsExceptionWithInvalidFile($filePath)
  78. {
  79. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  80. $loader->load($filePath);
  81. }
  82. /**
  83. * @expectedException \InvalidArgumentException
  84. * @dataProvider getPathsToInvalidFiles
  85. */
  86. public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
  87. {
  88. $loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  89. $loader->load($filePath);
  90. }
  91. public function getPathsToInvalidFiles()
  92. {
  93. return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
  94. }
  95. /**
  96. * @expectedException \InvalidArgumentException
  97. * @expectedExceptionMessage Document types are not allowed.
  98. */
  99. public function testDocTypeIsNotAllowed()
  100. {
  101. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  102. $loader->load('withdoctype.xml');
  103. }
  104. public function testNullValues()
  105. {
  106. $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
  107. $routeCollection = $loader->load('null_values.xml');
  108. $route = $routeCollection->get('blog_show');
  109. $this->assertTrue($route->hasDefault('foo'));
  110. $this->assertNull($route->getDefault('foo'));
  111. $this->assertTrue($route->hasDefault('bar'));
  112. $this->assertNull($route->getDefault('bar'));
  113. $this->assertEquals('foo', $route->getDefault('foobar'));
  114. $this->assertEquals('bar', $route->getDefault('baz'));
  115. }
  116. }