Completed
Push — master ( 678089...988510 )
by Andrii
02:43
created

FactoryTest::testGetTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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