Skip to content

Commit

Permalink
Allow dashed parameters in path matching
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashin committed Dec 5, 2023
2 parents 4b35e4b + 93cf80e commit 99584b1
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Service/Request/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ private function getRequestUri(ServiceRequestInterface $serviceRequest): UriInte
$queryParams = $this->parseQueryParams($path);

//check for placeholders
preg_match_all('/{(\w*)}/', $path, $matches);
preg_match_all('/{([\w-]*)}/', $path, $matches);
foreach ($matches[0] as $index => $placeholder) {
$key = $matches[1][$index];
$getterMethod = 'get'.ucfirst($key);
$getterMethod = 'get'.str_replace('-', '', ucwords($key, '-'));
if (!method_exists($serviceRequest, $getterMethod)) {
$message = 'Invalid request path argumentAlias';
$errorCode = Response::HTTP_BAD_REQUEST;
Expand Down
99 changes: 99 additions & 0 deletions Tests/Service/Request/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,103 @@ public function testBuildFlowValidationFailsOnUnmappedRequestArguments()
$requestBuilder->create($this->serviceRequestProphecy->reveal())
);
}

/**
* @return void
*/
public function testBuildFlowWithQueryDashParams()
{
$baseUrl = 'baseUrl';
$routeString = '/routeString?first-param={first-param}&second-param=ignored value';
$originParamValue = 'value with whitespaces';
$requestMethod = 'GET';
$requestBody = '';

$expectedUri = 'baseUrl/routeString?first-param=value+with+whitespaces&second-param=ignored value';

$endpointProphecy = $this->prophesize(EndpointInterface::class);
$endpointProphecy->getBaseUrl()
->willReturn($baseUrl)
->shouldBeCalled()
;
$endpointProphecy->getPath()
->willReturn($routeString)
->shouldBeCalled()
;
$endpointProphecy->getMethod()
->willReturn($requestMethod)
->shouldBeCalled()
;
$endpointProphecy->getRequestFormat()
->willReturn(EndpointInterface::FORMAT_JSON)
->shouldBeCalled()
;
$endpoint = $endpointProphecy->reveal();

$uri = $this->prophesize(UriInterface::class)->reveal();
$request = $this->prophesize(RequestInterface::class)->reveal();

// Mock non existing method of ServiceRequest `getParam`
$serviceRequest = $this->getMockBuilder(ServiceRequestInterface::class)
->setMethods(['getFirstParam'])
->getMock();

$serviceRequest
->method('getFirstParam')
->willReturn($originParamValue);

$this->endpointRegistryProphecy
->getEndpoint($serviceRequest)
->willReturn($endpoint)
->shouldBeCalled()
;

$this->serializerProphecy
->serialize($serviceRequest, EndpointInterface::FORMAT_JSON)
->willReturn($requestBody)
->shouldBeCalled()
;

$this->uriFactoryProphecy
->createUri($expectedUri)
->willReturn($uri)
->shouldBeCalled()
;

$this->messageFactoryProphecy
->createRequest(
$requestMethod,
$uri,
[],
$requestBody,
)
->willReturn($request)
->shouldBeCalled()
;

$this->requestVisitorRegistryProphecy
->getRegisteredRequestVisitors(EndpointInterface::FORMAT_JSON)
->willReturn([])
->shouldBeCalled()
;

$this->requestDecoratorProphecy
->visit($request)
->shouldNotBeCalled()
;

$requestBuilder = new RequestFactory(
$this->endpointRegistryProphecy->reveal(),
$this->serializerProphecy->reveal(),
$this->requestVisitorRegistryProphecy->reveal(),
$this->uriFactoryProphecy->reveal(),
$this->messageFactoryProphecy->reveal(),
false
);

self::assertInstanceOf(
RequestInterface::class,
$requestBuilder->create($serviceRequest)
);
}
}

0 comments on commit 99584b1

Please sign in to comment.