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