willThrowAnExceptionIfTheResultIsMissing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
/**
3
 *
4
 * Copyright (C) 2018  Ross Mitchell
5
 *
6
 * This file is part of RossMitchell/UpdateCloudFlare.
7
 *
8
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
9
 * it and/or modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
namespace RossMitchell\UpdateCloudFlare\Tests\Responses;
23
24
use RossMitchell\UpdateCloudFlare\Exceptions\CloudFlareException;
25
use RossMitchell\UpdateCloudFlare\Factories\Responses\ListZoneFactory;
26
use RossMitchell\UpdateCloudFlare\Responses\ListZones;
27
use RossMitchell\UpdateCloudFlare\Responses\Results\ListZonesResult;
28
use RossMitchell\UpdateCloudFlare\Tests\AbstractTestClass;
29
use RossMitchell\UpdateCloudFlare\Tests\Fakes\Helpers\ListZonesResponse;
30
use Symfony\Component\Console\Exception\LogicException;
31
32
/**
33
 * Class ListZonesTest
34
 * @package RossMitchell\UpdateCloudFlare\Tests\Responses
35
 */
36
class ListZonesTest extends AbstractTestClass
37
{
38
    /**
39
     * @Inject
40
     * @var ListZoneFactory
41
     */
42
    private $factory;
43
44
    /**
45
     * @Inject
46
     * @var ListZonesResponse
47
     */
48
    private $responseHelper;
49
50
    /**
51
     * @test
52
     */
53
    public function canCreateTheClassUsingTheFactory(): void
54
    {
55
        $class = $this->createClass();
56
        $this->assertInstanceOf(ListZones::class, $class);
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function canReturnTheSuccess(): void
63
    {
64
        $class = $this->createClass();
65
        $this->assertTrue($class->isSuccess());
66
    }
67
68
    /**
69
     * @test
70
     */
71
    public function canReturnTheErrors(): void
72
    {
73
        $class  = $this->createClass();
74
        $errors = $class->getErrors();
75
        $this->assertInternalType('array', $errors);
76
        $this->assertEmpty($errors);
77
    }
78
79
    /**
80
     * @test
81
     */
82
    public function canReturnTheMessages(): void
83
    {
84
        $class    = $this->createClass();
85
        $messages = $class->getMessages();
86
        $this->assertInternalType('array', $messages);
87
        $this->assertEmpty($messages);
88
    }
89
90
    /**
91
     * @test
92
     */
93
    public function canReturnTheResult(): void
94
    {
95
        $class  = $this->createClass();
96
        $result = $class->getResult();
97
        $this->assertInternalType('array', $result);
98
        $this->assertCount(1, $result);
99
        $resultObject = $result[0];
100
        $this->assertInstanceOf(ListZonesResult::class, $resultObject);
101
    }
102
103
    /**
104
     * @test
105
     */
106
    public function canReturnTheResultInfo(): void
107
    {
108
        $class      = $this->createClass();
109
        $resultInfo = $class->getResultInfo();
110
        $this->assertInstanceOf(\stdClass::class, $resultInfo);
111
        $this->assertEquals(1, $resultInfo->page);
112
        $this->assertEquals(20, $resultInfo->per_page);
113
        $this->assertEquals(1, $resultInfo->count);
114
        $this->assertEquals(2000, $resultInfo->total_count);
115
    }
116
117
    /**
118
     * @test
119
     */
120
    public function willThrowAnExceptionIfTheSuccessIsMissing(): void
121
    {
122
        $json = $this->getJson();
123
        unset($json->success);
124
        $this->expectException(LogicException::class);
125
        $this->createClass($json);
126
    }
127
128
    /**
129
     * @test
130
     */
131
    public function willThrowAnExceptionIfTheErrorsAreMissing(): void
132
    {
133
        $json = $this->getJson();
134
        unset($json->errors);
135
        $this->expectException(LogicException::class);
136
        $this->createClass($json);
137
    }
138
139
    /**
140
     * @test
141
     */
142
    public function willThrowAnExceptionIfTheMessagesAreMissing(): void
143
    {
144
        $json = $this->getJson();
145
        unset($json->messages);
146
        $this->expectException(LogicException::class);
147
        $this->createClass($json);
148
    }
149
150
    /**
151
     * @test
152
     */
153
    public function willThrowAnExceptionIfTheResultIsMissing(): void
154
    {
155
        $json = $this->getJson();
156
        unset($json->result);
157
        $this->expectException(LogicException::class);
158
        $this->createClass($json);
159
    }
160
161
    /**
162
     * @test
163
     */
164
    public function canHandleAResponseWhenTheResultIsEmpty(): void
165
    {
166
        $json         = $this->getJson();
167
        $json->result = null;
168
        $class        = $this->createClass($json);
169
        $this->assertInternalType('array', $class->getResult());
170
        $this->assertCount(0, $class->getResult());
171
    }
172
173
    /**
174
     * @test
175
     */
176
    public function willNotThrowAnExceptionIfTheResultInfoIsMissing(): void
177
    {
178
        $json = $this->getJson();
179
        unset($json->result_info);
180
        $class = $this->createClass($json);
181
        $this->assertNull($class->getResultInfo());
182
    }
183
184
    /**
185
     * @test
186
     */
187
    public function willThrowAnExceptionIfCloudFlareReportsAnError(): void
188
    {
189
        $error = '{"code":1003,"message":"Invalid or missing zone id."}';
190
        $json  = $this->getJson('false', $error);
191
        $this->expectException(CloudFlareException::class);
192
        $this->createClass($json);
193
    }
194
195
    /**
196
     * @param \stdClass|null $json
197
     *
198
     * @return ListZones
199
     */
200
    private function createClass(\stdClass $json = null): ListZones
201
    {
202
        if ($json === null) {
203
            $json = $this->getJson();
204
        }
205
206
        return $this->factory->create($json);
207
    }
208
209
    /**
210
     * @param string $success
211
     * @param string $error
212
     *
213
     * @return \stdClass
214
     */
215
    private function getJson(string $success = 'true', string $error = '{}'): \stdClass
216
    {
217
        $json = $this->responseHelper->getFullJson($success, $error);
218
219
        return \json_decode($json);
220
    }
221
}
222