Skip to content

Commit ab0576c

Browse files
committed
Add code, unit test and autoloader
1 parent 878571c commit ab0576c

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@
88
"psr/http-client": "^1.0",
99
"psr/http-message": "^1.0"
1010
},
11+
"require-dev": {
12+
"phpunit/phpunit": "^8.0",
13+
"squizlabs/php_codesniffer": "^3.5"
14+
},
1115
"autoload": {
1216
"psr-4": {
1317
"Coolblue\\Http\\Client\\": "src/"
1418
}
1519
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Coolblue\\Tests\\Http\\Client\\": "tests/"
23+
}
24+
},
1625
"config": {
1726
"sort-packages": true
1827
}

src/MiddlewareClient.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coolblue\Http\Client;
6+
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
final class MiddlewareClient implements ClientInterface
12+
{
13+
/** @var ClientInterface */
14+
private $client;
15+
16+
/** @var MiddlewareInterface[] */
17+
private $middleware;
18+
19+
public function __construct(ClientInterface $client, MiddlewareInterface ...$middleware)
20+
{
21+
$this->client = $client;
22+
$this->middleware = $middleware;
23+
}
24+
25+
public function sendRequest(RequestInterface $request): ResponseInterface
26+
{
27+
$middleware = reset($this->middleware);
28+
if ($middleware instanceof MiddlewareInterface) {
29+
$new = new self($this->client, ...array_slice($this->middleware, 1));
30+
return $middleware->process($request, $new);
31+
}
32+
33+
return $this->client->sendRequest($request);
34+
}
35+
}

src/MiddlewareInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coolblue\Http\Client;
6+
7+
use Psr\Http\Client\ClientInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
interface MiddlewareInterface
12+
{
13+
public function process(RequestInterface $request, ClientInterface $client): ResponseInterface;
14+
}

tests/MiddlewareClientTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coolblue\Tests\Http\Client;
6+
7+
use Coolblue\Http\Client\MiddlewareClient;
8+
use Coolblue\Http\Client\MiddlewareInterface;
9+
use PHPUnit\Framework\TestCase;
10+
use Prophecy\Argument;
11+
use Psr\Http\Client\ClientInterface;
12+
use Psr\Http\Message\RequestInterface;
13+
use Psr\Http\Message\ResponseInterface;
14+
15+
final class MiddlewareClientTest extends TestCase
16+
{
17+
public function testHandlingOfMiddlewareInCorrectOrder(): void
18+
{
19+
$order = [];
20+
21+
$request = $this->prophesize(RequestInterface::class);
22+
$response = $this->prophesize(ResponseInterface::class);
23+
24+
$middlewareOne = $this->prophesize(MiddlewareInterface::class);
25+
$middlewareOne->process(Argument::type(RequestInterface::class), Argument::type(ClientInterface::class))
26+
->shouldBeCalled()
27+
->will(function (array $arguments) use (&$order) {
28+
[$request, $client] = $arguments;
29+
$order[] = 'before one';
30+
$response = $client->sendRequest($request);
31+
$order[] = 'after one';
32+
return $response;
33+
});
34+
35+
$middlewareTwo = $this->prophesize(MiddlewareInterface::class);
36+
$middlewareTwo->process(Argument::type(RequestInterface::class), Argument::type(ClientInterface::class))
37+
->shouldBeCalled()
38+
->will(function (array $arguments) use (&$order) {
39+
[$request, $client] = $arguments;
40+
$order[] = 'before two';
41+
$response = $client->sendRequest($request);
42+
$order[] = 'after two';
43+
44+
return $response;
45+
});
46+
47+
$client = $this->prophesize(ClientInterface::class);
48+
$client->sendRequest(Argument::type(RequestInterface::class))
49+
->shouldBeCalledOnce()
50+
->will(function () use (&$order, $response) {
51+
$order[] = 'client';
52+
return $response->reveal();
53+
});
54+
55+
$middlewareClient = new MiddlewareClient(
56+
$client->reveal(),
57+
$middlewareOne->reveal(),
58+
$middlewareTwo->reveal()
59+
);
60+
61+
$response = $middlewareClient->sendRequest($request->reveal());
62+
63+
self::assertInstanceOf(ResponseInterface::class, $response);
64+
self::assertEquals( // json_encode is used to also test the order of elements in the array
65+
json_encode(['before one', 'before two', 'client', 'after two', 'after one']),
66+
json_encode($order)
67+
);
68+
}
69+
}

0 commit comments

Comments
 (0)