Merge branch '2.8' into 3.4

* 2.8:
  [Intl] Fixed the broken link
  [Routing] Fix trailing slash redirection for non-safe verbs
  [Debug] Fix bad registration of exception handler, leading to mem leak
  [Form] Fixed empty data on expanded ChoiceType and FileType
This commit is contained in:
Robin Chalas 2018-02-03 01:57:06 +01:00
commit 9f64a0fbd9
19 changed files with 415 additions and 104 deletions

View File

@ -136,9 +136,20 @@ class ErrorHandler
} }
if (!$replace && $prev) { if (!$replace && $prev) {
restore_error_handler(); restore_error_handler();
$handlerIsRegistered = is_array($prev) && $handler === $prev[0];
} else {
$handlerIsRegistered = true;
} }
if (is_array($prev = set_exception_handler(array($handler, 'handleException'))) && $prev[0] === $handler) { if (is_array($prev = set_exception_handler(array($handler, 'handleException'))) && $prev[0] instanceof self) {
restore_exception_handler(); restore_exception_handler();
if (!$handlerIsRegistered) {
$handler = $prev[0];
} elseif ($handler !== $prev[0] && $replace) {
set_exception_handler(array($handler, 'handleException'));
$p = $prev[0]->setExceptionHandler(null);
$handler->setExceptionHandler($p);
$prev[0]->setExceptionHandler($p);
}
} else { } else {
$handler->setExceptionHandler($prev); $handler->setExceptionHandler($prev);
} }

View File

@ -35,7 +35,7 @@ class ErrorHandlerTest extends TestCase
$newHandler = new ErrorHandler(); $newHandler = new ErrorHandler();
$this->assertSame($newHandler, ErrorHandler::register($newHandler, false)); $this->assertSame($handler, ErrorHandler::register($newHandler, false));
$h = set_error_handler('var_dump'); $h = set_error_handler('var_dump');
restore_error_handler(); restore_error_handler();
$this->assertSame(array($handler, 'handleError'), $h); $this->assertSame(array($handler, 'handleError'), $h);

View File

@ -31,7 +31,6 @@ use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener; use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer; use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer; use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
use Symfony\Component\Form\Util\FormUtil;
use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
@ -87,12 +86,12 @@ class ChoiceType extends AbstractType
$form = $event->getForm(); $form = $event->getForm();
$data = $event->getData(); $data = $event->getData();
// Since the type always use mapper an empty array will not be
// considered as empty in Form::submit(), we need to evaluate
// empty data here so its value is submitted to sub forms
if (null === $data) { if (null === $data) {
$emptyData = $form->getConfig()->getEmptyData(); $emptyData = $form->getConfig()->getEmptyData();
$data = $emptyData instanceof \Closure ? $emptyData($form, $data) : $emptyData;
if (false === FormUtil::isEmpty($emptyData) && array() !== $emptyData) {
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
}
} }
// Convert the submitted data to a string, if scalar, before // Convert the submitted data to a string, if scalar, before

View File

@ -27,10 +27,10 @@ class FileType extends AbstractType
*/ */
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
// Ensure that submitted data is always an uploaded file or an array of some
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) { $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
$form = $event->getForm(); $form = $event->getForm();
$requestHandler = $form->getConfig()->getRequestHandler(); $requestHandler = $form->getConfig()->getRequestHandler();
$data = null;
if ($options['multiple']) { if ($options['multiple']) {
$data = array(); $data = array();
@ -46,19 +46,16 @@ class FileType extends AbstractType
} }
} }
// submitted data for an input file (not required) without choosing any file // Since the array is never considered empty in the view data format
if (array(null) === $data || array() === $data) { // on submission, we need to evaluate the configured empty data here
if (array() === $data) {
$emptyData = $form->getConfig()->getEmptyData(); $emptyData = $form->getConfig()->getEmptyData();
$data = $emptyData instanceof \Closure ? $emptyData($form, $data) : $emptyData;
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
} }
$event->setData($data); $event->setData($data);
} elseif (!$requestHandler->isFileUpload($event->getData())) { } elseif (!$requestHandler->isFileUpload($event->getData())) {
$emptyData = $form->getConfig()->getEmptyData(); $event->setData(null);
$data = is_callable($emptyData) ? call_user_func($emptyData, $form, $data) : $emptyData;
$event->setData($data);
} }
}); });
} }

View File

@ -598,6 +598,20 @@ class ChoiceTypeTest extends BaseTypeTest
$this->assertSame('test', $form->getData()); $this->assertSame('test', $form->getData());
} }
public function testSubmitSingleChoiceWithEmptyDataAndInitialData()
{
$form = $this->factory->create(static::TESTED_TYPE, 'initial', array(
'multiple' => false,
'expanded' => false,
'choices' => array('initial', 'test'),
'empty_data' => 'test',
));
$form->submit(null);
$this->assertSame('test', $form->getData());
}
public function testSubmitMultipleChoiceWithEmptyData() public function testSubmitMultipleChoiceWithEmptyData()
{ {
$form = $this->factory->create(static::TESTED_TYPE, null, array( $form = $this->factory->create(static::TESTED_TYPE, null, array(
@ -612,6 +626,34 @@ class ChoiceTypeTest extends BaseTypeTest
$this->assertSame(array('test'), $form->getData()); $this->assertSame(array('test'), $form->getData());
} }
public function testSubmitMultipleChoiceWithEmptyDataAndInitialEmptyArray()
{
$form = $this->factory->create(static::TESTED_TYPE, array(), array(
'multiple' => true,
'expanded' => false,
'choices' => array('test'),
'empty_data' => array('test'),
));
$form->submit(null);
$this->assertSame(array('test'), $form->getData());
}
public function testSubmitMultipleChoiceWithEmptyDataAndInitialData()
{
$form = $this->factory->create(static::TESTED_TYPE, array('initial'), array(
'multiple' => true,
'expanded' => false,
'choices' => array('initial', 'test'),
'empty_data' => array('test'),
));
$form->submit(null);
$this->assertSame(array('test'), $form->getData());
}
public function testSubmitSingleChoiceExpandedWithEmptyData() public function testSubmitSingleChoiceExpandedWithEmptyData()
{ {
$form = $this->factory->create(static::TESTED_TYPE, null, array( $form = $this->factory->create(static::TESTED_TYPE, null, array(
@ -626,6 +668,20 @@ class ChoiceTypeTest extends BaseTypeTest
$this->assertSame('test', $form->getData()); $this->assertSame('test', $form->getData());
} }
public function testSubmitSingleChoiceExpandedWithEmptyDataAndInitialData()
{
$form = $this->factory->create(static::TESTED_TYPE, 'initial', array(
'multiple' => false,
'expanded' => true,
'choices' => array('initial', 'test'),
'empty_data' => 'test',
));
$form->submit(null);
$this->assertSame('test', $form->getData());
}
public function testSubmitMultipleChoiceExpandedWithEmptyData() public function testSubmitMultipleChoiceExpandedWithEmptyData()
{ {
$form = $this->factory->create(static::TESTED_TYPE, null, array( $form = $this->factory->create(static::TESTED_TYPE, null, array(
@ -640,6 +696,49 @@ class ChoiceTypeTest extends BaseTypeTest
$this->assertSame(array('test'), $form->getData()); $this->assertSame(array('test'), $form->getData());
} }
public function testSubmitMultipleChoiceExpandedWithEmptyDataAndInitialEmptyArray()
{
$form = $this->factory->create(static::TESTED_TYPE, array(), array(
'multiple' => true,
'expanded' => true,
'choices' => array('test'),
'empty_data' => array('test'),
));
$form->submit(null);
$this->assertSame(array('test'), $form->getData());
}
public function testSubmitMultipleChoiceExpandedWithEmptyDataAndInitialData()
{
$form = $this->factory->create(static::TESTED_TYPE, array('init'), array(
'multiple' => true,
'expanded' => true,
'choices' => array('init', 'test'),
'empty_data' => array('test'),
));
$form->submit(null);
$this->assertSame(array('test'), $form->getData());
}
/**
* @group legacy
*/
public function testLegacyNullChoices()
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'multiple' => false,
'expanded' => false,
'choices' => null,
));
$this->assertNull($form->getConfig()->getOption('choices'));
$this->assertFalse($form->getConfig()->getOption('multiple'));
$this->assertFalse($form->getConfig()->getOption('expanded'));
}
public function testSubmitMultipleNonExpanded() public function testSubmitMultipleNonExpanded()
{ {
$form = $this->factory->create(static::TESTED_TYPE, null, array( $form = $this->factory->create(static::TESTED_TYPE, null, array(

View File

@ -102,7 +102,7 @@ EOF;
\$pathinfo = rawurldecode(\$rawPathinfo); \$pathinfo = rawurldecode(\$rawPathinfo);
\$trimmedPathinfo = rtrim(\$pathinfo, '/'); \$trimmedPathinfo = rtrim(\$pathinfo, '/');
\$context = \$this->context; \$context = \$this->context;
\$request = \$this->request; \$request = \$this->request ?: \$this->createRequest(\$pathinfo);
\$requestMethod = \$canonicalMethod = \$context->getMethod(); \$requestMethod = \$canonicalMethod = \$context->getMethod();
\$scheme = \$context->getScheme(); \$scheme = \$context->getScheme();
@ -110,7 +110,6 @@ EOF;
\$canonicalMethod = 'GET'; \$canonicalMethod = 'GET';
} }
$code $code
throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException(); throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();
@ -361,7 +360,11 @@ EOF;
if ($hasTrailingSlash) { if ($hasTrailingSlash) {
$code .= <<<EOF $code .= <<<EOF
if (substr(\$pathinfo, -1) !== '/') { if ('/' === substr(\$pathinfo, -1)) {
// no-op
} elseif (!in_array(\$this->context->getMethod(), array('HEAD', 'GET'))) {
goto $gotoname;
} else {
return array_replace(\$ret, \$this->redirect(\$rawPathinfo.'/', '$name')); return array_replace(\$ret, \$this->redirect(\$rawPathinfo.'/', '$name'));
} }
@ -391,7 +394,7 @@ EOF;
} }
$code .= " }\n"; $code .= " }\n";
if ($methods) { if ($methods || $hasTrailingSlash) {
$code .= " $gotoname:\n"; $code .= " $gotoname:\n";
} }

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,7 +29,6 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
if ('/' === $pathinfo) { if ('/' === $pathinfo) {
throw new Symfony\Component\Routing\Exception\NoConfigurationException(); throw new Symfony\Component\Routing\Exception\NoConfigurationException();
} }

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,7 +29,6 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
if (0 === strpos($pathinfo, '/foo')) { if (0 === strpos($pathinfo, '/foo')) {
// foo // foo
if (preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) { if (preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) {

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,7 +29,6 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
if (0 === strpos($pathinfo, '/foo')) { if (0 === strpos($pathinfo, '/foo')) {
// foo // foo
if (preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) { if (preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) {
@ -83,24 +82,34 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// baz3 // baz3
if ('/test/baz3' === $trimmedPathinfo) { if ('/test/baz3' === $trimmedPathinfo) {
$ret = array('_route' => 'baz3'); $ret = array('_route' => 'baz3');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_baz3;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'baz3')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'baz3'));
} }
return $ret; return $ret;
} }
not_baz3:
} }
// baz4 // baz4
if (preg_match('#^/test/(?P<foo>[^/]++)/?$#s', $pathinfo, $matches)) { if (preg_match('#^/test/(?P<foo>[^/]++)/?$#s', $pathinfo, $matches)) {
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ()); $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_baz4;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'baz4')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'baz4'));
} }
return $ret; return $ret;
} }
not_baz4:
// baz5 // baz5
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) { if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
@ -179,12 +188,17 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// hey // hey
if ('/multi/hey' === $trimmedPathinfo) { if ('/multi/hey' === $trimmedPathinfo) {
$ret = array('_route' => 'hey'); $ret = array('_route' => 'hey');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_hey;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'hey')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'hey'));
} }
return $ret; return $ret;
} }
not_hey:
// overridden2 // overridden2
if ('/multi/new' === $pathinfo) { if ('/multi/new' === $pathinfo) {

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,7 +29,6 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
if (0 === strpos($pathinfo, '/rootprefix')) { if (0 === strpos($pathinfo, '/rootprefix')) {
// static // static
if ('/rootprefix/test' === $pathinfo) { if ('/rootprefix/test' === $pathinfo) {

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,7 +29,6 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
// just_head // just_head
if ('/just_head' === $pathinfo) { if ('/just_head' === $pathinfo) {
if ('HEAD' !== $requestMethod) { if ('HEAD' !== $requestMethod) {

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,7 +29,6 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
if (0 === strpos($pathinfo, '/a')) { if (0 === strpos($pathinfo, '/a')) {
// a_first // a_first
if ('/a/11' === $pathinfo) { if ('/a/11' === $pathinfo) {
@ -57,32 +56,47 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// a_fourth // a_fourth
if ('/a/44' === $trimmedPathinfo) { if ('/a/44' === $trimmedPathinfo) {
$ret = array('_route' => 'a_fourth'); $ret = array('_route' => 'a_fourth');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_a_fourth;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'a_fourth')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'a_fourth'));
} }
return $ret; return $ret;
} }
not_a_fourth:
// a_fifth // a_fifth
if ('/a/55' === $trimmedPathinfo) { if ('/a/55' === $trimmedPathinfo) {
$ret = array('_route' => 'a_fifth'); $ret = array('_route' => 'a_fifth');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_a_fifth;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'a_fifth')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'a_fifth'));
} }
return $ret; return $ret;
} }
not_a_fifth:
// a_sixth // a_sixth
if ('/a/66' === $trimmedPathinfo) { if ('/a/66' === $trimmedPathinfo) {
$ret = array('_route' => 'a_sixth'); $ret = array('_route' => 'a_sixth');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_a_sixth;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'a_sixth')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'a_sixth'));
} }
return $ret; return $ret;
} }
not_a_sixth:
} }
@ -95,32 +109,47 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// nested_a // nested_a
if ('/nested/group/a' === $trimmedPathinfo) { if ('/nested/group/a' === $trimmedPathinfo) {
$ret = array('_route' => 'nested_a'); $ret = array('_route' => 'nested_a');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_nested_a;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'nested_a')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'nested_a'));
} }
return $ret; return $ret;
} }
not_nested_a:
// nested_b // nested_b
if ('/nested/group/b' === $trimmedPathinfo) { if ('/nested/group/b' === $trimmedPathinfo) {
$ret = array('_route' => 'nested_b'); $ret = array('_route' => 'nested_b');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_nested_b;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'nested_b')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'nested_b'));
} }
return $ret; return $ret;
} }
not_nested_b:
// nested_c // nested_c
if ('/nested/group/c' === $trimmedPathinfo) { if ('/nested/group/c' === $trimmedPathinfo) {
$ret = array('_route' => 'nested_c'); $ret = array('_route' => 'nested_c');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_nested_c;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'nested_c')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'nested_c'));
} }
return $ret; return $ret;
} }
not_nested_c:
} }
@ -128,32 +157,47 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// slashed_a // slashed_a
if ('/slashed/group' === $trimmedPathinfo) { if ('/slashed/group' === $trimmedPathinfo) {
$ret = array('_route' => 'slashed_a'); $ret = array('_route' => 'slashed_a');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_slashed_a;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'slashed_a')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'slashed_a'));
} }
return $ret; return $ret;
} }
not_slashed_a:
// slashed_b // slashed_b
if ('/slashed/group/b' === $trimmedPathinfo) { if ('/slashed/group/b' === $trimmedPathinfo) {
$ret = array('_route' => 'slashed_b'); $ret = array('_route' => 'slashed_b');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_slashed_b;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'slashed_b')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'slashed_b'));
} }
return $ret; return $ret;
} }
not_slashed_b:
// slashed_c // slashed_c
if ('/slashed/group/c' === $trimmedPathinfo) { if ('/slashed/group/c' === $trimmedPathinfo) {
$ret = array('_route' => 'slashed_c'); $ret = array('_route' => 'slashed_c');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_slashed_c;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'slashed_c')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'slashed_c'));
} }
return $ret; return $ret;
} }
not_slashed_c:
} }

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,7 +29,6 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
if (0 === strpos($pathinfo, '/trailing/simple')) { if (0 === strpos($pathinfo, '/trailing/simple')) {
// simple_trailing_slash_no_methods // simple_trailing_slash_no_methods
if ('/trailing/simple/no-methods/' === $pathinfo) { if ('/trailing/simple/no-methods/' === $pathinfo) {

View File

@ -21,7 +21,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$pathinfo = rawurldecode($rawPathinfo); $pathinfo = rawurldecode($rawPathinfo);
$trimmedPathinfo = rtrim($pathinfo, '/'); $trimmedPathinfo = rtrim($pathinfo, '/');
$context = $this->context; $context = $this->context;
$request = $this->request; $request = $this->request ?: $this->createRequest($pathinfo);
$requestMethod = $canonicalMethod = $context->getMethod(); $requestMethod = $canonicalMethod = $context->getMethod();
$scheme = $context->getScheme(); $scheme = $context->getScheme();
@ -29,17 +29,21 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
$canonicalMethod = 'GET'; $canonicalMethod = 'GET';
} }
if (0 === strpos($pathinfo, '/trailing/simple')) { if (0 === strpos($pathinfo, '/trailing/simple')) {
// simple_trailing_slash_no_methods // simple_trailing_slash_no_methods
if ('/trailing/simple/no-methods' === $trimmedPathinfo) { if ('/trailing/simple/no-methods' === $trimmedPathinfo) {
$ret = array('_route' => 'simple_trailing_slash_no_methods'); $ret = array('_route' => 'simple_trailing_slash_no_methods');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_simple_trailing_slash_no_methods;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'simple_trailing_slash_no_methods')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'simple_trailing_slash_no_methods'));
} }
return $ret; return $ret;
} }
not_simple_trailing_slash_no_methods:
// simple_trailing_slash_GET_method // simple_trailing_slash_GET_method
if ('/trailing/simple/get-method' === $trimmedPathinfo) { if ('/trailing/simple/get-method' === $trimmedPathinfo) {
@ -49,7 +53,11 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
} }
$ret = array('_route' => 'simple_trailing_slash_GET_method'); $ret = array('_route' => 'simple_trailing_slash_GET_method');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_simple_trailing_slash_GET_method;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'simple_trailing_slash_GET_method')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'simple_trailing_slash_GET_method'));
} }
@ -65,7 +73,11 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
} }
$ret = array('_route' => 'simple_trailing_slash_HEAD_method'); $ret = array('_route' => 'simple_trailing_slash_HEAD_method');
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_simple_trailing_slash_HEAD_method;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'simple_trailing_slash_HEAD_method')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'simple_trailing_slash_HEAD_method'));
} }
@ -90,12 +102,17 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
// regex_trailing_slash_no_methods // regex_trailing_slash_no_methods
if (0 === strpos($pathinfo, '/trailing/regex/no-methods') && preg_match('#^/trailing/regex/no\\-methods/(?P<param>[^/]++)/?$#s', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/trailing/regex/no-methods') && preg_match('#^/trailing/regex/no\\-methods/(?P<param>[^/]++)/?$#s', $pathinfo, $matches)) {
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_no_methods')), array ()); $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_no_methods')), array ());
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_regex_trailing_slash_no_methods;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'regex_trailing_slash_no_methods')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'regex_trailing_slash_no_methods'));
} }
return $ret; return $ret;
} }
not_regex_trailing_slash_no_methods:
// regex_trailing_slash_GET_method // regex_trailing_slash_GET_method
if (0 === strpos($pathinfo, '/trailing/regex/get-method') && preg_match('#^/trailing/regex/get\\-method/(?P<param>[^/]++)/?$#s', $pathinfo, $matches)) { if (0 === strpos($pathinfo, '/trailing/regex/get-method') && preg_match('#^/trailing/regex/get\\-method/(?P<param>[^/]++)/?$#s', $pathinfo, $matches)) {
@ -105,7 +122,11 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
} }
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_GET_method')), array ()); $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_GET_method')), array ());
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_regex_trailing_slash_GET_method;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'regex_trailing_slash_GET_method')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'regex_trailing_slash_GET_method'));
} }
@ -121,7 +142,11 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\Redirec
} }
$ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_HEAD_method')), array ()); $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'regex_trailing_slash_HEAD_method')), array ());
if (substr($pathinfo, -1) !== '/') { if ('/' === substr($pathinfo, -1)) {
// no-op
} elseif (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) {
goto not_regex_trailing_slash_HEAD_method;
} else {
return array_replace($ret, $this->redirect($rawPathinfo.'/', 'regex_trailing_slash_HEAD_method')); return array_replace($ret, $this->redirect($rawPathinfo.'/', 'regex_trailing_slash_HEAD_method'));
} }

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher;
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
class DumpedRedirectableUrlMatcherTest extends RedirectableUrlMatcherTest
{
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
static $i = 0;
$class = 'DumpedRedirectableUrlMatcher'.++$i;
$dumper = new PhpMatcherDumper($routes);
$dumpedRoutes = eval('?>'.$dumper->dump(array('class' => $class, 'base_class' => 'Symfony\Component\Routing\Tests\Matcher\TestDumpedRedirectableUrlMatcher')));
return $this->getMockBuilder($class)
->setConstructorArgs(array($context ?: new RequestContext()))
->setMethods(array('redirect'))
->getMock();
}
}
class TestDumpedRedirectableUrlMatcher extends UrlMatcher implements RedirectableUrlMatcherInterface
{
public function redirect($path, $route, $scheme = null)
{
return array();
}
}

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher;
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
class DumpedUrlMatcherTest extends UrlMatcherTest
{
/**
* @expectedException \LogicException
* @expectedExceptionMessage The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.
*/
public function testSchemeRequirement()
{
parent::testSchemeRequirement();
}
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
static $i = 0;
$class = 'DumpedUrlMatcher'.++$i;
$dumper = new PhpMatcherDumper($routes);
$dumpedRoutes = eval('?>'.$dumper->dump(array('class' => $class)));
return new $class($context ?: new RequestContext());
}
}

View File

@ -11,19 +11,18 @@
namespace Symfony\Component\Routing\Tests\Matcher; namespace Symfony\Component\Routing\Tests\Matcher;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
class RedirectableUrlMatcherTest extends TestCase class RedirectableUrlMatcherTest extends UrlMatcherTest
{ {
public function testRedirectWhenNoSlash() public function testRedirectWhenNoSlash()
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo/')); $coll->add('foo', new Route('/foo/'));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); $matcher = $this->getUrlMatcher($coll);
$matcher->expects($this->once())->method('redirect')->will($this->returnValue(array())); $matcher->expects($this->once())->method('redirect')->will($this->returnValue(array()));
$matcher->match('/foo'); $matcher->match('/foo');
} }
@ -38,7 +37,7 @@ class RedirectableUrlMatcherTest extends TestCase
$context = new RequestContext(); $context = new RequestContext();
$context->setMethod('POST'); $context->setMethod('POST');
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, $context)); $matcher = $this->getUrlMatcher($coll, $context);
$matcher->match('/foo'); $matcher->match('/foo');
} }
@ -47,7 +46,7 @@ class RedirectableUrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array('FTP', 'HTTPS'))); $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('FTP', 'HTTPS')));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); $matcher = $this->getUrlMatcher($coll);
$matcher $matcher
->expects($this->once()) ->expects($this->once())
->method('redirect') ->method('redirect')
@ -62,7 +61,7 @@ class RedirectableUrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https', 'http'))); $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https', 'http')));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); $matcher = $this->getUrlMatcher($coll);
$matcher $matcher
->expects($this->never()) ->expects($this->never())
->method('redirect'); ->method('redirect');
@ -74,7 +73,7 @@ class RedirectableUrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https'))); $coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https')));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); $matcher = $this->getUrlMatcher($coll);
$matcher $matcher
->expects($this->once()) ->expects($this->once())
->method('redirect') ->method('redirect')
@ -89,7 +88,7 @@ class RedirectableUrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{bar}/')); $coll->add('foo', new Route('/foo/{bar}/'));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); $matcher = $this->getUrlMatcher($coll);
$matcher $matcher
->expects($this->once()) ->expects($this->once())
->method('redirect') ->method('redirect')
@ -104,8 +103,22 @@ class RedirectableUrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo:bar/')); $coll->add('foo', new Route('/foo:bar/'));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); $matcher = $this->getUrlMatcher($coll);
$matcher->expects($this->once())->method('redirect')->with('/foo%3Abar/')->willReturn(array()); $matcher->expects($this->once())->method('redirect')->with('/foo%3Abar/')->willReturn(array());
$matcher->match('/foo%3Abar'); $matcher->match('/foo%3Abar');
} }
public function testSchemeRequirement()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
$matcher = $this->getUrlMatcher($coll, new RequestContext());
$matcher->expects($this->once())->method('redirect')->with('/foo', 'foo', 'https')->willReturn(array('_route' => 'foo'));
$this->assertSame(array('_route' => 'foo'), $matcher->match('/foo'));
}
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($routes, $context ?: new RequestContext()));
}
} }

View File

@ -26,7 +26,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo')); $coll->add('foo', new Route('/foo'));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
} }
@ -35,7 +35,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('post'))); $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('post')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
try { try {
$matcher->match('/foo'); $matcher->match('/foo');
@ -50,7 +50,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get'))); $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get')));
$matcher = new UrlMatcher($coll, new RequestContext('', 'head')); $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'head'));
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
} }
@ -60,7 +60,7 @@ class UrlMatcherTest extends TestCase
$coll->add('foo1', new Route('/foo', array(), array(), array(), '', array(), array('post'))); $coll->add('foo1', new Route('/foo', array(), array(), array(), '', array(), array('post')));
$coll->add('foo2', new Route('/foo', array(), array(), array(), '', array(), array('put', 'delete'))); $coll->add('foo2', new Route('/foo', array(), array(), array(), '', array(), array('put', 'delete')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
try { try {
$matcher->match('/foo'); $matcher->match('/foo');
@ -75,7 +75,7 @@ class UrlMatcherTest extends TestCase
// test the patterns are matched and parameters are returned // test the patterns are matched and parameters are returned
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}')); $collection->add('foo', new Route('/foo/{bar}'));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
try { try {
$matcher->match('/no-match'); $matcher->match('/no-match');
$this->fail(); $this->fail();
@ -86,17 +86,17 @@ class UrlMatcherTest extends TestCase
// test that defaults are merged // test that defaults are merged
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}', array('def' => 'test'))); $collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz')); $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
// test that route "method" is ignored if no method is given in the context // test that route "method" is ignored if no method is given in the context
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get', 'head'))); $collection->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get', 'head')));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
// route does not match with POST method context // route does not match with POST method context
$matcher = new UrlMatcher($collection, new RequestContext('', 'post')); $matcher = $this->getUrlMatcher($collection, new RequestContext('', 'post'));
try { try {
$matcher->match('/foo'); $matcher->match('/foo');
$this->fail(); $this->fail();
@ -104,28 +104,28 @@ class UrlMatcherTest extends TestCase
} }
// route does match with GET or HEAD method context // route does match with GET or HEAD method context
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
$matcher = new UrlMatcher($collection, new RequestContext('', 'head')); $matcher = $this->getUrlMatcher($collection, new RequestContext('', 'head'));
$this->assertInternalType('array', $matcher->match('/foo')); $this->assertInternalType('array', $matcher->match('/foo'));
// route with an optional variable as the first segment // route with an optional variable as the first segment
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar'))); $collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar')));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo')); $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo'));
$this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo')); $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo'));
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar'))); $collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar')));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo')); $this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/')); $this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
// route with only optional variables // route with only optional variables
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('bar', new Route('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar'), array())); $collection->add('bar', new Route('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar'), array()));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'), $matcher->match('/')); $this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'), $matcher->match('/'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'), $matcher->match('/a')); $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'), $matcher->match('/a'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'b'), $matcher->match('/a/b')); $this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'b'), $matcher->match('/a/b'));
@ -138,7 +138,7 @@ class UrlMatcherTest extends TestCase
$collection->addPrefix('/b'); $collection->addPrefix('/b');
$collection->addPrefix('/a'); $collection->addPrefix('/a');
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo')); $this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
} }
@ -149,7 +149,7 @@ class UrlMatcherTest extends TestCase
$collection->addPrefix('/b'); $collection->addPrefix('/b');
$collection->addPrefix('/{_locale}'); $collection->addPrefix('/{_locale}');
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo')); $this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
} }
@ -158,7 +158,7 @@ class UrlMatcherTest extends TestCase
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('$péß^a|', new Route('/bar')); $collection->add('$péß^a|', new Route('/bar'));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar')); $this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
} }
@ -168,7 +168,7 @@ class UrlMatcherTest extends TestCase
$chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-'; $chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
$collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+'), array('utf8' => true))); $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+'), array('utf8' => true)));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.rawurlencode($chars).'/bar')); $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.rawurlencode($chars).'/bar'));
$this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25')).'/bar')); $this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25')).'/bar'));
} }
@ -178,7 +178,7 @@ class UrlMatcherTest extends TestCase
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+'))); $collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched'); $this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
} }
@ -192,7 +192,7 @@ class UrlMatcherTest extends TestCase
$collection->addCollection($collection1); $collection->addCollection($collection1);
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
$this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1')); $this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1'));
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException'); $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Routing\Exception\ResourceNotFoundException');
@ -205,12 +205,12 @@ class UrlMatcherTest extends TestCase
$coll->add('foo', new Route('/foo/{foo}')); $coll->add('foo', new Route('/foo/{foo}'));
$coll->add('bar', new Route('/foo/bar/{foo}')); $coll->add('bar', new Route('/foo/bar/{foo}'));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar')); $this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
$collection = new RouteCollection(); $collection = new RouteCollection();
$collection->add('foo', new Route('/{bar}')); $collection->add('foo', new Route('/{bar}'));
$matcher = new UrlMatcher($collection, new RequestContext()); $matcher = $this->getUrlMatcher($collection);
try { try {
$matcher->match('/'); $matcher->match('/');
$this->fail(); $this->fail();
@ -223,7 +223,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html'))); $coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml')); $this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml'));
} }
@ -232,7 +232,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+'))); $coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-')); $this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-'));
} }
@ -241,7 +241,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => 'y|Y'))); $coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => 'y|Y')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
// 'w' eagerly matches as much as possible and the other variables match the remaining chars. // 'w' eagerly matches as much as possible and the other variables match the remaining chars.
// This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement. // This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement.
// Otherwise they would also consume '.xml' and _format would never match as it's an optional variable. // Otherwise they would also consume '.xml' and _format would never match as it's an optional variable.
@ -260,7 +260,7 @@ class UrlMatcherTest extends TestCase
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/get{what}', array('what' => 'All'))); $coll->add('test', new Route('/get{what}', array('what' => 'All')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get')); $this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get'));
$this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites')); $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites'));
@ -275,7 +275,7 @@ class UrlMatcherTest extends TestCase
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/get{what}Suffix')); $coll->add('test', new Route('/get{what}Suffix'));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix')); $this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix'));
} }
@ -284,7 +284,7 @@ class UrlMatcherTest extends TestCase
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}')); $coll->add('test', new Route('/{page}.{_format}'));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html')); $this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html'));
} }
@ -296,7 +296,7 @@ class UrlMatcherTest extends TestCase
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}')); $coll->add('test', new Route('/{page}.{_format}'));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$matcher->match('/index.sl/ash'); $matcher->match('/index.sl/ash');
} }
@ -308,7 +308,7 @@ class UrlMatcherTest extends TestCase
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml'))); $coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$matcher->match('/do.t.html'); $matcher->match('/do.t.html');
} }
@ -320,7 +320,7 @@ class UrlMatcherTest extends TestCase
{ {
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https'))); $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$matcher->match('/foo'); $matcher->match('/foo');
} }
@ -333,7 +333,7 @@ class UrlMatcherTest extends TestCase
$route = new Route('/foo'); $route = new Route('/foo');
$route->setCondition('context.getMethod() == "POST"'); $route->setCondition('context.getMethod() == "POST"');
$coll->add('foo', $route); $coll->add('foo', $route);
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$matcher->match('/foo'); $matcher->match('/foo');
} }
@ -343,7 +343,7 @@ class UrlMatcherTest extends TestCase
$route = new Route('/foo/{bar}'); $route = new Route('/foo/{bar}');
$route->setCondition('request.getBaseUrl() == "/sub/front.php" and request.getPathInfo() == "/foo/bar"'); $route->setCondition('request.getBaseUrl() == "/sub/front.php" and request.getPathInfo() == "/foo/bar"');
$coll->add('foo', $route); $coll->add('foo', $route);
$matcher = new UrlMatcher($coll, new RequestContext('/sub/front.php')); $matcher = $this->getUrlMatcher($coll, new RequestContext('/sub/front.php'));
$this->assertEquals(array('bar' => 'bar', '_route' => 'foo'), $matcher->match('/foo/bar')); $this->assertEquals(array('bar' => 'bar', '_route' => 'foo'), $matcher->match('/foo/bar'));
} }
@ -352,7 +352,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}')); $coll->add('foo', new Route('/foo/{foo}'));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523')); $this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
} }
@ -368,7 +368,7 @@ class UrlMatcherTest extends TestCase
$coll->addCollection($subColl); $coll->addCollection($subColl);
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('_route' => 'bar'), $matcher->match('/new')); $this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
} }
@ -377,7 +377,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com')); $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com')); $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar')); $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
} }
@ -388,10 +388,10 @@ class UrlMatcherTest extends TestCase
$coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net')); $coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
$coll->setHost('{locale}.example.com'); $coll->setHost('{locale}.example.com');
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com')); $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar')); $this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com')); $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar')); $this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
} }
@ -403,7 +403,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com')); $coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com')); $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
$matcher->match('/foo/bar'); $matcher->match('/foo/bar');
} }
@ -415,7 +415,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE'))); $coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE')));
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = $this->getUrlMatcher($coll);
$matcher->match('/en'); $matcher->match('/en');
} }
@ -424,7 +424,7 @@ class UrlMatcherTest extends TestCase
$coll = new RouteCollection(); $coll = new RouteCollection();
$coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com')); $coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com')); $matcher = $this->getUrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/')); $this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));
} }
@ -438,4 +438,33 @@ class UrlMatcherTest extends TestCase
$matcher = new UrlMatcher($coll, new RequestContext()); $matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/'); $matcher->match('/');
} }
public function testNestedCollections()
{
$coll = new RouteCollection();
$subColl = new RouteCollection();
$subColl->add('a', new Route('/a'));
$subColl->add('b', new Route('/b'));
$subColl->add('c', new Route('/c'));
$subColl->addPrefix('/p');
$coll->addCollection($subColl);
$coll->add('baz', new Route('/{baz}'));
$subColl = new RouteCollection();
$subColl->add('buz', new Route('/buz'));
$subColl->addPrefix('/prefix');
$coll->addCollection($subColl);
$matcher = $this->getUrlMatcher($coll);
$this->assertEquals(array('_route' => 'a'), $matcher->match('/p/a'));
$this->assertEquals(array('_route' => 'baz', 'baz' => 'p'), $matcher->match('/p'));
$this->assertEquals(array('_route' => 'buz'), $matcher->match('/prefix/buz'));
}
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
{
return new UrlMatcher($routes, $context ?: new RequestContext());
}
} }