Passed
Push — master ( 6744b7...8ddff3 )
by Felipe
01:56
created

testSendRequestReturnResponseFromClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of coisa/http.
4
 *
5
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
use CoiSA\Http\Handler\PsrHttpClientHandler;
12
use PHPUnit\Framework\TestCase;
13
use Prophecy\Prophecy\ObjectProphecy;
14
use Psr\Http\Client\ClientInterface;
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
20
/**
21
 * Class EchoBodyMiddlewareTest
22
 */
23
final class PsrHttpClientHandlerTest extends TestCase
24
{
25
    /** @var ClientInterface|ObjectProphecy */
26
    private $client;
27
28
    /** @var RequestInterface|ObjectProphecy */
29
    private $request;
30
31
    /** @var ServerRequestInterface|ObjectProphecy */
32
    private $serverRequest;
33
34
    /** @var ResponseInterface|ObjectProphecy */
35
    private $response;
36
37
    /** @var PsrHttpClientHandler */
38
    private $handler;
39
40
    public function setUp(): void
41
    {
42
        $this->client = $this->prophesize(ClientInterface::class);
43
        $this->request = $this->prophesize(RequestInterface::class);
44
        $this->serverRequest = $this->prophesize(ServerRequestInterface::class);
45
        $this->response = $this->prophesize(ResponseInterface::class);
46
        $this->handler = new PsrHttpClientHandler($this->client->reveal());
47
48
        $this->client->sendRequest($this->request->reveal())->will([$this->response, 'reveal']);
49
        $this->client->sendRequest($this->serverRequest->reveal())->will([$this->response, 'reveal']);
50
    }
51
52
    public function testHandlerImplementInterfaces()
53
    {
54
        $this->assertInstanceOf(ClientInterface::class, $this->handler);
55
        $this->assertInstanceOf(RequestHandlerInterface::class, $this->handler);
56
    }
57
58
    public function testHandleReturnResponseFromClient()
59
    {
60
        $response = $this->handler->handle($this->serverRequest->reveal());
61
        $client = $this->client->reveal();
62
        $this->assertEquals($client->sendRequest($this->serverRequest->reveal()), $response);
63
    }
64
65
    public function testSendRequestReturnResponseFromClient()
66
    {
67
        $response = $this->handler->sendRequest($this->request->reveal());
68
        $client = $this->client->reveal();
69
        $this->assertEquals($client->sendRequest($this->request->reveal()), $response);
70
71
        $response = $this->handler->sendRequest($this->serverRequest->reveal());
72
        $this->assertEquals($client->sendRequest($this->serverRequest->reveal()), $response);
73
    }
74
}
75