Skip to content
Open
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
2 changes: 2 additions & 0 deletions Slim/Interfaces/RouteCollectorProxyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* @api
* @template TContainerInterface of (ContainerInterface|null)
*
* @method RouteInterface query(string $pattern, callable|array{0: string, 1: string}|string $callable)
*/
interface RouteCollectorProxyInterface
{
Expand Down
14 changes: 14 additions & 0 deletions Slim/Routing/RouteCollectorProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ public function options(string $pattern, $callable): RouteInterface
return $this->map(['OPTIONS'], $pattern, $callable);
}

/**
* Add QUERY route
*
* @api
* @param string $pattern The route URI pattern
* @param callable|array{class-string, string}|string $callable The route callback routine
*
* @link https://www.rfc-editor.org/rfc/rfc10008
*/
public function query(string $pattern, $callable): RouteInterface
{
return $this->map(['QUERY'], $pattern, $callable);
}

/**
* {@inheritdoc}
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,38 @@ public function testAnyRoute(): void
}
}

public function testQueryRoute(): void
{
$streamProphecy = $this->prophesize(StreamInterface::class);
$streamProphecy->__toString()->willReturn('Hello World');

$responseProphecy = $this->prophesize(ResponseInterface::class);
$responseProphecy->getBody()->willReturn($streamProphecy->reveal());

$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$responseFactoryProphecy->createResponse()->willReturn($responseProphecy->reveal());

$uriProphecy = $this->prophesize(UriInterface::class);
$uriProphecy->getPath()->willReturn('/');

$requestProphecy = $this->prophesize(ServerRequestInterface::class);
$requestProphecy->getMethod()->willReturn('QUERY');
$requestProphecy->getUri()->willReturn($uriProphecy->reveal());
$requestProphecy->getAttribute(RouteContext::ROUTING_RESULTS)->willReturn(null);
$requestProphecy->withAttribute(Argument::type('string'), Argument::any())->will(function ($args) {
$this->getAttribute($args[0])->willReturn($args[1]);
return $this;
});

$app = new App($responseFactoryProphecy->reveal());
$app->query('/', function (ServerRequestInterface $request, ResponseInterface $response) {
return $response;
});
$response = $app->handle($requestProphecy->reveal());

$this->assertSame('Hello World', (string) $response->getBody());
}

/********************************************************************************
* Route collector proxy methods
*******************************************************************************/
Expand Down
1 change: 1 addition & 0 deletions tests/Factory/AppFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function testDetermineResponseFactoryThrowsRuntimeExceptionIfDecoratedNot
/**
* @runInSeparateProcess - Psr17FactoryProvider::setFactories breaks other tests
*/
#[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
public function testDetermineResponseFactoryThrowsRuntimeException()
{
$this->expectException(RuntimeException::class);
Expand Down
33 changes: 33 additions & 0 deletions tests/Routing/RouteCollectorProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,39 @@ public function testOptions()
$this->assertSame($pattern, $route->getPattern());
}

public function testQuery()
{
$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
$callableResolverProphecy = $this->prophesize(CallableResolverInterface::class);

$pattern = '/';
$callable = function () {
};

$routeProphecy = $this->prophesize(RouteInterface::class);
$routeProphecy
->getPattern()
->willReturn($pattern)
->shouldBeCalledOnce();

$routeCollectorProphecy = $this->prophesize(RouteCollectorInterface::class);
$routeCollectorProphecy
->map(['QUERY'], $pattern, Argument::is($callable))
->willReturn($routeProphecy->reveal())
->shouldBeCalledOnce();

$routeCollectorProxy = new RouteCollectorProxy(
$responseFactoryProphecy->reveal(),
$callableResolverProphecy->reveal(),
null,
$routeCollectorProphecy->reveal()
);

$route = $routeCollectorProxy->query($pattern, $callable);

$this->assertSame($pattern, $route->getPattern());
}

public function testAny()
{
$responseFactoryProphecy = $this->prophesize(ResponseFactoryInterface::class);
Expand Down
Loading