|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SergeyNezbritskiy\NovaPoshta\Tests\Unit; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\Exception\TransferException; |
|
9
|
|
|
use PHPUnit\Framework\MockObject\Exception; |
|
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
13
|
|
|
use Psr\Http\Message\StreamInterface; |
|
14
|
|
|
use SergeyNezbritskiy\NovaPoshta\Connection; |
|
15
|
|
|
use SergeyNezbritskiy\NovaPoshta\NovaPoshtaApiException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ConnectionTest |
|
19
|
|
|
* Unit tests for class \SergeyNezbritskiy\NovaPoshta\Connection |
|
20
|
|
|
* @see Connection |
|
21
|
|
|
*/ |
|
22
|
|
|
class ConnectionTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
private Connection $object; |
|
25
|
|
|
private Client|MockObject $clientMock; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @throws Exception |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function setUp(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->clientMock = $this->createMock(Client::class); |
|
33
|
|
|
$this->object = new Connection('api-key', $this->clientMock); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testException(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$exceptionMock = new TransferException('Test'); |
|
39
|
|
|
/** @scrutinizer ignore-deprecated */ |
|
40
|
|
|
$this->clientMock->method('request')->willThrowException($exceptionMock); |
|
41
|
|
|
$this->expectException(NovaPoshtaApiException::class); |
|
42
|
|
|
$this->expectExceptionMessage('Connection to Nova Poshta API failed: Test'); |
|
43
|
|
|
$this->object->post('model', 'method'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @throws Exception |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testInvalidResponseCode(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$response = $this->createMock(ResponseInterface::class); |
|
52
|
|
|
$response->method('getStatusCode')->willReturn(201); |
|
53
|
|
|
$response->method('getReasonPhrase')->willReturn('Test'); |
|
54
|
|
|
/** @scrutinizer ignore-deprecated */ |
|
55
|
|
|
$this->clientMock->method('request')->willReturn($response); |
|
56
|
|
|
|
|
57
|
|
|
$this->expectException(NovaPoshtaApiException::class); |
|
58
|
|
|
$this->expectExceptionMessage('Connection to Nova Poshta API failed: Test'); |
|
59
|
|
|
|
|
60
|
|
|
$this->object->post('model', 'method'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @throws Exception |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testFalsySuccessStatus(): void |
|
67
|
|
|
{ |
|
68
|
|
|
$contentJson = json_encode(['success' => false, 'errors' => ['Test Error']]); |
|
69
|
|
|
$response = $this->createResponse($contentJson); |
|
70
|
|
|
/** @scrutinizer ignore-deprecated */ |
|
71
|
|
|
$this->clientMock->method('request')->willReturn($response); |
|
72
|
|
|
|
|
73
|
|
|
$this->expectException(NovaPoshtaApiException::class); |
|
74
|
|
|
$this->expectExceptionMessage('Connection to Nova Poshta API failed: Test Error'); |
|
75
|
|
|
|
|
76
|
|
|
$this->object->post('model', 'method'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @throws Exception |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testInvalidBody(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$response = $this->createResponse('NotAJson'); |
|
85
|
|
|
/** @scrutinizer ignore-deprecated */ |
|
86
|
|
|
$this->clientMock->method('request')->willReturn($response); |
|
87
|
|
|
|
|
88
|
|
|
$this->expectException(NovaPoshtaApiException::class); |
|
89
|
|
|
$this->expectExceptionMessage('Invalid response from Nova Poshta API'); |
|
90
|
|
|
|
|
91
|
|
|
$this->object->post('model', 'method'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $content |
|
96
|
|
|
* @return ResponseInterface|MockObject |
|
97
|
|
|
* @throws Exception |
|
98
|
|
|
*/ |
|
99
|
|
|
private function createResponse(string $content): ResponseInterface|MockObject |
|
100
|
|
|
{ |
|
101
|
|
|
$response = $this->createMock(ResponseInterface::class); |
|
102
|
|
|
$response->method('getStatusCode')->willReturn(200); |
|
103
|
|
|
$bodyMock = $this->createMock(StreamInterface::class); |
|
104
|
|
|
$bodyMock->method('getContents')->willReturn($content); |
|
105
|
|
|
$response->method('getBody')->willReturn($bodyMock); |
|
106
|
|
|
return $response; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|