No Description

RouterTest.php 5.0KB

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