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