Passed
Push — master ( 9c0ce0...43ac81 )
by Alex
52s queued 11s
created

resultDispatchingDataProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Mezon\CustomClient\Tests;
4
5
use Exception;
6
use PHPUnit\Framework\TestCase;
7
use Mezon\CustomClient\CustomClient;
8
9
class CustomClientUnitTest extends TestCase
10
{
11
12
    /**
13
     * Testing invalid construction
14
     */
15
    public function testConstructorInvalid(): void
16
    {
17
        // assertions
18
        $this->expectException(Exception::class);
19
20
        // setup and test body
21
        $client = new CustomClient('');
0 ignored issues
show
Unused Code introduced by
The assignment to $client is dead and can be removed.
Loading history...
22
    }
23
24
    /**
25
     * Testing valid construction
26
     */
27
    public function testConstructorValid(): void
28
    {
29
        $client = new CustomClient('http://yandex.ru/', [
30
            'header'
31
        ]);
32
33
        $this->assertEquals('http://yandex.ru', $client->getUrl(), 'Invalid URL');
34
        $this->assertEquals(1, count($client->getHeaders()), 'Invalid headers');
35
    }
36
37
    /**
38
     * Testing getters/setters for the field
39
     */
40
    public function testIdempotencyGetSet(): void
41
    {
42
        // setup
43
        $client = new CustomClient('some url', []);
44
45
        // test bodyand assertions
46
        $client->setIdempotencyKey('i-key');
47
48
        $this->assertEquals('i-key', $client->getIdempotencyKey(), 'Invalid idempotency key');
49
    }
50
51
    /**
52
     * Creating mock
53
     */
54
    protected function getMock(): object
55
    {
56
        return $this->getMockBuilder(CustomClient::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

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

56
        return /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(CustomClient::class)

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...
57
            ->setMethods([
58
            'sendRequest'
59
        ])
60
            ->disableOriginalConstructor()
61
            ->getMock();
62
    }
63
64
    /**
65
     * Data provider for sending requests tests
66
     *
67
     * @return array test data
68
     */
69
    public function sendRequestDataProvider(): array
70
    {
71
        return [
72
            [
73
                'sendGetRequest'
74
            ],
75
            [
76
                'sendPostRequest'
77
            ],
78
            [
79
                'sendPutRequest'
80
            ],
81
            [
82
                'sendDeleteRequest'
83
            ]
84
        ];
85
    }
86
87
    /**
88
     * Testing sendGetRequest method
89
     *
90
     * @param string $methodName
91
     *            Method name to be tested
92
     * @dataProvider sendRequestDataProvider
93
     */
94
    public function testSendRequest(string $methodName): void
95
    {
96
        // setup
97
        $client = $this->getMock();
98
        $client->method('sendRequest')->willReturn([
99
            'return',
100
            1
101
        ]);
102
103
        // test body
104
        $result = $client->$methodName('/end-point/');
105
106
        // assertions
107
        $this->assertEquals('return', $result);
108
    }
109
110
    /**
111
     * Data provider for the test testResultDispatching
112
     *
113
     * @return array testing data
114
     */
115
    public function resultDispatchingDataProvider(): array
116
    {
117
        $codes = [
118
            400,
119
            403,
120
            404,
121
            0
122
        ];
123
        $methods = [
124
            'sendGetRequest',
125
            'sendPostRequest',
126
            'sendPutRequest',
127
            'sendDeleteRequest'
128
        ];
129
        $return = [];
130
131
        foreach ($codes as $code) {
132
            foreach ($methods as $method) {
133
                $return[] = [
134
                    $code,
135
                    $method
136
                ];
137
            }
138
        }
139
140
        return $return;
141
    }
142
143
    /**
144
     * Testing result dispatcher
145
     *
146
     * @param int $httpCode
147
     *            code of the response
148
     * @param string $methodName
149
     *            name of the testing methodd
150
     * @dataProvider resultDispatchingDataProvider
151
     */
152
    public function testResultDispatching(int $httpCode, string $methodName): void
153
    {
154
        // setup
155
        $client = $this->getMock();
156
        $client->method('sendRequest')->willReturn([
157
            'result',
158
            $httpCode
159
        ]);
160
161
        // assertions
162
        $this->expectException(\Exception::class);
163
164
        // test body
165
        $client->$methodName('/end-point/');
166
    }
167
168
    /**
169
     * Testing setting idempotency key
170
     */
171
    public function testSetIdempotencyKey(): void
172
    {
173
        // setup
174
        $client = new TestClient('http://unit.test');
175
        $client->setIdempotencyKey('iKey');
176
177
        // test body and assertions
178
        $this->assertStringContainsString('iKey', implode('', $client->getCommonHeadersPublic()));
179
    }
180
}
181