Failed Conditions
Pull Request — master (#3)
by Sergey
02:12
created

ConnectionTest::testException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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);
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Client::__call() has been deprecated: Client::__call will be removed in guzzlehttp/guzzle:8.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
        /** @scrutinizer ignore-deprecated */ $this->clientMock->method('request')->willThrowException($exceptionMock);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Client::__call() has been deprecated: Client::__call will be removed in guzzlehttp/guzzle:8.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

53
        /** @scrutinizer ignore-deprecated */ $this->clientMock->method('request')->willReturn($response);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Client::__call() has been deprecated: Client::__call will be removed in guzzlehttp/guzzle:8.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

68
        /** @scrutinizer ignore-deprecated */ $this->clientMock->method('request')->willReturn($response);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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);
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Client::__call() has been deprecated: Client::__call will be removed in guzzlehttp/guzzle:8.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

82
        /** @scrutinizer ignore-deprecated */ $this->clientMock->method('request')->willReturn($response);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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
The expression return $response returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface.
Loading history...
103
    }
104
}
105