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