Skip to content

Commit cb13da8

Browse files
authored
Merge pull request #19 from lcobucci/psr-http-factories
Use PSR-17 factories
2 parents 63bea7f + d5a4045 commit cb13da8

File tree

4 files changed

+17
-80
lines changed

4 files changed

+17
-80
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ jobs:
5353
- mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,}
5454
- if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for mutation tests"; exit 1; fi
5555
script:
56-
- ./vendor/bin/infection --threads=$(nproc) --min-msi=97 --min-covered-msi=97
56+
- ./vendor/bin/infection --threads=$(nproc) --min-msi=100 --min-covered-msi=100

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"php": "^7.3",
2323
"ext-json": "^1.7",
2424
"fig/http-message-util": "^1.1",
25+
"psr/http-factory": "^1.0",
2526
"psr/http-server-middleware": "^1.0"
2627
},
2728
"require-dev": {

src/ContentTypeMiddleware.php

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
use Middlewares\ContentType;
88
use Psr\Http\Message\ResponseInterface;
99
use Psr\Http\Message\ServerRequestInterface;
10-
use Psr\Http\Message\StreamInterface;
10+
use Psr\Http\Message\StreamFactoryInterface;
1111
use Psr\Http\Server\MiddlewareInterface;
1212
use Psr\Http\Server\RequestHandlerInterface;
13-
use Zend\Diactoros\Stream;
14-
use function assert;
1513
use function strpos;
1614
use function substr;
1715

@@ -23,7 +21,7 @@ final class ContentTypeMiddleware implements MiddlewareInterface
2321
private $negotiator;
2422

2523
/**
26-
* @var callable
24+
* @var StreamFactoryInterface
2725
*/
2826
private $streamFactory;
2927

@@ -38,7 +36,7 @@ final class ContentTypeMiddleware implements MiddlewareInterface
3836
public function __construct(
3937
MiddlewareInterface $negotiator,
4038
array $formatters,
41-
callable $streamFactory
39+
StreamFactoryInterface $streamFactory
4240
) {
4341
$this->negotiator = $negotiator;
4442
$this->formatters = $formatters;
@@ -52,14 +50,12 @@ public function __construct(
5250
public static function fromRecommendedSettings(
5351
array $formats,
5452
array $formatters,
55-
?callable $streamFactory = null
53+
StreamFactoryInterface $streamFactory
5654
): self {
5755
return new self(
5856
new ContentType($formats),
5957
$formatters,
60-
$streamFactory ?? static function (): StreamInterface {
61-
return new Stream('php://temp', 'wb+');
62-
}
58+
$streamFactory
6359
);
6460
}
6561

@@ -98,18 +94,15 @@ private function extractContentType(string $contentType): string
9894
*/
9995
private function formatResponse(UnformattedResponse $response, ?Formatter $formatter): ResponseInterface
10096
{
101-
$body = ($this->streamFactory)();
102-
assert($body instanceof StreamInterface);
103-
104-
$response = $response->withBody($body);
105-
10697
if ($formatter === null) {
107-
return $response->withStatus(StatusCodeInterface::STATUS_NOT_ACCEPTABLE);
98+
return $response->withBody($this->streamFactory->createStream())
99+
->withStatus(StatusCodeInterface::STATUS_NOT_ACCEPTABLE);
108100
}
109101

110-
$body->write($formatter->format($response->getUnformattedContent(), $response->getAttributes()));
111-
$body->rewind();
112-
113-
return $response;
102+
return $response->withBody(
103+
$this->streamFactory->createStream(
104+
$formatter->format($response->getUnformattedContent(), $response->getAttributes())
105+
)
106+
);
114107
}
115108
}

tests/ContentTypeMiddlewareTest.php

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@
33

44
namespace Lcobucci\ContentNegotiation\Tests;
55

6-
use AssertionError;
76
use Fig\Http\Message\StatusCodeInterface;
87
use Lcobucci\ContentNegotiation\ContentTypeMiddleware;
98
use Lcobucci\ContentNegotiation\Formatter;
109
use Lcobucci\ContentNegotiation\Tests\Formatter\NaiveTemplateEngine;
1110
use Lcobucci\ContentNegotiation\UnformattedResponse;
12-
use PHPUnit\Framework\Error\Warning;
1311
use PHPUnit\Framework\TestCase;
1412
use Psr\Http\Message\ResponseInterface;
1513
use Psr\Http\Message\ServerRequestInterface;
1614
use Psr\Http\Server\RequestHandlerInterface;
1715
use Zend\Diactoros\Response;
1816
use Zend\Diactoros\Response\EmptyResponse;
1917
use Zend\Diactoros\ServerRequest;
18+
use Zend\Diactoros\StreamFactory;
2019
use function array_map;
21-
use function ini_set;
2220

2321
/**
2422
* @coversDefaultClass \Lcobucci\ContentNegotiation\ContentTypeMiddleware
@@ -170,61 +168,6 @@ public function processShouldReturnAResponseWithFormattedContentEvenWithoutForci
170168
self::assertJsonStringEqualsJsonString('{"id":1,"name":"Testing"}', (string) $response->getBody());
171169
}
172170

173-
/**
174-
* @test
175-
*
176-
* @covers ::__construct()
177-
* @covers ::fromRecommendedSettings()
178-
* @covers ::process()
179-
* @covers ::extractContentType()
180-
* @covers ::formatResponse()
181-
*
182-
* @uses \Lcobucci\ContentNegotiation\UnformattedResponse
183-
* @uses \Lcobucci\ContentNegotiation\Formatter\Json
184-
*/
185-
public function processShouldRaiseExceptionWhenInvalidStreamWasCreatedInDevelopmentMode(): void
186-
{
187-
ini_set('assert.exception', '1');
188-
189-
$this->expectException(AssertionError::class);
190-
$this->processWithInvalidStreamFactory();
191-
}
192-
193-
/**
194-
* @test
195-
*
196-
* @covers ::__construct()
197-
* @covers ::fromRecommendedSettings()
198-
* @covers ::process()
199-
* @covers ::extractContentType()
200-
* @covers ::formatResponse()
201-
*
202-
* @uses \Lcobucci\ContentNegotiation\UnformattedResponse
203-
* @uses \Lcobucci\ContentNegotiation\Formatter\Json
204-
*/
205-
public function processShouldRaiseAWarningWhenInvalidStreamWasCreatedInDevelopmentMode(): void
206-
{
207-
ini_set('assert.exception', '0');
208-
209-
$this->expectException(Warning::class);
210-
$this->processWithInvalidStreamFactory();
211-
}
212-
213-
private function processWithInvalidStreamFactory(): void
214-
{
215-
$middleware = $this->createMiddleware(
216-
true,
217-
static function () {
218-
return false;
219-
}
220-
);
221-
222-
$middleware->process(
223-
new ServerRequest(),
224-
$this->createRequestHandler($this->createResponse())
225-
);
226-
}
227-
228171
/**
229172
* @param mixed[] $attributes
230173
*/
@@ -261,15 +204,15 @@ public function handle(ServerRequestInterface $request): ResponseInterface
261204
};
262205
}
263206

264-
private function createMiddleware(bool $forceCharset = true, ?callable $streamFactory = null): ContentTypeMiddleware
207+
private function createMiddleware(bool $forceCharset = true): ContentTypeMiddleware
265208
{
266209
return ContentTypeMiddleware::fromRecommendedSettings(
267210
$this->configureCharset($forceCharset),
268211
[
269212
'application/json' => new Formatter\Json(),
270213
'text/html' => new NaiveTemplateEngine(),
271214
],
272-
$streamFactory
215+
new StreamFactory()
273216
);
274217
}
275218

0 commit comments

Comments
 (0)