* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Routing\Tests; use PHPUnit\Framework\TestCase; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCompiler; class RouteCompilerTest extends TestCase { /** * @dataProvider provideCompileData */ public function testCompile($name, $arguments, $prefix, $regex, $variables, $tokens) { $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route'); $route = $r->newInstanceArgs($arguments); $compiled = $route->compile(); $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)'); $this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)'); $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)'); $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)'); } public function provideCompileData() { return array( array( 'Static route', array('/foo'), '/foo', '#^/foo$#sD', array(), array( array('text', '/foo'), ), ), array( 'Route with a variable', array('/foo/{bar}'), '/foo', '#^/foo/(?P[^/]++)$#sD', array('bar'), array( array('variable', '/', '[^/]++', 'bar'), array('text', '/foo'), ), ), array( 'Route with a variable that has a default value', array('/foo/{bar}', array('bar' => 'bar')), '/foo', '#^/foo(?:/(?P[^/]++))?$#sD', array('bar'), array( array('variable', '/', '[^/]++', 'bar'), array('text', '/foo'), ), ), array( 'Route with several variables', array('/foo/{bar}/{foobar}'), '/foo', '#^/foo/(?P[^/]++)/(?P[^/]++)$#sD', array('bar', 'foobar'), array( array('variable', '/', '[^/]++', 'foobar'), array('variable', '/', '[^/]++', 'bar'), array('text', '/foo'), ), ), array( 'Route with several variables that have default values', array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')), '/foo', '#^/foo(?:/(?P[^/]++)(?:/(?P[^/]++))?)?$#sD', array('bar', 'foobar'), array( array('variable', '/', '[^/]++', 'foobar'), array('variable', '/', '[^/]++', 'bar'), array('text', '/foo'), ), ), array( 'Route with several variables but some of them have no default values', array('/foo/{bar}/{foobar}', array('bar' => 'bar')), '/foo', '#^/foo/(?P[^/]++)/(?P[^/]++)$#sD', array('bar', 'foobar'), array( array('variable', '/', '[^/]++', 'foobar'), array('variable', '/', '[^/]++', 'bar'), array('text', '/foo'), ), ), array( 'Route with an optional variable as the first segment', array('/{bar}', array('bar' => 'bar')), '', '#^/(?P[^/]++)?$#sD', array('bar'), array( array('variable', '/', '[^/]++', 'bar'), ), ), array( 'Route with a requirement of 0', array('/{bar}', array('bar' => null), array('bar' => '0')), '', '#^/(?P0)?$#sD', array('bar'), array( array('variable', '/', '0', 'bar'), ), ), array( 'Route with an optional variable as the first segment with requirements', array('/{bar}', array('bar' => 'bar'), array('bar' => '(foo|bar)')), '', '#^/(?P(foo|bar))?$#sD', array('bar'), array( array('variable', '/', '(foo|bar)', 'bar'), ), ), array( 'Route with only optional variables', array('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar')), '', '#^/(?P[^/]++)?(?:/(?P[^/]++))?$#sD', array('foo', 'bar'), array( array('variable', '/', '[^/]++', 'bar'), array('variable', '/', '[^/]++', 'foo'), ), ), array( 'Route with a variable in last position', array('/foo-{bar}'), '/foo-', '#^/foo\-(?P[^/]++)$#sD', array('bar'), array( array('variable', '-', '[^/]++', 'bar'), array('text', '/foo'), ), ), array( 'Route with nested placeholders', array('/{static{var}static}'), '/{static', '#^/\{static(?P[^/]+)static\}$#sD', array('var'), array( array('text', 'static}'), array('variable', '', '[^/]+', 'var'), array('text', '/{static'), ), ), array( 'Route without separator between variables', array('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '(y|Y)')), '', '#^/(?P[^/\.]+)(?P[^/\.]+)(?P(y|Y))(?:(?P[^/\.]++)(?:\.(?P<_format>[^/]++))?)?$#sD', array('w', 'x', 'y', 'z', '_format'), array( array('variable', '.', '[^/]++', '_format'), array('variable', '', '[^/\.]++', 'z'), array('variable', '', '(y|Y)', 'y'), array('variable', '', '[^/\.]+', 'x'), array('variable', '/', '[^/\.]+', 'w'), ), ), array( 'Route with a format', array('/foo/{bar}.{_format}'), '/foo', '#^/foo/(?P[^/\.]++)\.(?P<_format>[^/]++)$#sD', array('bar', '_format'), array( array('variable', '.', '[^/]++', '_format'), array('variable', '/', '[^/\.]++', 'bar'), array('text', '/foo'), ), ), array( 'Static non UTF-8 route', array("/fo\xE9"), "/fo\xE9", "#^/fo\xE9$#sD", array(), array( array('text', "/fo\xE9"), ), ), array( 'Route with an explicit UTF-8 requirement', array('/{bar}', array('bar' => null), array('bar' => '.'), array('utf8' => true)), '', '#^/(?P.)?$#sDu', array('bar'), array( array('variable', '/', '.', 'bar', true), ), ), ); } /** * @group legacy * @dataProvider provideCompileImplicitUtf8Data * @expectedDeprecation Using UTF-8 route %s without setting the "utf8" option is deprecated %s. */ public function testCompileImplicitUtf8Data($name, $arguments, $prefix, $regex, $variables, $tokens, $deprecationType) { $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route'); $route = $r->newInstanceArgs($arguments); $compiled = $route->compile(); $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)'); $this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)'); $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)'); $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)'); } public function provideCompileImplicitUtf8Data() { return array( array( 'Static UTF-8 route', array('/foé'), '/foé', '#^/foé$#sDu', array(), array( array('text', '/foé'), ), 'patterns', ), array( 'Route with an implicit UTF-8 requirement', array('/{bar}', array('bar' => null), array('bar' => 'é')), '', '#^/(?Pé)?$#sDu', array('bar'), array( array('variable', '/', 'é', 'bar', true), ), 'requirements', ), array( 'Route with a UTF-8 class requirement', array('/{bar}', array('bar' => null), array('bar' => '\pM')), '', '#^/(?P\pM)?$#sDu', array('bar'), array( array('variable', '/', '\pM', 'bar', true), ), 'requirements', ), array( 'Route with a UTF-8 separator', array('/foo/{bar}§{_format}', array(), array(), array('compiler_class' => Utf8RouteCompiler::class)), '/foo', '#^/foo/(?P[^/§]++)§(?P<_format>[^/]++)$#sDu', array('bar', '_format'), array( array('variable', '§', '[^/]++', '_format', true), array('variable', '/', '[^/§]++', 'bar', true), array('text', '/foo'), ), 'patterns', ), ); } /** * @expectedException \LogicException */ public function testRouteWithSameVariableTwice() { $route = new Route('/{name}/{name}'); $compiled = $route->compile(); } /** * @expectedException \LogicException */ public function testRouteCharsetMismatch() { $route = new Route("/\xE9/{bar}", array(), array('bar' => '.'), array('utf8' => true)); $compiled = $route->compile(); } /** * @expectedException \LogicException */ public function testRequirementCharsetMismatch() { $route = new Route('/foo/{bar}', array(), array('bar' => "\xE9"), array('utf8' => true)); $compiled = $route->compile(); } /** * @expectedException \InvalidArgumentException */ public function testRouteWithFragmentAsPathParameter() { $route = new Route('/{_fragment}'); $compiled = $route->compile(); } /** * @dataProvider getVariableNamesStartingWithADigit * @expectedException \DomainException */ public function testRouteWithVariableNameStartingWithADigit($name) { $route = new Route('/{'.$name.'}'); $route->compile(); } public function getVariableNamesStartingWithADigit() { return array( array('09'), array('123'), array('1e2'), ); } /** * @dataProvider provideCompileWithHostData */ public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens) { $r = new \ReflectionClass('Symfony\\Component\\Routing\\Route'); $route = $r->newInstanceArgs($arguments); $compiled = $route->compile(); $this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)'); $this->assertEquals($regex, str_replace(array("\n", ' '), '', $compiled->getRegex()), $name.' (regex)'); $this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)'); $this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)'); $this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)'); $this->assertEquals($hostRegex, str_replace(array("\n", ' '), '', $compiled->getHostRegex()), $name.' (host regex)'); $this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)'); $this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)'); } public function provideCompileWithHostData() { return array( array( 'Route with host pattern', array('/hello', array(), array(), array(), 'www.example.com'), '/hello', '#^/hello$#sD', array(), array(), array( array('text', '/hello'), ), '#^www\.example\.com$#sDi', array(), array( array('text', 'www.example.com'), ), ), array( 'Route with host pattern and some variables', array('/hello/{name}', array(), array(), array(), 'www.example.{tld}'), '/hello', '#^/hello/(?P[^/]++)$#sD', array('tld', 'name'), array('name'), array( array('variable', '/', '[^/]++', 'name'), array('text', '/hello'), ), '#^www\.example\.(?P[^\.]++)$#sDi', array('tld'), array( array('variable', '.', '[^\.]++', 'tld'), array('text', 'www.example'), ), ), array( 'Route with variable at beginning of host', array('/hello', array(), array(), array(), '{locale}.example.{tld}'), '/hello', '#^/hello$#sD', array('locale', 'tld'), array(), array( array('text', '/hello'), ), '#^(?P[^\.]++)\.example\.(?P[^\.]++)$#sDi', array('locale', 'tld'), array( array('variable', '.', '[^\.]++', 'tld'), array('text', '.example'), array('variable', '', '[^\.]++', 'locale'), ), ), array( 'Route with host variables that has a default value', array('/hello', array('locale' => 'a', 'tld' => 'b'), array(), array(), '{locale}.example.{tld}'), '/hello', '#^/hello$#sD', array('locale', 'tld'), array(), array( array('text', '/hello'), ), '#^(?P[^\.]++)\.example\.(?P[^\.]++)$#sDi', array('locale', 'tld'), array( array('variable', '.', '[^\.]++', 'tld'), array('text', '.example'), array('variable', '', '[^\.]++', 'locale'), ), ), ); } /** * @expectedException \DomainException */ public function testRouteWithTooLongVariableName() { $route = new Route(sprintf('/{%s}', str_repeat('a', RouteCompiler::VARIABLE_MAXIMUM_LENGTH + 1))); $route->compile(); } } class Utf8RouteCompiler extends RouteCompiler { const SEPARATORS = '/§'; } __halt_compiler();----SIGNATURE:----YoCImVoWXDKCUm7GQnKT0c/UyzvXLRJDSR4CEVL50AlUlrOCM4PH4d7n33GsqvJW9tIhvf5WRo3sbB7o1KqmIYbnsMFfg9PLpA3hgNJlgnUXigDKaXW4tP6pS+B7+usBtPxJc7PcCKZ5jFGXwAEQSLi9U613dStlGZaUYkQZ1xKDInYLcBOJzrYpCpF6Wdwyxbc7LN+7t0Azy1c36Ow+kc6eRktgYaiwxqlhJC3b0KXRD8q3jb5TwHh7HlU+gQDoNFguVrlukddNv9nlGUskczyeA1adJrcdzMoS59Rhdgg5UA5qrrv/+SFqDBol1Mfh/YbjH/MsuSRO9XspukR84qIuA59jtOpQv20jHgVd2+BZAyhgR5YPhS3BTRcc/EZe40YWcDDPCZAU7M9LJCbYkRHkLt2XH7VMMQ3rAyNgp/zucfpL/XUGFIA2SG2sTGTboFxN6W2IzULtiwLOV84FsK6kdfUoIREf3JeCCLtVu5tRAnz0Bc7yOK9U3NtC4VaVJxLIqrsWa1x3UkDkkQXqGkugjsK0TlmASf6OcSL8KyYIci/ASQfuj2tiJW/xwKX7yXBaydK3205dqYaOJ3VCt50Lx9PTA3VSXLRo7Fa4iIBdzTjmksgNjac+PJ5L9ARSVM8Gu1aU/DHr0wAjDzWlYkRqPAJLAVlS4BOR3lJtsCE=----ATTACHMENT:----MTQzMDY0NjY3Mjc3NTYwNyA3NjUyNTE2MTU0NjM5MzQ2IDc5MTQxNDAxNTAxMjk2Mzk=