RouteTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. 'segment_separators' => array('/', '.'),
  39. 'text_regex' => '.+?',
  40. 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
  41. ), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
  42. $this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
  43. }
  44. /**
  45. * @covers Symfony\Component\Routing\Route::setDefaults
  46. * @covers Symfony\Component\Routing\Route::getDefaults
  47. * @covers Symfony\Component\Routing\Route::setDefault
  48. * @covers Symfony\Component\Routing\Route::getDefault
  49. */
  50. public function testDefaults()
  51. {
  52. $route = new Route('/:foo');
  53. $route->setDefaults(array('foo' => 'bar'));
  54. $this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
  55. $this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
  56. $route->setDefault('foo', 'bar');
  57. $this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
  58. $route->setDefault('foo2', 'bar2');
  59. $this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
  60. $this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not setted');
  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 testCompile()
  74. {
  75. $route = new Route('/:foo');
  76. $this->assertEquals('Symfony\\Component\\Routing\\CompiledRoute', get_class($compiled = $route->compile()), '->compile() returns a compiled route');
  77. $this->assertEquals($compiled, $route->compile(), '->compile() only compiled the route once');
  78. }
  79. }