Passed
Pull Request — master (#4)
by Igor
04:25
created

ClientTest::testWrongStatusCodeException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 22
rs 9.7666
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
            ],
79
            'search'   => '77/2340',
80
        ];
81
        yield 'Белоусова Надежда Сергеевна' => [
82
            'formData' => [
83
                FindRequest::PAGE      => 1,
84
                FindRequest::FULL_NAME => 'б',
85
                FindRequest::STATUS    => 4,
86
            ],
87
            'search'   => '03/2165',
88
        ];
89
    }
90
91
    /**
92
     * @covers ::detail
93
     * @dataProvider detailProvider
94
     *
95
     * @param string $url
96
     * @param string $search
97
     */
98
    public function testDetail(string $url, string $search): void
99
    {
100
        $client = new Client(
101
            new GuzzleClient(),
102
            new RequestFactory()
103
        );
104
105
        $body = $client->detail($url);
106
        $this->assertStringContainsString($search, $body);
107
    }
108
109
    public function detailProvider(): Iterator
110
    {
111
        yield 'Михайлов' => [
112
            'url'    => '/lawyers/show/1610663',
113
            'search' => 'Адвокатская палата г. Москвы',
114
        ];
115
        yield 'Белоусова Надежда Сергеевна' => [
116
            'url'    => '/lawyers/show/1625881',
117
            'search' => 'г. Уфа, ул. К.Маркса, 3б',
118
        ];
119
    }
120
121
    /**
122
     * @covers ::handleRequest
123
     */
124
    public function testHttpClientExceptionHandleRequest(): void
125
    {
126
        $request = $this->createMock(RequestInterface::class);
127
128
        $exception = new ClientException('Test Message', 123);
129
130
        $httpClient = $this->createMock(ClientInterface::class);
131
        $requestFactory = $this->createMock(RequestFactoryInterface::class);
132
133
        $httpClient
134
            ->expects($this->once())
135
            ->method('sendRequest')
136
            ->with($request)
137
            ->willThrowException($exception);
138
139
        $this->expectException(HttpClientException::class);
140
        $this->expectExceptionCode(123);
141
        $this->expectExceptionMessage('Test Message');
142
143
        $client = new Client($httpClient, $requestFactory);
144
        /* @noinspection PhpUnhandledExceptionInspection */
145
        $this->invokeMethod($client, 'handleRequest', [$request]);
146
    }
147
148
    /**
149
     * Call protected/private method of a class.
150
     *
151
     * @param object &$object     Instantiated object that we will run method on.
152
     * @param string  $methodName Method name to call
153
     * @param array   $parameters Array of parameters to pass into method.
154
     *
155
     * @return mixed Method return.
156
     * @throws ReflectionException
157
     */
158
    public function invokeMethod(&$object, $methodName, array $parameters = [])
159
    {
160
        $reflection = new ReflectionClass(get_class($object));
161
        $method = $reflection->getMethod($methodName);
162
        $method->setAccessible(true);
163
164
        return $method->invokeArgs($object, $parameters);
165
    }
166
167
    /**
168
     * @throws \ReflectionException
169
     */
170
    public function testWrongStatusCodeException(): void
171
    {
172
        $httpClient = $this->createMock(ClientInterface::class);
173
        $requestFactory = $this->createMock(RequestFactoryInterface::class);
174
175
        $request = $this->createMock(RequestInterface::class);
176
        $response = $this->createMock(ResponseInterface::class);
177
        $response
178
            ->expects(new InvokedCountMatcher(2))
179
            ->method('getStatusCode')
180
            ->willReturn(500);
181
182
        $httpClient
183
            ->expects($this->once())
184
            ->method('sendRequest')
185
            ->willReturn($response);
186
187
        $this->expectException(WrongStatusCodeException::class);
188
189
        $client = new Client($httpClient, $requestFactory);
190
        /* @noinspection PhpUnhandledExceptionInspection */
191
        $this->invokeMethod($client, 'handleRequest', [$request]);
192
    }
193
}
194