Completed
Push — master ( 4eaf6e...a4940f )
by Ross
02:35
created

willThrowAnExceptionIfTheSuccessIsMissing()   A

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