Skip to content

Commit 8c59135

Browse files
authored
Merge pull request #124 from sunrise-php/release/v3.1.0
- Improved payload decoding; - Improved OpenAPI document generation; - Improved localization capabilities.
2 parents bf68c0d + facd42f commit 8c59135

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

src/Middleware/PayloadDecodingMiddleware.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Sunrise\Http\Router\Middleware;
1515

16-
use LogicException;
1716
use Psr\Http\Message\ResponseInterface;
1817
use Psr\Http\Message\ServerRequestInterface;
1918
use Psr\Http\Server\MiddlewareInterface;
@@ -26,7 +25,6 @@
2625
use Sunrise\Http\Router\StringableMediaType;
2726

2827
use function array_map;
29-
use function sprintf;
3028

3129
/**
3230
* @since 3.0.0
@@ -48,11 +46,10 @@ public function __construct(
4846
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
4947
{
5048
$serverRequest = ServerRequest::create($request);
51-
$route = $serverRequest->getRoute();
52-
$serverConsumedMediaTypes = $route->getConsumedMediaTypes();
5349

54-
// The server expects nothing from the client, just keep going...
50+
$serverConsumedMediaTypes = $serverRequest->getRoute()->getConsumedMediaTypes();
5551
if ($serverConsumedMediaTypes === []) {
52+
// The server expects nothing from the client, just keep going...
5653
return $handler->handle($request);
5754
}
5855

@@ -70,11 +67,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7067
}
7168

7269
if (!$this->codecManager->supportsMediaType($clientProducedMediaType)) {
73-
throw new LogicException(sprintf(
74-
'The route "%s" expects the media type "%s" that is not supported by the codec manager.',
75-
$route->getName(),
76-
$clientProducedMediaType->getIdentifier(),
77-
));
70+
return $handler->handle($request);
7871
}
7972

8073
try {

tests/Middleware/PayloadDecodingMiddlewareTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Sunrise\Http\Router\Tests\Middleware;
66

7-
use LogicException;
87
use PHPUnit\Framework\MockObject\MockObject;
98
use PHPUnit\Framework\TestCase;
109
use Psr\Http\Message\ResponseInterface;
@@ -144,15 +143,11 @@ public function testUnsupportedMediaTypeByCodecManager(): void
144143
$this->mockedRoute->expects(self::exactly(2))->method('getConsumedMediaTypes')->willReturn([$this->mockMediaType('application/json')]);
145144
$this->mockedRequest->expects(self::once())->method('getHeaderLine')->with('Content-Type')->willReturn('application/json');
146145
$this->mockedCodecManager->expects(self::once())->method('supportsMediaType')->withAnyParameters()->willReturn(false);
147-
$this->mockedRoute->expects(self::once())->method('getName')->willReturn('foo');
148146
$this->mockedRequestBody->expects(self::never())->method('__toString');
149147
$this->mockedCodecManager->expects(self::never())->method('decode');
150148
$this->mockedRequest->expects(self::never())->method('withParsedBody');
151-
$this->mockedRequestHandler->expects(self::never())->method('handle');
152-
$middleware = new PayloadDecodingMiddleware($this->mockedCodecManager);
153-
$this->expectException(LogicException::class);
154-
$this->expectExceptionMessage('The route "foo" expects the media type "application/json" that is not supported by the codec manager.');
155-
$middleware->process($this->mockedRequest, $this->mockedRequestHandler);
149+
$this->mockedRequestHandler->expects(self::once())->method('handle')->with($this->mockedRequest)->willReturn($this->mockedResponse);
150+
self::assertSame($this->mockedResponse, (new PayloadDecodingMiddleware($this->mockedCodecManager))->process($this->mockedRequest, $this->mockedRequestHandler));
156151
}
157152

158153
public function testInvalidBody(): void

0 commit comments

Comments
 (0)