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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Added

- Add `query()` route helper for the HTTP `QUERY` method ([RFC 10008](https://www.rfc-editor.org/rfc/rfc10008))

### Changed

### Removed
Expand Down
10 changes: 10 additions & 0 deletions Slim/Interfaces/RouteCollectorProxyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public function delete(string $pattern, $callable): RouteInterface;
*/
public function options(string $pattern, $callable): RouteInterface;

/**
* Add QUERY route
*
* @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;

/**
* Add route for any HTTP method
*
Expand Down
8 changes: 8 additions & 0 deletions Slim/Routing/RouteCollectorProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ public function options(string $pattern, $callable): RouteInterface
return $this->map(['OPTIONS'], $pattern, $callable);
}

/**
* {@inheritdoc}
*/
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