RouteCompilerTest.php 3.8 KB

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