RouterTest.php 4.1 KB

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