RouteCompilerTest.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 RouteCompilerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider provideCompileData
  16. */
  17. public function testCompile($name, $arguments, $prefix, $regex, $variables, $tokens)
  18. {
  19. $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
  20. $route = $r->newInstanceArgs($arguments);
  21. $compiled = $route->compile();
  22. $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
  23. $this->assertEquals($regex, str_replace(array("\n", ' '), '', $compiled->getRegex()), $name.' (regex)');
  24. $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
  25. $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
  26. }
  27. public function provideCompileData()
  28. {
  29. return array(
  30. array(
  31. 'Static route',
  32. array('/foo'),
  33. '/foo', '#^/foo$#x', array(), array(
  34. array('text', '/foo'),
  35. )),
  36. array(
  37. 'Route with a variable',
  38. array('/foo/{bar}'),
  39. '/foo', '#^/foo/(?P<bar>[^/]+?)$#x', array('bar'), array(
  40. array('variable', '/', '[^/]+?', 'bar'),
  41. array('text', '/foo'),
  42. )),
  43. array(
  44. 'Route with a variable that has a default value',
  45. array('/foo/{bar}', array('bar' => 'bar')),
  46. '/foo', '#^/foo(?:/(?P<bar>[^/]+?))?$#x', array('bar'), array(
  47. array('variable', '/', '[^/]+?', 'bar'),
  48. array('text', '/foo'),
  49. )),
  50. array(
  51. 'Route with several variables',
  52. array('/foo/{bar}/{foobar}'),
  53. '/foo', '#^/foo/(?P<bar>[^/]+?)/(?P<foobar>[^/]+?)$#x', array('bar', 'foobar'), array(
  54. array('variable', '/', '[^/]+?', 'foobar'),
  55. array('variable', '/', '[^/]+?', 'bar'),
  56. array('text', '/foo'),
  57. )),
  58. array(
  59. 'Route with several variables that have default values',
  60. array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')),
  61. '/foo', '#^/foo(?:/(?P<bar>[^/]+?)(?:/(?P<foobar>[^/]+?))?)?$#x', array('bar', 'foobar'), array(
  62. array('variable', '/', '[^/]+?', 'foobar'),
  63. array('variable', '/', '[^/]+?', 'bar'),
  64. array('text', '/foo'),
  65. )),
  66. array(
  67. 'Route with several variables but some of them have no default values',
  68. array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
  69. '/foo', '#^/foo/(?P<bar>[^/]+?)/(?P<foobar>[^/]+?)$#x', array('bar', 'foobar'), array(
  70. array('variable', '/', '[^/]+?', 'foobar'),
  71. array('variable', '/', '[^/]+?', 'bar'),
  72. array('text', '/foo'),
  73. )),
  74. array(
  75. 'Route with an optional variable as the first segment',
  76. array('/{bar}', array('bar' => 'bar')),
  77. '', '#^/(?:(?P<bar>[^/]+?))?$#x', array('bar'), array(
  78. array('variable', '/', '[^/]+?', 'bar'),
  79. )),
  80. array(
  81. 'Route with an optional variable as the first segment with requirements',
  82. array('/{bar}', array('bar' => 'bar'), array('bar' => '(foo|bar)')),
  83. '', '#^/(?:(?P<bar>(foo|bar)))?$#x', array('bar'), array(
  84. array('variable', '/', '(foo|bar)', 'bar'),
  85. )),
  86. );
  87. }
  88. }