Passed
Push — master ( 0c1970...c53e9c )
by Ross
02:31
created

willThrowAnExceptionIfCloudFlareReportsAnError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 Symfony\Component\Console\Exception\LogicException;
30
31
/**
32
 * Class ListZonesTest
33
 * @package RossMitchell\UpdateCloudFlare\Tests\Responses
34
 */
35
class ListZonesTest extends AbstractTestClass
36
{
37
    /**
38
     * @Inject
39
     * @var ListZoneFactory
40
     */
41
    private $factory;
42
43
    /**
44
     * @test
45
     */
46
    public function canCreateTheClassUsingTheFactory()
47
    {
48
        $class = $this->createClass();
49
        $this->assertInstanceOf(ListZones::class, $class);
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function canReturnTheSuccess()
56
    {
57
        $class = $this->createClass();
58
        $this->assertTrue($class->isSuccess());
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function canReturnTheErrors()
65
    {
66
        $class  = $this->createClass();
67
        $errors = $class->getErrors();
68
        $this->assertInternalType('array', $errors);
69
        $this->assertEmpty($errors);
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function canReturnTheMessages()
76
    {
77
        $class    = $this->createClass();
78
        $messages = $class->getMessages();
79
        $this->assertInternalType('array', $messages);
80
        $this->assertEmpty($messages);
81
    }
82
83
    /**
84
     * @test
85
     */
86
    public function canReturnTheResult()
87
    {
88
        $class  = $this->createClass();
89
        $result = $class->getResult();
90
        $this->assertInternalType('array', $result);
91
        $this->assertCount(1, $result);
92
        $resultObject = $result[0];
93
        $this->assertInstanceOf(ListZonesResult::class, $resultObject);
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function canReturnTheResultInfo()
100
    {
101
        $class      = $this->createClass();
102
        $resultInfo = $class->getResultInfo();
103
        $this->assertInstanceOf(\stdClass::class, $resultInfo);
104
        $this->assertEquals(1, $resultInfo->page);
105
        $this->assertEquals(20, $resultInfo->per_page);
106
        $this->assertEquals(1, $resultInfo->count);
107
        $this->assertEquals(2000, $resultInfo->total_count);
108
    }
109
110
    /**
111
     * @test
112
     */
113
    public function willThrowAnExceptionIfTheSuccessIsMissing()
114
    {
115
        $json = $this->getJson();
116
        unset($json->success);
117
        $this->expectException(LogicException::class);
118
        $this->createClass($json);
119
    }
120
121
    /**
122
     * @test
123
     */
124
    public function willThrowAnExceptionIfTheErrorsAreMissing()
125
    {
126
        $json = $this->getJson();
127
        unset($json->errors);
128
        $this->expectException(LogicException::class);
129
        $this->createClass($json);
130
    }
131
132
    /**
133
     * @test
134
     */
135
    public function willThrowAnExceptionIfTheMessagesAreMissing()
136
    {
137
        $json = $this->getJson();
138
        unset($json->messages);
139
        $this->expectException(LogicException::class);
140
        $this->createClass($json);
141
    }
142
143
    /**
144
     * @test
145
     */
146
    public function willThrowAnExceptionIfTheResultIsMissing()
147
    {
148
        $json = $this->getJson();
149
        unset($json->result);
150
        $this->expectException(LogicException::class);
151
        $this->createClass($json);
152
    }
153
154
    /**
155
     * @test
156
     */
157
    public function willNotThrowAnExceptionIfTheResultInfoIsMissing()
158
    {
159
        $json = $this->getJson();
160
        unset($json->result_info);
161
        $class = $this->createClass($json);
162
        $this->assertNull($class->getResultInfo());
163
    }
164
165
    /**
166
     * @test
167
     */
168
    public function willThrowAnExceptionIfCloudFlareReportsAnError()
169
    {
170
        $json = $this->getErrorJson();
171
        $this->expectException(CloudFlareException::class);
172
        $this->createClass($json);
173
    }
174
175
    /**
176
     * @param \stdClass|null $json
177
     *
178
     * @return ListZones
179
     */
180
    private function createClass(\stdClass $json = null): ListZones
181
    {
182
        if ($json === null) {
183
            $json = $this->getJson();
184
        }
185
186
        return $this->factory->create($json);
187
    }
188
189
    /**
190
     * @return \stdClass
191
     */
192
    private function getJson(): \stdClass
193
    {
194
        $json = <<<JSON
195
{
196
  "success": true,
197
  "errors": [
198
    {}
199
  ],
200
  "messages": [
201
    {}
202
  ],
203
  "result": [
204
    {
205
      "id": "023e105f4ecef8ad9ca31a8372d0c353",
206
      "name": "example.com",
207
      "development_mode": 7200,
208
      "original_name_servers": [
209
        "ns1.originaldnshost.com",
210
        "ns2.originaldnshost.com"
211
      ],
212
      "original_registrar": "GoDaddy",
213
      "original_dnshost": "NameCheap",
214
      "created_on": "2014-01-01T05:20:00.12345Z",
215
      "modified_on": "2014-01-01T05:20:00.12345Z",
216
      "owner": {
217
        "id": "7c5dae5552338874e5053f2534d2767a",
218
        "email": "[email protected]",
219
        "owner_type": "user"
220
      },
221
      "permissions": [
222
        "#zone:read",
223
        "#zone:edit"
224
      ],
225
      "plan": {
226
        "id": "e592fd9519420ba7405e1307bff33214",
227
        "name": "Pro Plan",
228
        "price": 20,
229
        "currency": "USD",
230
        "frequency": "monthly",
231
        "legacy_id": "pro",
232
        "is_subscribed": true,
233
        "can_subscribe": true
234
      },
235
      "plan_pending": {
236
        "id": "e592fd9519420ba7405e1307bff33214",
237
        "name": "Pro Plan",
238
        "price": 20,
239
        "currency": "USD",
240
        "frequency": "monthly",
241
        "legacy_id": "pro",
242
        "is_subscribed": true,
243
        "can_subscribe": true
244
      },
245
      "status": "active",
246
      "paused": false,
247
      "type": "full",
248
      "name_servers": [
249
        "tony.ns.cloudflare.com",
250
        "woz.ns.cloudflare.com"
251
      ]
252
    }
253
  ],
254
  "result_info": {
255
    "page": 1,
256
    "per_page": 20,
257
    "count": 1,
258
    "total_count": 2000
259
  }
260
}
261
JSON;
262
263
        return \json_decode($json);
264
    }
265
266
    /**
267
     * @return \stdClass
268
     */
269
    private function getErrorJson(): \stdClass
270
    {
271
        $json = <<<JSON
272
{
273
    "result": null,
274
    "success": false,
275
    "errors": [{"code":1003,"message":"Invalid or missing zone id."}],
276
    "messages": []
277
}
278
JSON;
279
280
        return \json_decode($json);
281
    }
282
}
283