DnsRecordsTest::canCreateTheClassUsingTheFactory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
26
use RossMitchell\UpdateCloudFlare\Exceptions\CloudFlareException;
27
use RossMitchell\UpdateCloudFlare\Factories\Responses\DnsRecordsFactory;
28
use RossMitchell\UpdateCloudFlare\Responses\DnsRecords;
29
use RossMitchell\UpdateCloudFlare\Responses\Results\DnsRecord;
30
use RossMitchell\UpdateCloudFlare\Tests\AbstractTestClass;
31
use RossMitchell\UpdateCloudFlare\Tests\Fakes\Helpers\DnsRecordsResponse;
32
use Symfony\Component\Console\Exception\LogicException;
33
34
/**
35
 * Class DnsRecordsTest
36
 * @testdox RossMitchell\UpdateCloudFlare\Responses\DnsRecords
37
 * @package RossMitchell\UpdateCloudFlare\Tests\Responses
38
 */
39
class DnsRecordsTest extends AbstractTestClass
40
{
41
42
    /**
43
     * @Inject
44
     * @var DnsRecordsFactory
45
     */
46
    private $factory;
47
48
    /**
49
     * @Inject
50
     * @var DnsRecordsResponse
51
     */
52
    private $response;
53
54
    /**
55
     * @test
56
     */
57
    public function canCreateTheClassUsingTheFactory(): void
58
    {
59
        $class = $this->createClass();
60
        $this->assertInstanceOf(DnsRecords::class, $class);
61
    }
62
63
    /**
64
     * @test
65
     */
66
    public function canReturnTheSuccess(): void
67
    {
68
        $class = $this->createClass();
69
        $this->assertTrue($class->isSuccess());
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function canReturnTheErrors(): void
76
    {
77
        $class  = $this->createClass();
78
        $errors = $class->getErrors();
79
        $this->assertInternalType('array', $errors);
80
        $this->assertEmpty($errors);
81
    }
82
83
    /**
84
     * @test
85
     */
86
    public function canReturnTheMessages(): void
87
    {
88
        $class    = $this->createClass();
89
        $messages = $class->getMessages();
90
        $this->assertInternalType('array', $messages);
91
        $this->assertEmpty($messages);
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function canReturnTheResult(): void
98
    {
99
        $class  = $this->createClass();
100
        $result = $class->getResult();
101
        $this->assertInternalType('array', $result);
102
        $this->assertCount(1, $result);
103
        $resultObject = $result[0];
104
        $this->assertInstanceOf(DnsRecord::class, $resultObject);
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function canReturnTheResultInfo(): void
111
    {
112
        $class      = $this->createClass();
113
        $resultInfo = $class->getResultInfo();
114
        $this->assertInstanceOf(\stdClass::class, $resultInfo);
115
        $this->assertEquals(1, $resultInfo->page);
116
        $this->assertEquals(20, $resultInfo->per_page);
117
        $this->assertEquals(1, $resultInfo->count);
118
        $this->assertEquals(2000, $resultInfo->total_count);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function willThrowAnExceptionIfTheSuccessIsMissing(): void
125
    {
126
        $json = $this->getJson();
127
        unset($json->success);
128
        $this->expectException(LogicException::class);
129
        $this->createClass($json);
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function willThrowAnExceptionIfTheErrorsAreMissing(): void
136
    {
137
        $json = $this->getJson();
138
        unset($json->errors);
139
        $this->expectException(LogicException::class);
140
        $this->createClass($json);
141
    }
142
143
    /**
144
     * @test
145
     */
146
    public function willThrowAnExceptionIfTheMessagesAreMissing(): void
147
    {
148
        $json = $this->getJson();
149
        unset($json->messages);
150
        $this->expectException(LogicException::class);
151
        $this->createClass($json);
152
    }
153
154
    /**
155
     * @test
156
     */
157
    public function willThrowAnExceptionIfTheResultIsMissing(): void
158
    {
159
        $json = $this->getJson();
160
        unset($json->result);
161
        $this->expectException(LogicException::class);
162
        $this->createClass($json);
163
    }
164
165
    /**
166
     * @test
167
     */
168
    public function willNotThrowAnExceptionIfTheResultInfoIsMissing(): void
169
    {
170
        $json = $this->getJson();
171
        unset($json->result_info);
172
        $class = $this->createClass($json);
173
        $this->assertNull($class->getResultInfo());
174
    }
175
176
    /**
177
     * @test
178
     */
179
    public function willThrowAnExceptionIfCloudFlareReportsAnError(): void
180
    {
181
        $error = '{"code":1003,"message":"Invalid or missing zone id."}';
182
        $json  = $this->getJson('false', $error);
183
        $this->expectException(CloudFlareException::class);
184
        $this->createClass($json);
185
    }
186
187
    /**
188
     * @param \stdClass|null $json
189
     *
190
     * @return DnsRecords
191
     * @throws \RossMitchell\UpdateCloudFlare\Exceptions\CloudFlareException
192
     */
193
    private function createClass(\stdClass $json = null): DnsRecords
194
    {
195
        if ($json === null) {
196
            $json = $this->getJson();
197
        }
198
199
        return $this->factory->create($json);
200
    }
201
202
    /**
203
     * @param string $success
204
     * @param string $error
205
     *
206
     * @return \stdClass
207
     */
208
    private function getJson(string $success = 'true', string $error = '{}'): \stdClass
209
    {
210
        $json = $this->response->getFullJson($success, $error);
211
212
        return \json_decode($json);
213
    }
214
}
215