Completed
Push — master ( a88c1a...2a7402 )
by Andrii
02:44
created

FactoryTest::testGetTarget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.8333
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit\tools;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\action\Action;
15
use hiqdev\php\billing\customer\Customer;
16
use hiqdev\php\billing\bill\Bill;
17
use hiqdev\php\billing\plan\Plan;
18
use hiqdev\php\billing\price\PriceInterface;
19
use hiqdev\php\billing\sale\Sale;
20
use hiqdev\php\billing\target\Target;
21
use hiqdev\php\billing\tests\support\tools\SimpleFactory;
22
use hiqdev\php\billing\type\Type;
23
use hiqdev\php\units\Unit;
24
25
class FactoryTest extends \PHPUnit\Framework\TestCase
26
{
27
    private $type = 'type';
28
    private $typeId = 'type-id';
29
30
    private $name = 'name';
31
32
    private $time = '2020-02-01T00:00:00+00:00';
33
34
    private $quantity = '10';
35
    private $unit = 'items';
36
37
    private $sum = '11.99';
38
    private $currency = 'USD';
39
40
    private $user = 'user';
41
    private $reseller = 'reseller';
42
43
    private $planId = 'plan-id';
44
    private $plan = 'plan';
45
46
    private $saleId = 'sale-id';
47
48
    private $billId = 'bill-id';
49
50
    private $actionId = 'action-id';
51
52
    private $priceId = 'price-id';
53
54
    private $targetId = 'target-id';
55
56
    protected function setUp()
57
    {
58
        $this->factory = new SimpleFactory();
0 ignored issues
show
Bug Best Practice introduced by
The property factory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
    }
60
61
    public function testGetCustomer()
62
    {
63
        $c1 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]);
64
        $c2 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]);
65
        $c3 = $this->factory->get('customer', ['login' => $this->user]);
66
        $c4 = $this->factory->get('customer', $this->user);
67
        $c5 = $this->factory->find('customer', [$this->user]);
68
        $this->assertInstanceOf(Customer::class, $c1);
69
        $this->assertSame($this->user, $c1->getLogin());
70
        $this->assertSame($this->reseller, $c1->getSeller()->getLogin());
71
        $this->assertSame($c1, $c2);
72
        $this->assertSame($c1, $c3);
73
        $this->assertSame($c1, $c4);
74
        $this->assertSame($c1, $c5);
75
    }
76
77
    public function testGetType()
78
    {
79
        $t1 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]);
80
        $t2 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]);
81
        $t3 = $this->factory->get('type', ['id' => $this->typeId]);
82
        $t4 = $this->factory->get('type', $this->type);
83
        $t5 = $this->factory->find('type', [$this->type]);
84
        $this->assertInstanceOf(Type::class, $t1);
85
        $this->assertSame($this->type, $t1->getName());
86
        $this->assertSame($this->typeId, $t1->getId());
87
        $this->assertSame($t1, $t2);
88
        $this->assertSame($t1, $t3);
89
        $this->assertSame($t1, $t4);
90
        $this->assertSame($t1, $t5);
91
    }
92
93
    public function testGetTarget()
94
    {
95
        $t1 = $this->factory->get('target', ['id' => $this->targetId, 'name' => $this->name, 'type' => $this->type]);
96
        $t2 = $this->factory->get('target', ['name' => $this->name, 'type' => $this->type]);
97
        $t3 = $this->factory->get('target', ['id' => $this->targetId]);
98
        $t4 = $this->factory->get('target', $this->targetId);
99
        $t5 = $this->factory->get('target', $this->type . ':' . $this->name);
100
        $this->assertInstanceOf(Target::class, $t1);
101
        $this->assertSame($this->name, $t1->getName());
102
        $this->assertSame($this->type, $t1->getType());
103
        $this->assertSame($this->targetId, $t1->getId());
104
        $this->assertSame($t1, $t2);
105
        $this->assertSame($t1, $t3);
106
        $this->assertSame($t1, $t4);
107
        $this->assertSame($t1, $t5);
108
    }
109
110
    public function testParseTarget()
111
    {
112
        $id = $this->type . ':' . $this->name;
113
        $t1 = $this->factory->get('target', $id);
114
        $this->assertInstanceOf(Target::class, $t1);
115
        $this->assertSame($this->name, $t1->getName());
116
        $this->assertSame($this->type, $t1->getType());
117
        $this->assertSame($id, $t1->getId());
118
    }
119
120
    public function testGetPlan()
121
    {
122
        $str = $this->plan . ' ' . $this->reseller;
123
        $p1 = $this->factory->get('plan', ['id' => $this->planId, 'name' => $this->plan, 'seller' => $this->reseller]);
124
        $p2 = $this->factory->get('plan', ['name' => $this->plan, 'seller' => $this->reseller]);
125
        $p3 = $this->factory->get('plan', ['id' => $this->planId]);
126
        $p4 = $this->factory->get('plan', $str);
127
        $p5 = $this->factory->find('plan', [$str]);
128
        $this->assertInstanceOf(Plan::class, $p1);
129
        $this->assertSame($this->plan, $p1->getName());
130
        $this->assertSame($this->reseller, $p1->getSeller()->getLogin());
131
        $this->assertSame($p1, $p2);
132
        $this->assertSame($p1, $p3);
133
        $this->assertSame($p1, $p4);
134
        $this->assertSame($p1, $p5);
135
    }
136
137
    public function testGetSale()
138
    {
139
        $this->testGetCustomer();
140
        $this->testGetPlan();
141
        $s1 = $this->factory->get('sale', [
142
            'id' => $this->saleId,
143
            'customer' => $this->user,
144
            'target' => $this->targetId,
145
            'plan' => $this->planId,
146
            'time' => $this->time,
147
        ]);
148
        $s2 = $this->factory->get('sale', ['id' => $this->saleId, 'target' => $this->targetId]);
149
        $s3 = $this->factory->get('sale', ['id' => $this->saleId]);
150
        $s4 = $this->factory->get('sale', $this->saleId);
151
        $s5 = $this->factory->find('sale', [$this->saleId]);
152
        $this->assertInstanceOf(Sale::class, $s1);
153
        $this->assertSame($this->saleId, $s1->getId());
154
        $this->assertSame($this->user, $s1->getCustomer()->getLogin());
155
        $this->assertSame($this->time, $s1->getTime()->format('c'));
156
        $this->assertSame($this->targetId, $s1->getTarget()->getId());
157
        $this->assertSame($this->planId, $s1->getPlan()->getId());
158
        $this->assertSame($s1, $s2);
159
        $this->assertSame($s1, $s3);
160
        $this->assertSame($s1, $s4);
161
        $this->assertSame($s1, $s5);
162
    }
163
164
    public function testGetBill()
165
    {
166
        $this->testGetSale();
167
        $s1 = $this->factory->get('bill', [
168
            'id' => $this->billId,
169
            'type' => $this->type,
170
            'customer' => $this->user,
171
            'target' => $this->targetId,
172
            'sum' => $this->sum . ' ' . $this->currency,
173
            'quantity' => $this->quantity . ' ' . $this->unit,
174
            'plan' => $this->planId,
175
            'time' => $this->time,
176
        ]);
177
        $s2 = $this->factory->get('bill', ['id' => $this->billId, 'target' => $this->targetId]);
178
        $s3 = $this->factory->get('bill', ['id' => $this->billId]);
179
        $s4 = $this->factory->get('bill', $this->billId);
180
        $s5 = $this->factory->find('bill', [$this->billId]);
181
        $this->assertInstanceOf(Bill::class, $s1);
182
        $this->assertSame($this->billId, $s1->getId());
183
        $this->assertSame($this->user, $s1->getCustomer()->getLogin());
184
        $this->assertSame($this->time, $s1->getTime()->format('c'));
185
        $this->assertSame($this->targetId, $s1->getTarget()->getId());
186
        $this->assertSame($this->planId, $s1->getPlan()->getId());
187
        $this->assertSame($this->quantity, $s1->getQuantity()->getQuantity());
188
        $this->assertSame($this->unit, $s1->getQuantity()->getUnit()->getName());
189
        $this->assertEquals($this->sum*100, $s1->getSum()->getAmount());
190
        $this->assertSame($this->currency, $s1->getSum()->getCurrency()->getCode());
191
        $this->assertSame($s1, $s2);
192
        $this->assertSame($s1, $s3);
193
        $this->assertSame($s1, $s4);
194
        $this->assertSame($s1, $s5);
195
    }
196
197
    public function testGetAction()
198
    {
199
        $this->testGetSale();
200
        $s1 = $this->factory->get('action', [
201
            'id' => $this->actionId,
202
            'type' => $this->type,
203
            'target' => $this->targetId,
204
            'customer' => $this->user,
205
            'quantity' => $this->quantity . ' ' . $this->unit,
206
            'sale' => $this->saleId,
207
            'time' => $this->time,
208
        ]);
209
        $s2 = $this->factory->get('action', ['id' => $this->actionId, 'target' => $this->targetId]);
210
        $s3 = $this->factory->get('action', ['id' => $this->actionId]);
211
        $s4 = $this->factory->get('action', $this->actionId);
212
        $s5 = $this->factory->find('action', [$this->actionId]);
213
        $this->assertInstanceOf(Action::class, $s1);
214
        $this->assertSame($this->actionId, $s1->getId());
215
        $this->assertSame($this->user, $s1->getCustomer()->getLogin());
216
        $this->assertSame($this->time, $s1->getTime()->format('c'));
217
        $this->assertSame($this->type, $s1->getType()->getName());
218
        $this->assertSame($this->targetId, $s1->getTarget()->getId());
219
        $this->assertSame($this->saleId, $s1->getSale()->getId());
220
        $this->assertSame($s1, $s2);
221
        $this->assertSame($s1, $s3);
222
        $this->assertSame($s1, $s4);
223
        $this->assertSame($s1, $s5);
224
    }
225
226
    public function testGetPrice()
227
    {
228
        $p1 = $this->factory->get('price', [
229
            'id' => $this->priceId,
230
            'type' => $this->type,
231
            'target' => $this->targetId,
232
            'price' => $this->sum . ' ' . $this->currency,
233
            'prepaid' => '0 ' . $this->unit,
234
            'currency' => $this->currency,
235
        ]);
236
        $p2 = $this->factory->get('price', ['id' => $this->priceId, 'target' => $this->targetId]);
237
        $p3 = $this->factory->get('price', ['id' => $this->priceId]);
238
        $p4 = $this->factory->get('price', $this->priceId);
239
        $p5 = $this->factory->find('price', [$this->priceId]);
240
        $this->assertInstanceOf(PriceInterface::class, $p1);
241
        $this->assertSame($this->priceId, $p1->getId());
242
        $this->assertSame($this->type, $p1->getType()->getName());
243
        $this->assertSame($this->targetId, $p1->getTarget()->getId());
244
        $this->assertSame($this->unit, $p1->getPrepaid()->getUnit()->getName());
245
        $this->assertEquals($this->sum*100, $p1->getPrice()->getAmount());
246
        $this->assertSame($p1, $p2);
247
        $this->assertSame($p1, $p3);
248
        $this->assertSame($p1, $p4);
249
        $this->assertSame($p1, $p5);
250
    }
251
252
    public function testGetMoney()
253
    {
254
        $str = $this->sum . ' ' . $this->currency;
255
        $m1 = $this->factory->get('money', ['amount' => $this->sum, 'currency' => $this->currency]);
256
        $m2 = $this->factory->get('money', $str);
257
        $m3 = $this->factory->find('money', [$str]);
258
        $this->assertEquals($this->sum*100, $m1->getAmount());
259
        $this->assertSame($this->currency, $m1->getCurrency()->getCode());
260
        $this->assertSame($m1, $m2);
261
        $this->assertSame($m1, $m3);
262
    }
263
264
    public function testGetQuantity()
265
    {
266
        $str = $this->quantity . ' ' . $this->unit;
267
        $m1 = $this->factory->get('quantity', ['quantity' => $this->quantity, 'unit' => $this->unit]);
268
        $m2 = $this->factory->get('quantity', $str);
269
        $m3 = $this->factory->find('quantity', [$str]);
270
        $this->assertSame($this->quantity, $m1->getQuantity());
271
        $this->assertSame($this->unit, $m1->getUnit()->getName());
272
        $this->assertSame($m1, $m2);
273
        $this->assertSame($m1, $m3);
274
    }
275
276
    public function testGetUnit()
277
    {
278
        $u1 = $this->factory->get('unit', ['name' => $this->unit]);
279
        $u2 = $this->factory->get('unit', ['name' => $this->unit]);
280
        $u3 = $this->factory->get('unit', ['name' => $this->unit]);
281
        $u4 = $this->factory->get('unit', $this->unit);
282
        $u5 = $this->factory->find('unit', [$this->unit]);
283
        $this->assertInstanceOf(Unit::class, $u1);
284
        $this->assertSame($this->unit, $u1->getName());
285
        $this->assertSame($u1, $u2);
286
        $this->assertSame($u1, $u3);
287
        $this->assertSame($u1, $u4);
288
        $this->assertSame($u1, $u5);
289
    }
290
291
    public function testGetTime()
292
    {
293
        $u1 = $this->factory->get('time', ['time' => $this->time]);
294
        $u2 = $this->factory->get('time', ['time' => $this->time]);
295
        $u3 = $this->factory->get('time', ['time' => $this->time]);
296
        $u4 = $this->factory->get('time', $this->time);
297
        $u5 = $this->factory->find('time', [$this->time]);
298
        $this->assertInstanceOf(DateTimeImmutable::class, $u1);
299
        $this->assertSame($this->time, $u1->format('c'));
300
        $this->assertSame($u1, $u2);
301
        $this->assertSame($u1, $u3);
302
        $this->assertSame($u1, $u4);
303
        $this->assertSame($u1, $u5);
304
    }
305
306
}
307