Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\RouteCollection;
  14. use Symfony\Component\Routing\Router;
  15. class RouterTest extends TestCase
  16. {
  17. private $router = null;
  18. private $loader = null;
  19. protected function setUp()
  20. {
  21. $this->loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
  22. $this->router = new Router($this->loader, 'routing.yml');
  23. }
  24. public function testSetOptionsWithSupportedOptions()
  25. {
  26. $this->router->setOptions([
  27. 'cache_dir' => './cache',
  28. 'debug' => true,
  29. 'resource_type' => 'ResourceType',
  30. ]);
  31. $this->assertSame('./cache', $this->router->getOption('cache_dir'));
  32. $this->assertTrue($this->router->getOption('debug'));
  33. $this->assertSame('ResourceType', $this->router->getOption('resource_type'));
  34. }
  35. /**
  36. * @expectedException \InvalidArgumentException
  37. * @expectedExceptionMessage The Router does not support the following options: "option_foo", "option_bar"
  38. */
  39. public function testSetOptionsWithUnsupportedOptions()
  40. {
  41. $this->router->setOptions([
  42. 'cache_dir' => './cache',
  43. 'option_foo' => true,
  44. 'option_bar' => 'baz',
  45. 'resource_type' => 'ResourceType',
  46. ]);
  47. }
  48. public function testSetOptionWithSupportedOption()
  49. {
  50. $this->router->setOption('cache_dir', './cache');
  51. $this->assertSame('./cache', $this->router->getOption('cache_dir'));
  52. }
  53. /**
  54. * @expectedException \InvalidArgumentException
  55. * @expectedExceptionMessage The Router does not support the "option_foo" option
  56. */
  57. public function testSetOptionWithUnsupportedOption()
  58. {
  59. $this->router->setOption('option_foo', true);
  60. }
  61. /**
  62. * @expectedException \InvalidArgumentException
  63. * @expectedExceptionMessage The Router does not support the "option_foo" option
  64. */
  65. public function testGetOptionWithUnsupportedOption()
  66. {
  67. $this->router->getOption('option_foo', true);
  68. }
  69. public function testThatRouteCollectionIsLoaded()
  70. {
  71. $this->router->setOption('resource_type', 'ResourceType');
  72. $routeCollection = new RouteCollection();
  73. $this->loader->expects($this->once())
  74. ->method('load')->with('routing.yml', 'ResourceType')
  75. ->willReturn($routeCollection);
  76. $this->assertSame($routeCollection, $this->router->getRouteCollection());
  77. }
  78. public function testMatcherIsCreatedIfCacheIsNotConfigured()
  79. {
  80. $this->router->setOption('cache_dir', null);
  81. $this->loader->expects($this->once())
  82. ->method('load')->with('routing.yml', null)
  83. ->willReturn(new RouteCollection());
  84. $this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher());
  85. }
  86. public function testGeneratorIsCreatedIfCacheIsNotConfigured()
  87. {
  88. $this->router->setOption('cache_dir', null);
  89. $this->loader->expects($this->once())
  90. ->method('load')->with('routing.yml', null)
  91. ->willReturn(new RouteCollection());
  92. $this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator());
  93. }
  94. public function testMatchRequestWithUrlMatcherInterface()
  95. {
  96. $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();
  97. $matcher->expects($this->once())->method('match');
  98. $p = new \ReflectionProperty($this->router, 'matcher');
  99. $p->setAccessible(true);
  100. $p->setValue($this->router, $matcher);
  101. $this->router->matchRequest(Request::create('/'));
  102. }
  103. public function testMatchRequestWithRequestMatcherInterface()
  104. {
  105. $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
  106. $matcher->expects($this->once())->method('matchRequest');
  107. $p = new \ReflectionProperty($this->router, 'matcher');
  108. $p->setAccessible(true);
  109. $p->setValue($this->router, $matcher);
  110. $this->router->matchRequest(Request::create('/'));
  111. }
  112. }