RouteTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Tests\Component\Routing;
  11. use Symfony\Component\Routing\Route;
  12. class RouteTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'));
  17. $this->assertEquals('/{foo}', $route->getPattern(), '__construct() takes a pattern as its first argument');
  18. $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '__construct() takes defaults as its second argument');
  19. $this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '__construct() takes requirements as its third argument');
  20. $this->assertEquals('bar', $route->getOption('foo'), '__construct() takes options as its fourth argument');
  21. }
  22. public function testPattern()
  23. {
  24. $route = new Route('/{foo}');
  25. $route->setPattern('/{bar}');
  26. $this->assertEquals('/{bar}', $route->getPattern(), '->setPattern() sets the pattern');
  27. $route->setPattern('');
  28. $this->assertEquals('/', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed');
  29. $route->setPattern('bar');
  30. $this->assertEquals('/bar', $route->getPattern(), '->setPattern() adds a / at the beginning of the pattern if needed');
  31. $this->assertEquals($route, $route->setPattern(''), '->setPattern() implements a fluent interface');
  32. }
  33. public function testOptions()
  34. {
  35. $route = new Route('/{foo}');
  36. $route->setOptions(array('foo' => 'bar'));
  37. $this->assertEquals(array_merge(array(
  38. 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
  39. ), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
  40. $this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
  41. }
  42. /**
  43. * @covers Symfony\Component\Routing\Route::setDefaults
  44. * @covers Symfony\Component\Routing\Route::getDefaults
  45. * @covers Symfony\Component\Routing\Route::setDefault
  46. * @covers Symfony\Component\Routing\Route::getDefault
  47. */
  48. public function testDefaults()
  49. {
  50. $route = new Route('/{foo}');
  51. $route->setDefaults(array('foo' => 'bar'));
  52. $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
  53. $this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
  54. $route->setDefault('foo', 'bar');
  55. $this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
  56. $route->setDefault('foo2', 'bar2');
  57. $this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
  58. $this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not setted');
  59. $route->setDefault('_controller', $closure = function () { return 'Hello'; });
  60. $this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
  61. }
  62. public function testRequirements()
  63. {
  64. $route = new Route('/{foo}');
  65. $route->setRequirements(array('foo' => '\d+'));
  66. $this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
  67. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
  68. $this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
  69. $route->setRequirements(array('foo' => '^\d+$'));
  70. $this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the pattern');
  71. $this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
  72. }
  73. public function testRequirement()
  74. {
  75. $route = new Route('/{foo}');
  76. $route->setRequirement('foo', '^\d+$');
  77. $this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the pattern');
  78. }
  79. public function testCompile()
  80. {
  81. $route = new Route('/{foo}');
  82. $this->assertEquals('Symfony\\Component\\Routing\\CompiledRoute', get_class($compiled = $route->compile()), '->compile() returns a compiled route');
  83. $this->assertEquals($compiled, $route->compile(), '->compile() only compiled the route once');
  84. }
  85. }