Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Routing/EndpointLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function load($resource, $type = null): object

//TODO: go through $requestClass and set requirements \d+ etc based on type
$route = new Route(
$endpoint->getPath(),
$this->stripQueryString($endpoint->getPath()),
['_controller' => $controller]
);
$route->setMethods([$endpoint->getMethod()]);
Expand All @@ -77,4 +77,14 @@ public function supports($resource, $type = null): bool
{
return 'endpoint_handler' === $type;
}

/**
* Endpoint paths may contain a query string template (e.g. "/v1/cars?ids={ids}") used by API clients
* to build request URIs. Routes are matched against the path info only, so the query string must not
* be part of the route path.
*/
private function stripQueryString(string $path): string
{
return explode('?', $path, 2)[0];
}
}
67 changes: 67 additions & 0 deletions Tests/Routing/EndpointLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* This file is part of the auto1-oss/service-api-handler-bundle.
*
* (c) AUTO1 Group SE https://www.auto1-group.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Tests\Auto1\ServiceAPIHandlerBundle\Routing;

use Auto1\ServiceAPIComponentsBundle\Service\Endpoint\Endpoint;
use Auto1\ServiceAPIComponentsBundle\Service\Endpoint\EndpointRegistryInterface;
use Auto1\ServiceAPIHandlerBundle\Routing\EndpointLoader;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\Routing\RouteCollection;

class EndpointLoaderTest extends TestCase
Comment thread
aivus marked this conversation as resolved.
{
private const CONTROLLER = 'App\Controller\SomeController::someAction';

public function testLoadStripsQueryStringFromRoutePath(): void
{
$routes = $this->loadRoutes('/v1/items?excludeIds={excludeIds}', 'GET');

$route = $routes->get(RequestStub::class);
$this->assertNotNull($route);
$this->assertSame('/v1/items', $route->getPath());
$this->assertSame(['GET'], $route->getMethods());
}

public function testLoadKeepsPathPlaceholders(): void
{
$routes = $this->loadRoutes('/v1/cars/{id}', 'POST');

$route = $routes->get(RequestStub::class);
$this->assertNotNull($route);
$this->assertSame('/v1/cars/{id}', $route->getPath());
$this->assertSame(['POST'], $route->getMethods());
}

private function loadRoutes(string $path, string $method): RouteCollection
{
$endpoint = (new Endpoint())
->setPath($path)
->setMethod($method);

$endpointRegistryProphecy = $this->prophesize(EndpointRegistryInterface::class);
$endpointRegistryProphecy->getEndpoint(Argument::type(RequestStub::class))
->willReturn($endpoint);

$endpointLoader = new EndpointLoader(
$endpointRegistryProphecy->reveal(),
[self::CONTROLLER => RequestStub::class]
);

$routes = $endpointLoader->load('.', 'endpoint_handler');
$this->assertInstanceOf(RouteCollection::class, $routes);

return $routes;
}
}
20 changes: 20 additions & 0 deletions Tests/Routing/RequestStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the auto1-oss/service-api-handler-bundle.
*
* (c) AUTO1 Group SE https://www.auto1-group.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Tests\Auto1\ServiceAPIHandlerBundle\Routing;

use Auto1\ServiceAPIRequest\ServiceRequestInterface;

class RequestStub implements ServiceRequestInterface
{
}
Loading