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-2020, 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\bill\Bill; |
16
|
|
|
use hiqdev\php\billing\charge\Charge; |
17
|
|
|
use hiqdev\php\billing\customer\Customer; |
18
|
|
|
use hiqdev\php\billing\plan\Plan; |
19
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
20
|
|
|
use hiqdev\php\billing\sale\Sale; |
21
|
|
|
use hiqdev\php\billing\target\Target; |
22
|
|
|
use hiqdev\php\billing\tests\support\tools\SimpleFactory; |
23
|
|
|
use hiqdev\php\billing\type\Type; |
24
|
|
|
use hiqdev\php\units\Unit; |
25
|
|
|
|
26
|
|
|
class FactoryTest extends \PHPUnit\Framework\TestCase |
27
|
|
|
{ |
28
|
|
|
private $type = 'type'; |
29
|
|
|
private $typeId = 'type-id'; |
30
|
|
|
|
31
|
|
|
private $name = 'name'; |
32
|
|
|
|
33
|
|
|
private $time = '2020-02-01T00:00:00+00:00'; |
34
|
|
|
|
35
|
|
|
private $quantity = '10'; |
36
|
|
|
private $unit = 'items'; |
37
|
|
|
|
38
|
|
|
private $sum = '11.99'; |
39
|
|
|
private $currency = 'USD'; |
40
|
|
|
|
41
|
|
|
private $user = 'user'; |
42
|
|
|
private $reseller = 'reseller'; |
43
|
|
|
|
44
|
|
|
private $planId = 'plan-id'; |
45
|
|
|
private $plan = 'plan'; |
46
|
|
|
|
47
|
|
|
private $saleId = 'sale-id'; |
48
|
|
|
|
49
|
|
|
private $chargeId = 'charge-id'; |
50
|
|
|
|
51
|
|
|
private $billId = 'bill-id'; |
52
|
|
|
|
53
|
|
|
private $actionId = 'action-id'; |
54
|
|
|
|
55
|
|
|
private $priceId = 'price-id'; |
56
|
|
|
|
57
|
|
|
private $targetId = 'target-id'; |
58
|
|
|
|
59
|
|
|
private $target = 'type:name'; |
60
|
|
|
|
61
|
|
|
private SimpleFactory $factory; |
62
|
|
|
|
63
|
|
|
protected function setUp(): void |
64
|
|
|
{ |
65
|
|
|
$this->factory = new SimpleFactory(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetCustomer() |
69
|
|
|
{ |
70
|
|
|
$c1 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]); |
71
|
|
|
$c2 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]); |
72
|
|
|
$c3 = $this->factory->get('customer', ['login' => $this->user]); |
73
|
|
|
$c4 = $this->factory->get('customer', $this->user); |
74
|
|
|
$c5 = $this->factory->find('customer', [$this->user]); |
75
|
|
|
$this->assertInstanceOf(Customer::class, $c1); |
76
|
|
|
$this->assertSame($this->user, $c1->getLogin()); |
77
|
|
|
$this->assertSame($this->reseller, $c1->getSeller()->getLogin()); |
78
|
|
|
$this->assertSame($c1, $c2); |
79
|
|
|
$this->assertSame($c1, $c3); |
80
|
|
|
$this->assertSame($c1, $c4); |
81
|
|
|
$this->assertSame($c1, $c5); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetType() |
85
|
|
|
{ |
86
|
|
|
$t1 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]); |
87
|
|
|
$t2 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]); |
88
|
|
|
$t3 = $this->factory->get('type', ['id' => $this->typeId]); |
89
|
|
|
$t4 = $this->factory->get('type', $this->type); |
90
|
|
|
$t5 = $this->factory->find('type', [$this->type]); |
91
|
|
|
$this->assertInstanceOf(Type::class, $t1); |
92
|
|
|
$this->assertSame($this->type, $t1->getName()); |
93
|
|
|
$this->assertSame($this->typeId, $t1->getId()); |
94
|
|
|
$this->assertSame($t1, $t2); |
95
|
|
|
$this->assertSame($t1, $t3); |
96
|
|
|
$this->assertSame($t1, $t4); |
97
|
|
|
$this->assertSame($t1, $t5); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetTarget() |
101
|
|
|
{ |
102
|
|
|
$t1 = $this->factory->get('target', ['id' => $this->targetId, 'name' => $this->name, 'type' => $this->type]); |
103
|
|
|
$t2 = $this->factory->get('target', ['name' => $this->name, 'type' => $this->type]); |
104
|
|
|
$t3 = $this->factory->get('target', ['id' => $this->targetId]); |
105
|
|
|
$t4 = $this->factory->get('target', $this->targetId); |
106
|
|
|
$t5 = $this->factory->get('target', $this->type . ':' . $this->name); |
107
|
|
|
$this->assertInstanceOf(Target::class, $t1); |
108
|
|
|
$this->assertSame($this->name, $t1->getName()); |
109
|
|
|
$this->assertSame($this->type, $t1->getType()); |
110
|
|
|
$this->assertSame($this->targetId, $t1->getId()); |
111
|
|
|
$this->assertSame($t1, $t2); |
112
|
|
|
$this->assertSame($t1, $t3); |
113
|
|
|
$this->assertSame($t1, $t4); |
114
|
|
|
$this->assertSame($t1, $t5); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testParseTarget() |
118
|
|
|
{ |
119
|
|
|
$id = $this->type . ':' . $this->name; |
120
|
|
|
$t1 = $this->factory->get('target', $id); |
121
|
|
|
$this->assertInstanceOf(Target::class, $t1); |
122
|
|
|
$this->assertSame($this->name, $t1->getName()); |
123
|
|
|
$this->assertSame($this->type, $t1->getType()); |
124
|
|
|
$this->assertSame($id, $t1->getId()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testGetPlan() |
128
|
|
|
{ |
129
|
|
|
$str = $this->plan . ' ' . $this->reseller; |
130
|
|
|
$p1 = $this->factory->get('plan', ['id' => $this->planId, 'name' => $this->plan, 'seller' => $this->reseller]); |
131
|
|
|
$p2 = $this->factory->get('plan', ['name' => $this->plan, 'seller' => $this->reseller]); |
132
|
|
|
$p3 = $this->factory->get('plan', ['id' => $this->planId]); |
133
|
|
|
$p4 = $this->factory->get('plan', $str); |
134
|
|
|
$p5 = $this->factory->find('plan', [$str]); |
135
|
|
|
$this->assertInstanceOf(Plan::class, $p1); |
136
|
|
|
$this->assertSame($this->plan, $p1->getName()); |
137
|
|
|
$this->assertSame($this->reseller, $p1->getSeller()->getLogin()); |
138
|
|
|
$this->assertSame($p1, $p2); |
139
|
|
|
$this->assertSame($p1, $p3); |
140
|
|
|
$this->assertSame($p1, $p4); |
141
|
|
|
$this->assertSame($p1, $p5); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testGetSale() |
145
|
|
|
{ |
146
|
|
|
$this->testGetCustomer(); |
147
|
|
|
$this->testGetPlan(); |
148
|
|
|
$s1 = $this->factory->get('sale', [ |
149
|
|
|
'id' => $this->saleId, |
150
|
|
|
'customer' => $this->user, |
151
|
|
|
'target' => $this->targetId, |
152
|
|
|
'plan' => $this->planId, |
153
|
|
|
'time' => $this->time, |
154
|
|
|
]); |
155
|
|
|
$s2 = $this->factory->get('sale', ['id' => $this->saleId, 'target' => $this->targetId]); |
156
|
|
|
$s3 = $this->factory->get('sale', ['id' => $this->saleId]); |
157
|
|
|
$s4 = $this->factory->get('sale', $this->saleId); |
158
|
|
|
$s5 = $this->factory->find('sale', [$this->saleId]); |
159
|
|
|
$this->assertInstanceOf(Sale::class, $s1); |
160
|
|
|
$this->assertSame($this->saleId, $s1->getId()); |
161
|
|
|
$this->assertSame($this->user, $s1->getCustomer()->getLogin()); |
162
|
|
|
$this->assertSame($this->time, $s1->getTime()->format('c')); |
163
|
|
|
$this->assertSame($this->targetId, $s1->getTarget()->getId()); |
164
|
|
|
$this->assertSame($this->planId, $s1->getPlan()->getId()); |
165
|
|
|
$this->assertSame($s1, $s2); |
166
|
|
|
$this->assertSame($s1, $s3); |
167
|
|
|
$this->assertSame($s1, $s4); |
168
|
|
|
$this->assertSame($s1, $s5); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testGetCharge() |
172
|
|
|
{ |
173
|
|
|
$this->testGetBill(); |
174
|
|
|
$this->testGetPrice(); |
175
|
|
|
$this->testGetAction(); |
176
|
|
|
$charge = $this->factory->get('charge', [ |
177
|
|
|
'id' => $this->chargeId, |
178
|
|
|
'type' => $this->type, |
179
|
|
|
'target' => $this->target, |
180
|
|
|
'action' => $this->actionId, |
181
|
|
|
'price' => $this->priceId, |
182
|
|
|
'usage' => $this->quantity . ' ' . $this->unit, |
183
|
|
|
'sum' => $this->sum . ' ' . $this->currency, |
184
|
|
|
'bill' => $this->billId, |
185
|
|
|
]); |
186
|
|
|
$e2 = $this->factory->get('charge', ['id' => $this->chargeId, 'target' => $this->targetId]); |
187
|
|
|
$e3 = $this->factory->get('charge', ['id' => $this->chargeId]); |
188
|
|
|
$e4 = $this->factory->get('charge', $this->chargeId); |
189
|
|
|
$e5 = $this->factory->find('charge', [$this->chargeId]); |
190
|
|
|
$this->assertInstanceOf(Charge::class, $charge); |
191
|
|
|
$this->assertSame($this->chargeId, $charge->getId()); |
192
|
|
|
$this->assertSame($this->type, $charge->getType()->getName()); |
193
|
|
|
$this->assertSame($this->target, $charge->getTarget()->getFullName()); |
194
|
|
|
$this->assertSame($this->actionId, $charge->getAction()->getId()); |
195
|
|
|
$this->assertSame($this->priceId, $charge->getPrice()->getId()); |
196
|
|
|
$this->assertSame($this->quantity, $charge->getUsage()->getQuantity()); |
197
|
|
|
$this->assertSame($this->unit, $charge->getUsage()->getUnit()->getName()); |
198
|
|
|
$this->assertEquals($this->sum*100, $charge->getSum()->getAmount()); |
199
|
|
|
$this->assertSame($this->currency, $charge->getSum()->getCurrency()->getCode()); |
200
|
|
|
$this->assertSame($this->billId, $charge->getBill()->getId()); |
201
|
|
|
$this->assertSame($charge, $e2); |
202
|
|
|
$this->assertSame($charge, $e3); |
203
|
|
|
$this->assertSame($charge, $e4); |
204
|
|
|
$this->assertSame($charge, $e5); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testGetBill() |
208
|
|
|
{ |
209
|
|
|
$this->testGetSale(); |
210
|
|
|
$s1 = $this->factory->get('bill', [ |
211
|
|
|
'id' => $this->billId, |
212
|
|
|
'type' => $this->type, |
213
|
|
|
'customer' => $this->user, |
214
|
|
|
'target' => $this->targetId, |
215
|
|
|
'sum' => $this->sum . ' ' . $this->currency, |
216
|
|
|
'quantity' => $this->quantity . ' ' . $this->unit, |
217
|
|
|
'plan' => $this->planId, |
218
|
|
|
'time' => $this->time, |
219
|
|
|
]); |
220
|
|
|
$s2 = $this->factory->get('bill', ['id' => $this->billId, 'target' => $this->targetId]); |
221
|
|
|
$s3 = $this->factory->get('bill', ['id' => $this->billId]); |
222
|
|
|
$s4 = $this->factory->get('bill', $this->billId); |
223
|
|
|
$s5 = $this->factory->find('bill', [$this->billId]); |
224
|
|
|
$this->assertInstanceOf(Bill::class, $s1); |
225
|
|
|
$this->assertSame($this->billId, $s1->getId()); |
226
|
|
|
$this->assertSame($this->user, $s1->getCustomer()->getLogin()); |
227
|
|
|
$this->assertSame($this->time, $s1->getTime()->format('c')); |
228
|
|
|
$this->assertSame($this->targetId, $s1->getTarget()->getId()); |
229
|
|
|
$this->assertSame($this->planId, $s1->getPlan()->getId()); |
230
|
|
|
$this->assertSame($this->quantity, $s1->getQuantity()->getQuantity()); |
231
|
|
|
$this->assertSame($this->unit, $s1->getQuantity()->getUnit()->getName()); |
232
|
|
|
$this->assertEquals($this->sum*100, $s1->getSum()->getAmount()); |
233
|
|
|
$this->assertSame($this->currency, $s1->getSum()->getCurrency()->getCode()); |
234
|
|
|
$this->assertSame($s1, $s2); |
235
|
|
|
$this->assertSame($s1, $s3); |
236
|
|
|
$this->assertSame($s1, $s4); |
237
|
|
|
$this->assertSame($s1, $s5); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testGetAction() |
241
|
|
|
{ |
242
|
|
|
$this->testGetSale(); |
243
|
|
|
$s1 = $this->factory->get('action', [ |
244
|
|
|
'id' => $this->actionId, |
245
|
|
|
'type' => $this->type, |
246
|
|
|
'target' => $this->targetId, |
247
|
|
|
'customer' => $this->user, |
248
|
|
|
'quantity' => $this->quantity . ' ' . $this->unit, |
249
|
|
|
'sale' => $this->saleId, |
250
|
|
|
'time' => $this->time, |
251
|
|
|
]); |
252
|
|
|
$s2 = $this->factory->get('action', ['id' => $this->actionId, 'target' => $this->targetId]); |
253
|
|
|
$s3 = $this->factory->get('action', ['id' => $this->actionId]); |
254
|
|
|
$s4 = $this->factory->get('action', $this->actionId); |
255
|
|
|
$s5 = $this->factory->find('action', [$this->actionId]); |
256
|
|
|
$this->assertInstanceOf(Action::class, $s1); |
257
|
|
|
$this->assertSame($this->actionId, $s1->getId()); |
258
|
|
|
$this->assertSame($this->user, $s1->getCustomer()->getLogin()); |
259
|
|
|
$this->assertSame($this->time, $s1->getTime()->format('c')); |
260
|
|
|
$this->assertSame($this->type, $s1->getType()->getName()); |
261
|
|
|
$this->assertSame($this->targetId, $s1->getTarget()->getId()); |
262
|
|
|
$this->assertSame($this->saleId, $s1->getSale()->getId()); |
263
|
|
|
$this->assertSame($s1, $s2); |
264
|
|
|
$this->assertSame($s1, $s3); |
265
|
|
|
$this->assertSame($s1, $s4); |
266
|
|
|
$this->assertSame($s1, $s5); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testGetPrice() |
270
|
|
|
{ |
271
|
|
|
$p1 = $this->factory->get('price', [ |
272
|
|
|
'id' => $this->priceId, |
273
|
|
|
'type' => $this->type, |
274
|
|
|
'target' => $this->targetId, |
275
|
|
|
'price' => $this->sum . ' ' . $this->currency, |
276
|
|
|
'prepaid' => '0 ' . $this->unit, |
277
|
|
|
'currency' => $this->currency, |
278
|
|
|
]); |
279
|
|
|
$p2 = $this->factory->get('price', ['id' => $this->priceId, 'target' => $this->targetId]); |
280
|
|
|
$p3 = $this->factory->get('price', ['id' => $this->priceId]); |
281
|
|
|
$p4 = $this->factory->get('price', $this->priceId); |
282
|
|
|
$p5 = $this->factory->find('price', [$this->priceId]); |
283
|
|
|
$this->assertInstanceOf(PriceInterface::class, $p1); |
284
|
|
|
$this->assertSame($this->priceId, $p1->getId()); |
285
|
|
|
$this->assertSame($this->type, $p1->getType()->getName()); |
286
|
|
|
$this->assertSame($this->targetId, $p1->getTarget()->getId()); |
287
|
|
|
$this->assertSame($this->unit, $p1->getPrepaid()->getUnit()->getName()); |
288
|
|
|
$this->assertEquals($this->sum*100, $p1->getPrice()->getAmount()); |
289
|
|
|
$this->assertSame($p1, $p2); |
290
|
|
|
$this->assertSame($p1, $p3); |
291
|
|
|
$this->assertSame($p1, $p4); |
292
|
|
|
$this->assertSame($p1, $p5); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testGetMoney() |
296
|
|
|
{ |
297
|
|
|
$str = $this->sum . ' ' . $this->currency; |
298
|
|
|
$m1 = $this->factory->get('money', ['amount' => $this->sum*100, 'currency' => $this->currency]); |
299
|
|
|
$m2 = $this->factory->get('money', $str); |
300
|
|
|
//$m3 = $this->factory->find('money', [$str]); |
301
|
|
|
$this->assertEquals($this->sum*100, $m1->getAmount()); |
302
|
|
|
$this->assertSame($this->currency, $m1->getCurrency()->getCode()); |
303
|
|
|
$this->assertSame($m1, $m2); |
304
|
|
|
//$this->assertSame($m1, $m3); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
public function testGetQuantity() |
308
|
|
|
{ |
309
|
|
|
$str = $this->quantity . ' ' . $this->unit; |
310
|
|
|
$m1 = $this->factory->get('quantity', ['quantity' => $this->quantity, 'unit' => $this->unit]); |
311
|
|
|
$m2 = $this->factory->get('quantity', $str); |
312
|
|
|
$m3 = $this->factory->find('quantity', [$str]); |
313
|
|
|
$this->assertSame($this->quantity, $m1->getQuantity()); |
314
|
|
|
$this->assertSame($this->unit, $m1->getUnit()->getName()); |
315
|
|
|
$this->assertSame($m1, $m2); |
316
|
|
|
$this->assertSame($m1, $m3); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
public function testGetUnit() |
320
|
|
|
{ |
321
|
|
|
$u1 = $this->factory->get('unit', ['name' => $this->unit]); |
322
|
|
|
$u2 = $this->factory->get('unit', ['name' => $this->unit]); |
323
|
|
|
$u3 = $this->factory->get('unit', ['name' => $this->unit]); |
324
|
|
|
$u4 = $this->factory->get('unit', $this->unit); |
325
|
|
|
$u5 = $this->factory->find('unit', [$this->unit]); |
326
|
|
|
$this->assertInstanceOf(Unit::class, $u1); |
327
|
|
|
$this->assertSame($this->unit, $u1->getName()); |
328
|
|
|
$this->assertSame($u1, $u2); |
329
|
|
|
$this->assertSame($u1, $u3); |
330
|
|
|
$this->assertSame($u1, $u4); |
331
|
|
|
$this->assertSame($u1, $u5); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function testGetTime() |
335
|
|
|
{ |
336
|
|
|
$u1 = $this->factory->get('time', ['date' => $this->time]); |
337
|
|
|
$u2 = $this->factory->get('time', ['date' => $this->time]); |
338
|
|
|
$u3 = $this->factory->get('time', ['date' => $this->time]); |
339
|
|
|
$u4 = $this->factory->get('time', $this->time); |
340
|
|
|
$u5 = $this->factory->find('time', [$this->time]); |
341
|
|
|
$this->assertInstanceOf(DateTimeImmutable::class, $u1); |
342
|
|
|
$this->assertSame($this->time, $u1->format('c')); |
343
|
|
|
$this->assertSame($u1, $u2); |
344
|
|
|
$this->assertSame($u1, $u3); |
345
|
|
|
$this->assertSame($u1, $u4); |
346
|
|
|
$this->assertSame($u1, $u5); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|