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