|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\Http\{Client\ClientFactory, Client\Psr18Exception, ClientRequest, ServerRequest}; |
|
6
|
|
|
use Koded\Http\Interfaces\{ClientType, HttpMethod, HttpStatus}; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Http\Client\{ClientExceptionInterface, ClientInterface}; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @group internet |
|
12
|
|
|
*/ |
|
13
|
|
|
class Psr18Test extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @dataProvider clients |
|
17
|
|
|
* |
|
18
|
|
|
* @param ClientInterface $client |
|
19
|
|
|
* |
|
20
|
|
|
* @throws ClientExceptionInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
public function test_should_fail_with_server_request_instance($client) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->expectException(Psr18Exception::class); |
|
25
|
|
|
$this->expectExceptionCode(HttpStatus::FAILED_DEPENDENCY); |
|
26
|
|
|
|
|
27
|
|
|
$client->sendRequest(new ServerRequest); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @dataProvider clients |
|
32
|
|
|
* |
|
33
|
|
|
* @param ClientInterface $client |
|
34
|
|
|
* |
|
35
|
|
|
* @throws ClientExceptionInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
public function test_should_pass_with_client_request_instance($client) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->markTestSkipped(); |
|
40
|
|
|
|
|
41
|
|
|
// $response = $client->sendRequest(new ClientRequest('GET', 'http://example.com')); |
|
42
|
|
|
$response = $client->sendRequest(new ClientRequest(HttpMethod::GET, 'http://example.com')); |
|
43
|
|
|
$this->assertSame(HttpStatus::OK, $response->getStatusCode()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @dataProvider clients |
|
48
|
|
|
* |
|
49
|
|
|
* @param ClientInterface $client |
|
50
|
|
|
* |
|
51
|
|
|
* @throws ClientExceptionInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
public function test_exception_with_client_request_instance_and_empty_url($client) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->expectException(Psr18Exception::class); |
|
56
|
|
|
$this->expectExceptionCode(HttpStatus::FAILED_DEPENDENCY); |
|
57
|
|
|
|
|
58
|
|
|
$client->sendRequest(new ClientRequest(HttpMethod::GET, '')); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @dataProvider clients |
|
63
|
|
|
* |
|
64
|
|
|
* @param ClientInterface $client |
|
65
|
|
|
* |
|
66
|
|
|
* @throws ClientExceptionInterface |
|
67
|
|
|
*/ |
|
68
|
|
|
public function test_exception_class_methods($client) |
|
69
|
|
|
{ |
|
70
|
|
|
$request = new ClientRequest(HttpMethod::GET, ''); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$client->sendRequest($request); |
|
74
|
|
|
} catch (\Exception $e) { |
|
75
|
|
|
$this->assertInstanceOf(Psr18Exception::class, $e);; |
|
76
|
|
|
$this->assertSame($request, $e->getRequest()); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function clients() |
|
81
|
|
|
{ |
|
82
|
|
|
return [ |
|
83
|
|
|
[ |
|
84
|
|
|
(new ClientFactory(ClientType::PHP)) |
|
85
|
|
|
->client() |
|
86
|
|
|
->timeout(5) |
|
87
|
|
|
->maxRedirects(3) |
|
88
|
|
|
], |
|
89
|
|
|
[ |
|
90
|
|
|
(new ClientFactory(ClientType::CURL)) |
|
91
|
|
|
->client() |
|
92
|
|
|
->timeout(5) |
|
93
|
|
|
->maxRedirects(3) |
|
94
|
|
|
] |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function setUp(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'] = ''; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|