sergeynezbritskiy /
nova-poshta-api
| 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 | $this->clientMock->method('request')->willThrowException($exceptionMock); |
||
| 40 | $this->expectException(NovaPoshtaApiException::class); |
||
| 41 | $this->expectExceptionMessage('Connection to Nova Poshta API failed: Test'); |
||
| 42 | $this->object->post('model', 'method'); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @throws Exception |
||
| 47 | */ |
||
| 48 | public function testInvalidResponseCode(): void |
||
| 49 | { |
||
| 50 | $response = $this->createMock(ResponseInterface::class); |
||
| 51 | $response->method('getStatusCode')->willReturn(201); |
||
| 52 | $response->method('getReasonPhrase')->willReturn('Test'); |
||
| 53 | $this->clientMock->method('request')->willReturn($response); |
||
| 54 | |||
| 55 | $this->expectException(NovaPoshtaApiException::class); |
||
| 56 | $this->expectExceptionMessage('Connection to Nova Poshta API failed: Test'); |
||
| 57 | |||
| 58 | $this->object->post('model', 'method'); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @throws Exception |
||
| 63 | */ |
||
| 64 | public function testFalsySuccessStatus(): void |
||
| 65 | { |
||
| 66 | $contentJson = json_encode(['success' => false, 'errors' => ['Test Error']]); |
||
| 67 | $response = $this->createResponse($contentJson); |
||
| 68 | $this->clientMock->method('request')->willReturn($response); |
||
| 69 | |||
| 70 | $this->expectException(NovaPoshtaApiException::class); |
||
| 71 | $this->expectExceptionMessage('Connection to Nova Poshta API failed: Test Error'); |
||
| 72 | |||
| 73 | $this->object->post('model', 'method'); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @throws Exception |
||
| 78 | */ |
||
| 79 | public function testInvalidBody(): void |
||
| 80 | { |
||
| 81 | $response = $this->createResponse('NotAJson'); |
||
| 82 | $this->clientMock->method('request')->willReturn($response); |
||
| 83 | |||
| 84 | $this->expectException(NovaPoshtaApiException::class); |
||
| 85 | $this->expectExceptionMessage('Invalid response from Nova Poshta API'); |
||
| 86 | |||
| 87 | $this->object->post('model', 'method'); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $content |
||
| 92 | * @return ResponseInterface |
||
| 93 | * @throws Exception |
||
| 94 | */ |
||
| 95 | private function createResponse(string $content): ResponseInterface |
||
| 96 | { |
||
| 97 | $response = $this->createMock(ResponseInterface::class); |
||
| 98 | $response->method('getStatusCode')->willReturn(200); |
||
| 99 | $bodyMock = $this->createMock(StreamInterface::class); |
||
| 100 | $bodyMock->method('getContents')->willReturn($content); |
||
| 101 | $response->method('getBody')->willReturn($bodyMock); |
||
| 102 | return $response; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 103 | } |
||
| 104 | } |
||
| 105 |