Completed
Push — master ( cf53d8...fd2f7c )
by Andrii
02:14
created

FactoryTest::testGetBill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 31
rs 9.472
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 $time = '2020-02-01T00:00:00+00:00';
28
29
    private $quantity = '10';
30
    private $unit = 'items';
31
32
    private $sum = '11.99';
33
    private $currency = 'USD';
34
35
    private $user = 'user';
36
    private $reseller = 'reseller';
37
38
    private $planId = 'plan-id';
39
    private $plan = 'plan';
40
41
    private $type = 'type';
42
    private $typeId = 'type-id';
43
44
    private $saleId = 'sale-id';
45
46
    private $billId = 'bill-id';
47
48
    private $actionId = 'action-id';
49
50
    private $priceId = 'price-id';
51
52
    private $target = 'target';
53
    private $targetId = 'target-id';
54
55
    protected function setUp()
56
    {
57
        $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...
58
    }
59
60
    public function testGetCustomer()
61
    {
62
        $c1 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]);
63
        $c2 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]);
64
        $c3 = $this->factory->get('customer', ['login' => $this->user]);
65
        $c4 = $this->factory->get('customer', $this->user);
66
        $c5 = $this->factory->find('customer', [$this->user]);
67
        $this->assertInstanceOf(Customer::class, $c1);
68
        $this->assertSame($this->user, $c1->getLogin());
69
        $this->assertSame($this->reseller, $c1->getSeller()->getLogin());
70
        $this->assertSame($c1, $c2);
71
        $this->assertSame($c1, $c3);
72
        $this->assertSame($c1, $c4);
73
        $this->assertSame($c1, $c5);
74
    }
75
76
    public function testGetType()
77
    {
78
        $t1 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]);
79
        $t2 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]);
80
        $t3 = $this->factory->get('type', ['id' => $this->typeId]);
81
        $t4 = $this->factory->get('type', $this->type);
82
        $t5 = $this->factory->find('type', [$this->type]);
83
        $this->assertInstanceOf(Type::class, $t1);
84
        $this->assertSame($this->type, $t1->getName());
85
        $this->assertSame($this->typeId, $t1->getId());
86
        $this->assertSame($t1, $t2);
87
        $this->assertSame($t1, $t3);
88
        $this->assertSame($t1, $t4);
89
        $this->assertSame($t1, $t5);
90
    }
91
92
    public function testGetTarget()
93
    {
94
        $t1 = $this->factory->get('target', ['id' => $this->targetId, 'name' => $this->target, 'type' => 'simple']);
95
        $t2 = $this->factory->get('target', ['id' => $this->targetId, 'name' => $this->target, 'type' => 'simple']);
96
        $t3 = $this->factory->get('target', ['id' => $this->targetId]);
97
        $t4 = $this->factory->get('target', $this->targetId);
98
        $t5 = $this->factory->find('target', [$this->targetId]);
99
        $this->assertInstanceOf(Target::class, $t1);
100
        $this->assertSame($this->target, $t1->getName());
101
        $this->assertSame($this->targetId, $t1->getId());
102
        $this->assertSame($t1, $t2);
103
        $this->assertSame($t1, $t3);
104
        $this->assertSame($t1, $t4);
105
        $this->assertSame($t1, $t5);
106
    }
107
108
    public function testGetPlan()
109
    {
110
        $str = $this->plan . ' ' . $this->reseller;
111
        $p1 = $this->factory->get('plan', ['id' => $this->planId, 'name' => $this->plan, 'seller' => $this->reseller]);
112
        $p2 = $this->factory->get('plan', ['name' => $this->plan, 'seller' => $this->reseller]);
113
        $p3 = $this->factory->get('plan', ['id' => $this->planId]);
114
        $p4 = $this->factory->get('plan', $str);
115
        $p5 = $this->factory->find('plan', [$str]);
116
        $this->assertInstanceOf(Plan::class, $p1);
117
        $this->assertSame($this->plan, $p1->getName());
118
        $this->assertSame($this->reseller, $p1->getSeller()->getLogin());
119
        $this->assertSame($p1, $p2);
120
        $this->assertSame($p1, $p3);
121
        $this->assertSame($p1, $p4);
122
        $this->assertSame($p1, $p5);
123
    }
124
125
    public function testGetSale()
126
    {
127
        $this->testGetCustomer();
128
        $this->testGetPlan();
129
        $s1 = $this->factory->get('sale', [
130
            'id' => $this->saleId,
131
            'customer' => $this->user,
132
            'target' => $this->targetId,
133
            'plan' => $this->planId,
134
            'time' => $this->time,
135
        ]);
136
        $s2 = $this->factory->get('sale', ['id' => $this->saleId, 'target' => $this->targetId]);
137
        $s3 = $this->factory->get('sale', ['id' => $this->saleId]);
138
        $s4 = $this->factory->get('sale', $this->saleId);
139
        $s5 = $this->factory->find('sale', [$this->saleId]);
140
        $this->assertInstanceOf(Sale::class, $s1);
141
        $this->assertSame($this->saleId, $s1->getId());
142
        $this->assertSame($this->user, $s1->getCustomer()->getLogin());
143
        $this->assertSame($this->time, $s1->getTime()->format('c'));
144
        $this->assertSame($this->targetId, $s1->getTarget()->getId());
145
        $this->assertSame($this->planId, $s1->getPlan()->getId());
146
        $this->assertSame($s1, $s2);
147
        $this->assertSame($s1, $s3);
148
        $this->assertSame($s1, $s4);
149
        $this->assertSame($s1, $s5);
150
    }
151
152
    public function testGetBill()
153
    {
154
        $this->testGetSale();
155
        $s1 = $this->factory->get('bill', [
156
            'id' => $this->billId,
157
            'type' => $this->type,
158
            'customer' => $this->user,
159
            'target' => $this->targetId,
160
            'sum' => $this->sum . ' ' . $this->currency,
161
            'quantity' => $this->quantity . ' ' . $this->unit,
162
            'plan' => $this->planId,
163
            'time' => $this->time,
164
        ]);
165
        $s2 = $this->factory->get('bill', ['id' => $this->billId, 'target' => $this->targetId]);
166
        $s3 = $this->factory->get('bill', ['id' => $this->billId]);
167
        $s4 = $this->factory->get('bill', $this->billId);
168
        $s5 = $this->factory->find('bill', [$this->billId]);
169
        $this->assertInstanceOf(Bill::class, $s1);
170
        $this->assertSame($this->billId, $s1->getId());
171
        $this->assertSame($this->user, $s1->getCustomer()->getLogin());
172
        $this->assertSame($this->time, $s1->getTime()->format('c'));
173
        $this->assertSame($this->targetId, $s1->getTarget()->getId());
174
        $this->assertSame($this->planId, $s1->getPlan()->getId());
175
        $this->assertSame($this->quantity, $s1->getQuantity()->getQuantity());
176
        $this->assertSame($this->unit, $s1->getQuantity()->getUnit()->getName());
177
        $this->assertEquals($this->sum*100, $s1->getSum()->getAmount());
178
        $this->assertSame($this->currency, $s1->getSum()->getCurrency()->getCode());
179
        $this->assertSame($s1, $s2);
180
        $this->assertSame($s1, $s3);
181
        $this->assertSame($s1, $s4);
182
        $this->assertSame($s1, $s5);
183
    }
184
185
    public function testGetAction()
186
    {
187
        $this->testGetSale();
188
        $s1 = $this->factory->get('action', [
189
            'id' => $this->actionId,
190
            'type' => $this->type,
191
            'target' => $this->targetId,
192
            'customer' => $this->user,
193
            'quantity' => $this->quantity . ' ' . $this->unit,
194
            'sale' => $this->saleId,
195
            'time' => $this->time,
196
        ]);
197
        $s2 = $this->factory->get('action', ['id' => $this->actionId, 'target' => $this->targetId]);
198
        $s3 = $this->factory->get('action', ['id' => $this->actionId]);
199
        $s4 = $this->factory->get('action', $this->actionId);
200
        $s5 = $this->factory->find('action', [$this->actionId]);
201
        $this->assertInstanceOf(Action::class, $s1);
202
        $this->assertSame($this->actionId, $s1->getId());
203
        $this->assertSame($this->user, $s1->getCustomer()->getLogin());
204
        $this->assertSame($this->time, $s1->getTime()->format('c'));
205
        $this->assertSame($this->type, $s1->getType()->getName());
206
        $this->assertSame($this->targetId, $s1->getTarget()->getId());
207
        $this->assertSame($this->saleId, $s1->getSale()->getId());
208
        $this->assertSame($s1, $s2);
209
        $this->assertSame($s1, $s3);
210
        $this->assertSame($s1, $s4);
211
        $this->assertSame($s1, $s5);
212
    }
213
214
    public function testGetPrice()
215
    {
216
        $p1 = $this->factory->get('price', [
217
            'id' => $this->priceId,
218
            'type' => $this->type,
219
            'target' => $this->targetId,
220
            'price' => $this->sum . ' ' . $this->currency,
221
            'prepaid' => '0 ' . $this->unit,
222
            'currency' => $this->currency,
223
        ]);
224
        $p2 = $this->factory->get('price', ['id' => $this->priceId, 'target' => $this->targetId]);
225
        $p3 = $this->factory->get('price', ['id' => $this->priceId]);
226
        $p4 = $this->factory->get('price', $this->priceId);
227
        $p5 = $this->factory->find('price', [$this->priceId]);
228
        $this->assertInstanceOf(PriceInterface::class, $p1);
229
        $this->assertSame($this->priceId, $p1->getId());
230
        $this->assertSame($this->type, $p1->getType()->getName());
231
        $this->assertSame($this->targetId, $p1->getTarget()->getId());
232
        $this->assertSame($this->unit, $p1->getPrepaid()->getUnit()->getName());
233
        $this->assertEquals($this->sum*100, $p1->getPrice()->getAmount());
234
        $this->assertSame($p1, $p2);
235
        $this->assertSame($p1, $p3);
236
        $this->assertSame($p1, $p4);
237
        $this->assertSame($p1, $p5);
238
    }
239
240
    public function testGetMoney()
241
    {
242
        $str = $this->sum . ' ' . $this->currency;
243
        $m1 = $this->factory->get('money', ['amount' => $this->sum, 'currency' => $this->currency]);
244
        $m2 = $this->factory->get('money', $str);
245
        $m3 = $this->factory->find('money', [$str]);
246
        $this->assertEquals($this->sum*100, $m1->getAmount());
247
        $this->assertSame($this->currency, $m1->getCurrency()->getCode());
248
        $this->assertSame($m1, $m2);
249
        $this->assertSame($m1, $m3);
250
    }
251
252
    public function testGetQuantity()
253
    {
254
        $str = $this->quantity . ' ' . $this->unit;
255
        $m1 = $this->factory->get('quantity', ['quantity' => $this->quantity, 'unit' => $this->unit]);
256
        $m2 = $this->factory->get('quantity', $str);
257
        $m3 = $this->factory->find('quantity', [$str]);
258
        $this->assertSame($this->quantity, $m1->getQuantity());
259
        $this->assertSame($this->unit, $m1->getUnit()->getName());
260
        $this->assertSame($m1, $m2);
261
        $this->assertSame($m1, $m3);
262
    }
263
264
    public function testGetUnit()
265
    {
266
        $u1 = $this->factory->get('unit', ['name' => $this->unit]);
267
        $u2 = $this->factory->get('unit', ['name' => $this->unit]);
268
        $u3 = $this->factory->get('unit', ['name' => $this->unit]);
269
        $u4 = $this->factory->get('unit', $this->unit);
270
        $u5 = $this->factory->find('unit', [$this->unit]);
271
        $this->assertInstanceOf(Unit::class, $u1);
272
        $this->assertSame($this->unit, $u1->getName());
273
        $this->assertSame($u1, $u2);
274
        $this->assertSame($u1, $u3);
275
        $this->assertSame($u1, $u4);
276
        $this->assertSame($u1, $u5);
277
    }
278
279
    public function testGetTime()
280
    {
281
        $u1 = $this->factory->get('time', ['time' => $this->time]);
282
        $u2 = $this->factory->get('time', ['time' => $this->time]);
283
        $u3 = $this->factory->get('time', ['time' => $this->time]);
284
        $u4 = $this->factory->get('time', $this->time);
285
        $u5 = $this->factory->find('time', [$this->time]);
286
        $this->assertInstanceOf(DateTimeImmutable::class, $u1);
287
        $this->assertSame($this->time, $u1->format('c'));
288
        $this->assertSame($u1, $u2);
289
        $this->assertSame($u1, $u3);
290
        $this->assertSame($u1, $u4);
291
        $this->assertSame($u1, $u5);
292
    }
293
294
}
295