Passed
Push — master ( c833e6...2cc74e )
by Felipe
02:42 queued 19s
created

RequestHandlerClientTest::testSendServerRequestReturnResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
namespace CoiSA\Http\Test;
12
13
use CoiSA\Http\Client\RequestHandlerClient;
14
use PHPUnit\Framework\TestCase;
15
use Prophecy\Prophecy\ObjectProphecy;
16
use Psr\Http\Client\ClientInterface;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestFactoryInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Server\RequestHandlerInterface;
22
23
/**
24
 * Class RequestHandlerClientTest
25
 *
26
 * @package CoiSA\Http\Test
27
 */
28
final class RequestHandlerClientTest extends TestCase
29
{
30
    /** @var ObjectProphecy|RequestHandlerInterface */
31
    private $requestHandler;
32
33
    /** @var ObjectProphecy|ServerRequestFactoryInterface */
34
    private $serverRequestFactory;
35
36
    /** @var ObjectProphecy|RequestInterface */
37
    private $request;
38
39
    /** @var ObjectProphecy|ServerRequestInterface */
40
    private $serverRequest;
41
42
    /** @var ObjectProphecy|ResponseInterface */
43
    private $response;
44
45
    public function setUp(): void
46
    {
47
        $this->requestHandler       = $this->prophesize(RequestHandlerInterface::class);
48
        $this->serverRequestFactory = $this->prophesize(ServerRequestFactoryInterface::class);
49
        $this->request              = $this->prophesize(RequestInterface::class);
50
        $this->serverRequest        = $this->prophesize(ServerRequestInterface::class);
51
        $this->response             = $this->prophesize(ResponseInterface::class);
52
53
        $method = 'GET';
54
        $uri    = 'http://google.com';
55
56
        $this->request->getMethod()->willReturn($method);
57
        $this->request->getUri()->willReturn($uri);
58
59
        $this->serverRequestFactory->createServerRequest($method, $uri, $_SERVER)->will([$this->serverRequest, 'reveal']);
60
61
        $this->requestHandler->handle($this->request->reveal())->will([$this->response, 'reveal']);
62
        $this->requestHandler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']);
63
    }
64
65
    public function testImplementClientInterface(): void
66
    {
67
        $client = new RequestHandlerClient($this->requestHandler->reveal());
68
        $this->assertInstanceOf(ClientInterface::class, $client);
69
    }
70
71
    public function testConstructorWithoutServerRequestFactory(): void
72
    {
73
        $client = new RequestHandlerClient($this->requestHandler->reveal());
74
        $this->assertInstanceOf(RequestHandlerClient::class, $client);
75
    }
76
77
    public function testConstructorWithServerRequestFactory(): void
78
    {
79
        $client = new RequestHandlerClient($this->requestHandler->reveal(), $this->serverRequestFactory->reveal());
80
        $this->assertInstanceOf(RequestHandlerClient::class, $client);
81
    }
82
83
    public function testSendServerRequestReturnResponse(): void
84
    {
85
        $client   = new RequestHandlerClient($this->requestHandler->reveal());
86
        $response = $client->sendRequest($this->serverRequest->reveal());
87
88
        $this->assertInstanceOf(ResponseInterface::class, $response);
89
    }
90
91
    public function testSendRequestReturnResponse(): void
92
    {
93
        $client   = new RequestHandlerClient($this->requestHandler->reveal(), $this->serverRequestFactory->reveal());
94
        $response = $client->sendRequest($this->request->reveal());
95
96
        $this->assertInstanceOf(ResponseInterface::class, $response);
97
    }
98
}
99