RouterTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. ->will($this->returnValue($routeCollection));
  76. $this->assertSame($routeCollection, $this->router->getRouteCollection());
  77. }
  78. /**
  79. * @dataProvider provideMatcherOptionsPreventingCaching
  80. */
  81. public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
  82. {
  83. $this->router->setOption($option, null);
  84. $this->loader->expects($this->once())
  85. ->method('load')->with('routing.yml', null)
  86. ->will($this->returnValue(new RouteCollection()));
  87. $this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher());
  88. }
  89. public function provideMatcherOptionsPreventingCaching()
  90. {
  91. return [
  92. ['cache_dir'],
  93. ['matcher_cache_class'],
  94. ];
  95. }
  96. /**
  97. * @dataProvider provideGeneratorOptionsPreventingCaching
  98. */
  99. public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
  100. {
  101. $this->router->setOption($option, null);
  102. $this->loader->expects($this->once())
  103. ->method('load')->with('routing.yml', null)
  104. ->will($this->returnValue(new RouteCollection()));
  105. $this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator());
  106. }
  107. public function provideGeneratorOptionsPreventingCaching()
  108. {
  109. return [
  110. ['cache_dir'],
  111. ['generator_cache_class'],
  112. ];
  113. }
  114. public function testMatchRequestWithUrlMatcherInterface()
  115. {
  116. $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\UrlMatcherInterface')->getMock();
  117. $matcher->expects($this->once())->method('match');
  118. $p = new \ReflectionProperty($this->router, 'matcher');
  119. $p->setAccessible(true);
  120. $p->setValue($this->router, $matcher);
  121. $this->router->matchRequest(Request::create('/'));
  122. }
  123. public function testMatchRequestWithRequestMatcherInterface()
  124. {
  125. $matcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
  126. $matcher->expects($this->once())->method('matchRequest');
  127. $p = new \ReflectionProperty($this->router, 'matcher');
  128. $p->setAccessible(true);
  129. $p->setValue($this->router, $matcher);
  130. $this->router->matchRequest(Request::create('/'));
  131. }
  132. }