1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SomeWork\Minjust\Tests\Unit; |
6
|
|
|
|
7
|
|
|
use DivineOmega\Psr18GuzzleAdapter\Client as GuzzleClient; |
8
|
|
|
use DivineOmega\Psr18GuzzleAdapter\Exceptions\ClientException; |
9
|
|
|
use Http\Factory\Guzzle\RequestFactory; |
10
|
|
|
use Iterator; |
11
|
|
|
use PHPUnit\Framework\MockObject\Rule\InvokedCount as InvokedCountMatcher; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Psr\Http\Client\ClientInterface; |
14
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use ReflectionClass; |
18
|
|
|
use ReflectionException; |
19
|
|
|
use SomeWork\Minjust\Client; |
20
|
|
|
use SomeWork\Minjust\Exception\HttpClientException; |
21
|
|
|
use SomeWork\Minjust\Exception\WrongStatusCodeException; |
22
|
|
|
use SomeWork\Minjust\FindRequest; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @coversDefaultClass \SomeWork\Minjust\Client |
26
|
|
|
*/ |
27
|
|
|
class ClientTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @covers ::__construct |
31
|
|
|
* @throws ReflectionException |
32
|
|
|
*/ |
33
|
|
|
public function testConstruct(): void |
34
|
|
|
{ |
35
|
|
|
$httpClient = $this->createMock(ClientInterface::class); |
36
|
|
|
$requestFactory = $this->createMock(RequestFactoryInterface::class); |
37
|
|
|
|
38
|
|
|
$client = new Client($httpClient, $requestFactory); |
39
|
|
|
|
40
|
|
|
$ref = new ReflectionClass(Client::class); |
41
|
|
|
$clientProperty = $ref->getProperty('client'); |
42
|
|
|
$requestFactoryProperty = $ref->getProperty('requestFactory'); |
43
|
|
|
|
44
|
|
|
$clientProperty->setAccessible(true); |
45
|
|
|
$requestFactoryProperty->setAccessible(true); |
46
|
|
|
|
47
|
|
|
$this->assertEquals($httpClient, $clientProperty->getValue($client)); |
48
|
|
|
$this->assertEquals($requestFactory, $requestFactoryProperty->getValue($client)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @covers ::list |
53
|
|
|
* @dataProvider listProvider |
54
|
|
|
* |
55
|
|
|
* @param array $formData |
56
|
|
|
* @param string $search |
57
|
|
|
*/ |
58
|
|
|
public function testList(array $formData, string $search): void |
59
|
|
|
{ |
60
|
|
|
$client = new Client( |
61
|
|
|
new GuzzleClient(), |
62
|
|
|
new RequestFactory() |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$body = $client->list($formData); |
66
|
|
|
$this->assertStringContainsString($search, $body); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function listProvider(): Iterator |
70
|
|
|
{ |
71
|
|
|
yield 'empty' => [ |
72
|
|
|
'formData' => [], |
73
|
|
|
'search' => '01/102', |
74
|
|
|
]; |
75
|
|
|
yield 'Михайлов' => [ |
76
|
|
|
'formData' => [ |
77
|
|
|
FindRequest::FULL_NAME => 'михайлов олег николаевич', |
78
|
|
|
FindRequest::TERRITORIAL_SUBJECT => '77', |
79
|
|
|
], |
80
|
|
|
'search' => '77/2340', |
81
|
|
|
]; |
82
|
|
|
yield 'Белоусова Надежда Сергеевна' => [ |
83
|
|
|
'formData' => [ |
84
|
|
|
FindRequest::PAGE => 1, |
85
|
|
|
FindRequest::FULL_NAME => 'б', |
86
|
|
|
FindRequest::STATUS => 4, |
87
|
|
|
], |
88
|
|
|
'search' => '03/2165', |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @covers ::detail |
94
|
|
|
* @dataProvider detailProvider |
95
|
|
|
* |
96
|
|
|
* @param string $url |
97
|
|
|
* @param string $search |
98
|
|
|
*/ |
99
|
|
|
public function testDetail(string $url, string $search): void |
100
|
|
|
{ |
101
|
|
|
$client = new Client( |
102
|
|
|
new GuzzleClient(), |
103
|
|
|
new RequestFactory() |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$body = $client->detail($url); |
107
|
|
|
$this->assertStringContainsString($search, $body); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function detailProvider(): Iterator |
111
|
|
|
{ |
112
|
|
|
yield 'Михайлов' => [ |
113
|
|
|
'url' => '/lawyers/show/1610663', |
114
|
|
|
'search' => 'Адвокатская палата г. Москвы', |
115
|
|
|
]; |
116
|
|
|
yield 'Белоусова Надежда Сергеевна' => [ |
117
|
|
|
'url' => '/lawyers/show/1625881', |
118
|
|
|
'search' => 'г. Уфа, ул. К.Маркса, 3б', |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @covers ::handleRequest |
124
|
|
|
*/ |
125
|
|
|
public function testHttpClientExceptionHandleRequest(): void |
126
|
|
|
{ |
127
|
|
|
$request = $this->createMock(RequestInterface::class); |
128
|
|
|
|
129
|
|
|
$exception = new ClientException('Test Message', 123); |
130
|
|
|
|
131
|
|
|
$httpClient = $this->createMock(ClientInterface::class); |
132
|
|
|
$requestFactory = $this->createMock(RequestFactoryInterface::class); |
133
|
|
|
|
134
|
|
|
$httpClient |
135
|
|
|
->expects($this->once()) |
136
|
|
|
->method('sendRequest') |
137
|
|
|
->with($request) |
138
|
|
|
->willThrowException($exception); |
139
|
|
|
|
140
|
|
|
$this->expectException(HttpClientException::class); |
141
|
|
|
$this->expectExceptionCode(123); |
142
|
|
|
$this->expectExceptionMessage('Test Message'); |
143
|
|
|
|
144
|
|
|
$client = new Client($httpClient, $requestFactory); |
145
|
|
|
/* @noinspection PhpUnhandledExceptionInspection */ |
146
|
|
|
$this->invokeMethod($client, 'handleRequest', [$request]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Call protected/private method of a class. |
151
|
|
|
* |
152
|
|
|
* @param object &$object Instantiated object that we will run method on. |
153
|
|
|
* @param string $methodName Method name to call |
154
|
|
|
* @param array $parameters Array of parameters to pass into method. |
155
|
|
|
* |
156
|
|
|
* @return mixed Method return. |
157
|
|
|
* @throws ReflectionException |
158
|
|
|
*/ |
159
|
|
|
public function invokeMethod($object, $methodName, array $parameters = []) |
160
|
|
|
{ |
161
|
|
|
$reflection = new ReflectionClass(get_class($object)); |
162
|
|
|
$method = $reflection->getMethod($methodName); |
163
|
|
|
$method->setAccessible(true); |
164
|
|
|
|
165
|
|
|
return $method->invokeArgs($object, $parameters); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @throws ReflectionException |
170
|
|
|
*/ |
171
|
|
|
public function testWrongStatusCodeException(): void |
172
|
|
|
{ |
173
|
|
|
$httpClient = $this->createMock(ClientInterface::class); |
174
|
|
|
$requestFactory = $this->createMock(RequestFactoryInterface::class); |
175
|
|
|
|
176
|
|
|
$request = $this->createMock(RequestInterface::class); |
177
|
|
|
$response = $this->createMock(ResponseInterface::class); |
178
|
|
|
$response |
179
|
|
|
->expects(new InvokedCountMatcher(2)) |
180
|
|
|
->method('getStatusCode') |
181
|
|
|
->willReturn(500); |
182
|
|
|
|
183
|
|
|
$httpClient |
184
|
|
|
->expects($this->once()) |
185
|
|
|
->method('sendRequest') |
186
|
|
|
->willReturn($response); |
187
|
|
|
|
188
|
|
|
$this->expectException(WrongStatusCodeException::class); |
189
|
|
|
|
190
|
|
|
$client = new Client($httpClient, $requestFactory); |
191
|
|
|
$this->invokeMethod($client, 'handleRequest', [$request]); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|