Skip to content

Commit 359c329

Browse files
committed
Alternative fix to #2298
1 parent 39f74f2 commit 359c329

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
3131
use ApiPlatform\Core\Util\AttributesExtractor;
3232
use ApiPlatform\Core\Util\ClassInfoTrait;
33-
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
3433
use Symfony\Component\PropertyAccess\PropertyAccess;
3534
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
3635
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
@@ -74,8 +73,6 @@ public function getItemFromIri(string $iri, array $context = [])
7473
$parameters = $this->router->match($iri);
7574
} catch (RoutingExceptionInterface $e) {
7675
throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e);
77-
} catch (RequestExceptionInterface $e) {
78-
throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e);
7976
}
8077

8178
if (!isset($parameters['_api_resource_class'])) {

src/Bridge/Symfony/Routing/Router.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
namespace ApiPlatform\Core\Bridge\Symfony\Routing;
1515

1616
use ApiPlatform\Core\Api\UrlGeneratorInterface;
17+
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
1718
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
1820
use Symfony\Component\Routing\RequestContext;
1921
use Symfony\Component\Routing\RouterInterface;
2022

@@ -72,7 +74,12 @@ public function match($pathInfo)
7274
$pathInfo = str_replace($baseContext->getBaseUrl(), '', $pathInfo);
7375

7476
$request = Request::create($pathInfo, 'GET', [], [], [], ['HTTP_HOST' => $baseContext->getHost()]);
75-
$context = (new RequestContext())->fromRequest($request);
77+
try {
78+
$context = (new RequestContext())->fromRequest($request);
79+
} catch (RequestExceptionInterface $e) {
80+
throw new RouteNotFoundException('Invalid request context.');
81+
}
82+
7683
$context->setPathInfo($pathInfo);
7784
$context->setScheme($baseContext->getScheme());
7885
$context->setHost($baseContext->getHost());

tests/Bridge/Symfony/Routing/IriConverterTest.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
3232
use PHPUnit\Framework\TestCase;
3333
use Prophecy\Argument;
34-
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
3534
use Symfony\Component\Routing\Exception\RouteNotFoundException;
3635
use Symfony\Component\Routing\RouterInterface;
3736

@@ -99,20 +98,6 @@ public function testGetItemFromIriItemNotFoundException()
9998
$converter->getItemFromIri('/users/3');
10099
}
101100

102-
public function testGetItemFromIriWithDateLooksLikeUrl()
103-
{
104-
$this->expectException(InvalidArgumentException::class);
105-
$this->expectExceptionMessage('No route matches "28-01-2018 10:10".');
106-
107-
$itemDataProviderProphecy = $this->prophesize(ItemDataProviderInterface::class);
108-
109-
$routerProphecy = $this->prophesize(RouterInterface::class);
110-
$routerProphecy->match('28-01-2018 10:10')->willThrow(new SuspiciousOperationException())->shouldBeCalledTimes(1);
111-
112-
$converter = $this->getIriConverter($routerProphecy, null, $itemDataProviderProphecy);
113-
$converter->getItemFromIri('28-01-2018 10:10');
114-
}
115-
116101
public function testGetItemFromIri()
117102
{
118103
$item = new \stdClass();

tests/Bridge/Symfony/Routing/RouterTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Bridge\Symfony\Routing\Router;
1717
use PHPUnit\Framework\TestCase;
1818
use Prophecy\Argument;
19+
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
1920
use Symfony\Component\Routing\RequestContext;
2021
use Symfony\Component\Routing\RouteCollection;
2122
use Symfony\Component\Routing\RouterInterface;
@@ -72,4 +73,17 @@ public function testMatch()
7273

7374
$this->assertEquals(['bar'], $router->match('/app_dev.php/foo'));
7475
}
76+
77+
public function testWithinvalidContext()
78+
{
79+
$this->expectException(RoutingExceptionInterface::class);
80+
$this->expectExceptionMessage('Invalid request context.');
81+
$context = new RequestContext('/app_dev.php', 'GET', 'localhost', 'https');
82+
83+
$mockedRouter = $this->prophesize('Symfony\Component\Routing\RouterInterface');
84+
$mockedRouter->getContext()->willReturn($context)->shouldBeCalled();
85+
86+
$router = new Router($mockedRouter->reveal());
87+
$router->match('28-01-2018 10:10');
88+
}
7589
}

0 commit comments

Comments
 (0)