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\PsrHttpClient; |
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\ServerRequestFactoryInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Felipe Sayão Lobato Abreu <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class PsrHttpClientTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** @var RequestHandlerInterface|ObjectProphecy */ |
27
|
|
|
private $requestHandler; |
28
|
|
|
|
29
|
|
|
/** @var ServerRequestFactoryInterface|ObjectProphecy */ |
30
|
|
|
private $serverRequestFactory; |
31
|
|
|
|
32
|
|
|
/** @var RequestInterface|ObjectProphecy */ |
33
|
|
|
private $request; |
34
|
|
|
|
35
|
|
|
/** @var ServerRequestInterface|ObjectProphecy */ |
36
|
|
|
private $serverRequest; |
37
|
|
|
|
38
|
|
|
/** @var ResponseInterface|ObjectProphecy */ |
39
|
|
|
private $response; |
40
|
|
|
|
41
|
|
|
public function setUp(): void |
42
|
|
|
{ |
43
|
|
|
$this->requestHandler = $this->prophesize(RequestHandlerInterface::class); |
44
|
|
|
$this->serverRequestFactory = $this->prophesize(ServerRequestFactoryInterface::class); |
45
|
|
|
$this->request = $this->prophesize(RequestInterface::class); |
46
|
|
|
$this->serverRequest = $this->prophesize(ServerRequestInterface::class); |
47
|
|
|
$this->response = $this->prophesize(ResponseInterface::class); |
48
|
|
|
|
49
|
|
|
$method = 'GET'; |
50
|
|
|
$uri = 'http://google.com'; |
51
|
|
|
|
52
|
|
|
$this->request->getMethod()->willReturn($method); |
53
|
|
|
$this->request->getUri()->willReturn($uri); |
54
|
|
|
|
55
|
|
|
$this->serverRequestFactory->createServerRequest($method, $uri, $_SERVER)->will([$this->serverRequest, 'reveal']); |
56
|
|
|
|
57
|
|
|
$this->requestHandler->handle($this->request->reveal())->will([$this->response, 'reveal']); |
58
|
|
|
$this->requestHandler->handle($this->serverRequest->reveal())->will([$this->response, 'reveal']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testImplementClientInterface() |
62
|
|
|
{ |
63
|
|
|
$client = new PsrHttpClient($this->requestHandler->reveal()); |
64
|
|
|
$this->assertInstanceOf(ClientInterface::class, $client); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testConstructorWithoutServerRequestFactory() |
68
|
|
|
{ |
69
|
|
|
$client = new PsrHttpClient($this->requestHandler->reveal()); |
70
|
|
|
$this->assertInstanceOf(PsrHttpClient::class, $client); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testConstructorWithServerRequestFactory() |
74
|
|
|
{ |
75
|
|
|
$client = new PsrHttpClient($this->requestHandler->reveal(), $this->serverRequestFactory->reveal()); |
76
|
|
|
$this->assertInstanceOf(PsrHttpClient::class, $client); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testSendServerRequestReturnResponse() |
80
|
|
|
{ |
81
|
|
|
$client = new PsrHttpClient($this->requestHandler->reveal()); |
82
|
|
|
$response = $client->sendRequest($this->serverRequest->reveal()); |
83
|
|
|
|
84
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testSendRequestReturnResponse() |
88
|
|
|
{ |
89
|
|
|
$client = new PsrHttpClient($this->requestHandler->reveal(), $this->serverRequestFactory->reveal()); |
90
|
|
|
$response = $client->sendRequest($this->request->reveal()); |
91
|
|
|
|
92
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|