Passed
Push — master ( 6348d2...e389d8 )
by Ross
02:39
created

PlanTest::canCreateTheClassUsingTheFactory()   A

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 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\Results;
23
24
use RossMitchell\UpdateCloudFlare\Factories\Responses\Results\PlanFactory;
25
use RossMitchell\UpdateCloudFlare\Responses\Results\Plan;
26
use RossMitchell\UpdateCloudFlare\Tests\AbstractTestClass;
27
use Symfony\Component\Console\Exception\LogicException;
28
29
/**
30
 * Class PlanTest
31
 * @testdox RossMitchell\UpdateCloudFlare\Responses\Results\Plan
32
 * @package RossMitchell\UpdateCloudFlare\Tests\Responses\Results
33
 */
34
class PlanTest extends AbstractTestClass
35
{
36
    /**
37
     * @Inject
38
     * @var PlanFactory
39
     */
40
    private $factory;
41
42
    /**
43
     * @test
44
     */
45
    public function canCreateTheClassUsingTheFactory()
46
    {
47
        $class = $this->createClass();
48
        $this->assertInstanceOf(Plan::class, $class);
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function canReturnTheId()
55
    {
56
        $class = $this->createClass();
57
        $this->assertEquals('e592fd9519420ba7405e1307bff33214', $class->getId());
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function canReturnTheName()
64
    {
65
        $class = $this->createClass();
66
        $this->assertEquals('Pro Plan', $class->getName());
67
    }
68
69
    /**
70
     * @test
71
     */
72
    public function canReturnThePrice()
73
    {
74
        $class = $this->createClass();
75
        $this->assertEquals(20, $class->getPrice());
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function canReturnTheCurrency()
82
    {
83
        $class = $this->createClass();
84
        $this->assertEquals('USD', $class->getCurrency());
85
    }
86
87
    /**
88
     * @test
89
     */
90
    public function canReturnTheFrequency()
91
    {
92
        $class = $this->createClass();
93
        $this->assertEquals('monthly', $class->getFrequency());
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function canReturnTheLegacyId()
100
    {
101
        $class = $this->createClass();
102
        $this->assertEquals('pro', $class->getLegacyId());
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function canReturnTheIsSubscribed()
109
    {
110
        $class = $this->createClass();
111
        $this->assertTrue($class->getIsSubscribed());
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function canReturnTheCanSubscribe()
118
    {
119
        $class = $this->createClass();
120
        $this->assertTrue($class->getCanSubscribe());
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function willThrowAnExceptionIfTheIdIsMissing()
127
    {
128
        $json = $this->getJson();
129
        unset($json->id);
130
        $this->expectException(LogicException::class);
131
        $this->createClass($json);
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function willThrowAnExceptionIfTheNameIsMissing()
138
    {
139
        $json = $this->getJson();
140
        unset($json->name);
141
        $this->expectException(LogicException::class);
142
        $this->createClass($json);
143
    }
144
145
    /**
146
     * @test
147
     */
148
    public function willThrowAnExceptionIfThePriceIsMissing()
149
    {
150
        $json = $this->getJson();
151
        unset($json->price);
152
        $this->expectException(LogicException::class);
153
        $this->createClass($json);
154
    }
155
156
    /**
157
     * @test
158
     */
159
    public function willThrowAnExceptionIfTheCurrencyIsMissing()
160
    {
161
        $json = $this->getJson();
162
        unset($json->currency);
163
        $this->expectException(LogicException::class);
164
        $this->createClass($json);
165
    }
166
167
    /**
168
     * @test
169
     */
170
    public function willThrowAnExceptionIfTheFrequencyIsMissing()
171
    {
172
        $json = $this->getJson();
173
        unset($json->frequency);
174
        $this->expectException(LogicException::class);
175
        $this->createClass($json);
176
    }
177
178
    /**
179
     * @test
180
     */
181
    public function willThrowAnExceptionIfTheLegacyIdIsMissing()
182
    {
183
        $json = $this->getJson();
184
        unset($json->legacy_id);
185
        $this->expectException(LogicException::class);
186
        $this->createClass($json);
187
    }
188
189
    /**
190
     * @test
191
     */
192
    public function willThrowAnExceptionIfTheIsSubscribedIsMissing()
193
    {
194
        $json = $this->getJson();
195
        unset($json->is_subscribed);
196
        $this->expectException(LogicException::class);
197
        $this->createClass($json);
198
    }
199
200
    /**
201
     * @test
202
     */
203
    public function willThrowAnExceptionIfTheCanSubscribeIsMissing()
204
    {
205
        $json = $this->getJson();
206
        unset($json->can_subscribe);
207
        $this->expectException(LogicException::class);
208
        $this->createClass($json);
209
    }
210
211
    /**
212
     * @param \stdClass|null $json
213
     *
214
     * @return Plan
215
     */
216
    private function createClass(\stdClass $json = null): Plan
217
    {
218
        if ($json === null) {
219
            $json = $this->getJson();
220
        }
221
222
        return $this->factory->create($json);
223
    }
224
225
    /**
226
     * @return \stdClass
227
     */
228
    private function getJson(): \stdClass
229
    {
230
        $json = <<<JSON
231
{
232
    "id": "e592fd9519420ba7405e1307bff33214",
233
    "name": "Pro Plan",
234
    "price": 20,
235
    "currency": "USD",
236
    "frequency": "monthly",
237
    "legacy_id": "pro",
238
    "is_subscribed": true,
239
    "can_subscribe": true
240
}
241
JSON;
242
243
        return \json_decode($json);
244
    }
245
}
246