Completed
Push — master ( 4d3fdb...fd1b5e )
by Ross
02:40
created

UpdateDnsRecordsTest::createClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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