Skip to content

Commit 2f59dca

Browse files
authored
Merge pull request #8 from php-middleware/psr-15
Psr 15
2 parents 2d8ae34 + cd8da31 commit 2f59dca

13 files changed

+99
-75
lines changed

.travis.yml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
language: php
22

3-
matrix:
4-
fast_finish: true
5-
include:
6-
- php: 5.5
7-
- php: 5.6
8-
- php: 7
9-
- php: hhvm
3+
php:
4+
- 5.6
5+
- 7.0
6+
- 7.1
107

11-
install:
12-
- travis_retry composer install --no-interaction --ignore-platform-reqs --prefer-source
13-
- composer info -i
8+
env:
9+
- DEPS=lowest
10+
- DEPS=latest
11+
12+
before_script:
13+
- phpenv config-rm xdebug.ini
14+
- composer self-update
15+
- if [[ $DEPS == 'lowest' ]]; then composer update --prefer-stable --no-interaction --prefer-lowest ; fi
16+
- if [[ $DEPS == 'latest' ]]; then composer update --prefer-stable --no-interaction ; fi
1417

1518
script:
1619
- ./vendor/bin/phpunit

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
"request-id"
1010
],
1111
"require": {
12-
"php": "^5.5 || ^7.0",
13-
"psr/http-message": "^1.0"
12+
"php": "^5.6 || ^7.0",
13+
"psr/http-message": "^1.0",
14+
"php-middleware/double-pass-compatibility": "^1.0",
15+
"http-interop/http-middleware": "^0.4.1"
1416
},
1517
"require-dev": {
16-
"phpunit/phpunit": "^4.8.6",
18+
"phpunit/phpunit": "^5.6 || ^6.1.3",
1719
"ramsey/uuid": "^3.0",
1820
"zendframework/zend-diactoros": "^1.1.3"
1921
},

phpunit.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
<phpunit bootstrap="./vendor/autoload.php" colors="true">
44
<testsuites>
5-
<testsuite name="PhpMiddleware\\LogHttpMessages Tests">
5+
<testsuite>
66
<directory>./test</directory>
77
</testsuite>
88
</testsuites>
99

10-
<logging>
11-
<log type="coverage-clover" target="tmp/phpunit-coverage-clover.xml"/>
12-
<log type="coverage-html" target="tmp/" />
13-
</logging>
14-
1510
<filter>
1611
<whitelist processUncoveredFilesFromWhitelist="true">
1712
<directory suffix=".php">./src</directory>

src/RequestIdMiddleware.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
namespace PhpMiddleware\RequestId;
44

5+
use Interop\Http\ServerMiddleware\DelegateInterface;
6+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
7+
use PhpMiddleware\DoublePassCompatibilityTrait;
58
use PhpMiddleware\RequestId\Exception\NotGenerated;
69
use PhpMiddleware\RequestId\RequestIdProviderFactoryInterface;
710
use Psr\Http\Message\ResponseInterface;
811
use Psr\Http\Message\ServerRequestInterface;
912

10-
final class RequestIdMiddleware implements RequestIdProviderInterface
13+
final class RequestIdMiddleware implements RequestIdProviderInterface, MiddlewareInterface
1114
{
15+
use DoublePassCompatibilityTrait;
16+
1217
const DEFAULT_RESPONSE_HEADER = 'X-Request-Id';
1318
const ATTRIBUTE_NAME = 'request-id';
1419

@@ -40,26 +45,45 @@ public function __construct(
4045
}
4146

4247
/**
43-
* @param ServerRequestInterface $request
44-
* @param ResponseInterface $response
45-
* @param callable $next
46-
*
4748
* @return ResponseInterface
4849
*/
49-
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
50+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
5051
{
51-
$requestIdProvider = $this->requestIdProviderFactory->create($request);
52+
$requestWithAttribute = $this->attachRequestIdToAttribute($request);
5253

54+
$response = $delegate->process($requestWithAttribute);
55+
56+
if ($this->canAttachToResponse()) {
57+
return $this->attachRequestIdToResponse($response);
58+
}
59+
return $response;
60+
}
61+
62+
/**
63+
* @return ResponseInterface
64+
*/
65+
private function attachRequestIdToAttribute(ServerRequestInterface $request)
66+
{
67+
$requestIdProvider = $this->requestIdProviderFactory->create($request);
5368
$this->requestId = $requestIdProvider->getRequestId();
5469

55-
$requestWithAttribute = $request->withAttribute(self::ATTRIBUTE_NAME, $this->requestId);
70+
return $request->withAttribute(self::ATTRIBUTE_NAME, $this->requestId);
71+
}
5672

57-
$nextResponse = $next($requestWithAttribute, $response);
73+
/**
74+
* @return ResponseInterface
75+
*/
76+
private function attachRequestIdToResponse(ResponseInterface $response)
77+
{
78+
return $response->withHeader($this->responseHeader, $this->requestId);
79+
}
5880

59-
if (is_string($this->responseHeader)) {
60-
return $nextResponse->withHeader($this->responseHeader, $this->requestId);
61-
}
62-
return $nextResponse;
81+
/**
82+
* @return bool
83+
*/
84+
private function canAttachToResponse()
85+
{
86+
return is_string($this->responseHeader) && !empty($this->responseHeader);
6387
}
6488

6589
/**

test/Generator/Md5GeneratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use PhpMiddleware\RequestId\Generator\GeneratorInterface;
66
use PhpMiddleware\RequestId\Generator\Md5Generator;
7-
use PHPUnit_Framework_TestCase;
7+
use PHPUnit\Framework\TestCase;
88

9-
class Md5GeneratorTest extends PHPUnit_Framework_TestCase
9+
class Md5GeneratorTest extends TestCase
1010
{
1111
protected $generator;
1212

1313
protected function setUp()
1414
{
15-
$decoratedGenerator = $this->getMock(GeneratorInterface::class);
15+
$decoratedGenerator = $this->createMock(GeneratorInterface::class);
1616
$decoratedGenerator->method('generateRequestId')->willReturn('boo');
1717

1818
$this->generator = new Md5Generator($decoratedGenerator);

test/Generator/PhpUniqidGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace PhpMiddlewareTestTest\RequestId\Generator;
44

55
use PhpMiddleware\RequestId\Generator\PhpUniqidGenerator;
6-
use PHPUnit_Framework_TestCase;
6+
use PHPUnit\Framework\TestCase;
77

8-
class PhpUniqidGeneratorTest extends PHPUnit_Framework_TestCase
8+
class PhpUniqidGeneratorTest extends TestCase
99
{
1010
protected $generator;
1111

test/Generator/PrefixedGeneratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use PhpMiddleware\RequestId\Generator\GeneratorInterface;
66
use PhpMiddleware\RequestId\Generator\PrefixedGenerator;
7-
use PHPUnit_Framework_TestCase;
7+
use PHPUnit\Framework\TestCase;
88

9-
class PrefixedGeneratorTest extends PHPUnit_Framework_TestCase
9+
class PrefixedGeneratorTest extends TestCase
1010
{
1111
protected $decoratedGenerator;
1212

1313
protected function setUp()
1414
{
15-
$this->decoratedGenerator = $this->getMock(GeneratorInterface::class);
15+
$this->decoratedGenerator = $this->createMock(GeneratorInterface::class);
1616
$this->decoratedGenerator->method('generateRequestId')->willReturn('boo');
1717
}
1818

test/Generator/RamseyFactoryUuidGeneratorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@
66
use PhpMiddleware\RequestId\Generator\RamseyUuid3Generator;
77
use PhpMiddleware\RequestId\Generator\RamseyUuid4Generator;
88
use PhpMiddleware\RequestId\Generator\RamseyUuid5Generator;
9-
use PHPUnit_Framework_TestCase;
9+
use PHPUnit\Framework\TestCase;
1010
use Ramsey\Uuid\UuidFactoryInterface;
1111
use Ramsey\Uuid\UuidInterface;
1212

13-
class RamseyFactoryUuidGeneratorTest extends PHPUnit_Framework_TestCase
13+
class RamseyFactoryUuidGeneratorTest extends TestCase
1414
{
1515
protected $factory;
1616
protected $uuid;
1717

1818
protected function setUp()
1919
{
20-
$this->factory = $this->getMock(UuidFactoryInterface::class);
20+
$this->factory = $this->createMock(UuidFactoryInterface::class);
2121

22-
$this->uuid = $this->getMock(UuidInterface::class);
22+
$this->uuid = $this->createMock(UuidInterface::class);
2323
$this->uuid->method('toString')->willReturn('uuid');
2424
}
2525

test/Generator/RamseyUuid4StaticGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace PhpMiddlewareTestTest\RequestId\Generator;
44

55
use PhpMiddleware\RequestId\Generator\RamseyUuid4StaticGenerator;
6-
use PHPUnit_Framework_TestCase;
6+
use PHPUnit\Framework\TestCase;
77
use Ramsey\Uuid\Uuid;
88
use Ramsey\Uuid\UuidInterface;
99

10-
class RamseyUuid4StaticGeneratorTest extends PHPUnit_Framework_TestCase
10+
class RamseyUuid4StaticGeneratorTest extends TestCase
1111
{
1212
protected $generator;
1313

test/MonologProcessorTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
use PhpMiddleware\RequestId\Exception\MissingRequestId;
66
use PhpMiddleware\RequestId\MonologProcessor;
77
use PhpMiddleware\RequestId\RequestIdProviderInterface;
8+
use PHPUnit\Framework\TestCase;
89

9-
class MonologProcessorTest extends \PHPUnit_Framework_TestCase
10+
class MonologProcessorTest extends TestCase
1011
{
1112
protected $processor;
12-
/**
13-
* @var RequestIdProviderInterface|\PHPUnit_Framework_MockObject_MockObject
14-
*/
1513
private $requestIdProvider;
1614

1715
public function testIsRequestIdInRecord()
@@ -38,7 +36,7 @@ public function testIsMissingRequestIdExceptionHandledProperly()
3836

3937
protected function setUp()
4038
{
41-
$this->requestIdProvider = $this->getMock(RequestIdProviderInterface::class);
39+
$this->requestIdProvider = $this->createMock(RequestIdProviderInterface::class);
4240
$this->processor = new MonologProcessor($this->requestIdProvider);
4341
}
4442
}

0 commit comments

Comments
 (0)