Passed
Push — master ( 1fc46b...24437a )
by Andrii
02:26
created

FactoryTest::testGetUnit()   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
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 9.9
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 hiqdev\php\billing\tools\Factory;
14
use hiqdev\php\billing\customer\Customer;
15
use hiqdev\php\billing\customer\CustomerFactory;
16
use hiqdev\php\billing\type\Type;
17
use hiqdev\php\billing\type\TypeFactory;
18
use hiqdev\php\billing\target\Target;
19
use hiqdev\php\billing\target\TargetFactory;
20
use hiqdev\php\billing\plan\Plan;
21
use hiqdev\php\billing\plan\PlanFactory;
22
use hiqdev\php\billing\price\PriceInterface;
23
use hiqdev\php\billing\price\PriceFactory;
24
use hiqdev\php\billing\price\SinglePrice;
25
use hiqdev\php\units\Unit;
26
27
class FactoryTest extends \PHPUnit\Framework\TestCase
28
{
29
    private $unit = 'items';
30
    private $quantity = '10';
31
    private $currency = 'USD';
32
33
    private $user = 'user';
34
    private $reseller = 'reseller';
35
36
    private $planId = 'plan-id';
37
    private $plan = 'plan';
38
39
    private $type = 'type';
40
    private $typeId = 'type-id';
41
42
    private $priceId = 'price-id';
43
    private $priceSum = '11.99';
44
45
    private $target = 'target';
46
    private $targetId = 'target-id';
47
48
    protected function setUp()
49
    {
50
        $this->factory = new Factory([
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...
51
            'type'      => new TypeFactory(),
52
            'plan'      => new PlanFactory(),
53
            'price'     => new PriceFactory([], SinglePrice::class),
54
            'target'    => new TargetFactory(),
55
            'customer'  => new CustomerFactory(),
56
        ]);
57
    }
58
59
    public function testGetCustomer()
60
    {
61
        $c1 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]);
62
        $c2 = $this->factory->get('customer', ['login' => $this->user, 'seller' => $this->reseller]);
63
        $c3 = $this->factory->get('customer', ['login' => $this->user]);
64
        $c4 = $this->factory->get('customer', $this->user);
65
        $c5 = $this->factory->find('customer', [$this->user]);
66
        $this->assertInstanceOf(Customer::class, $c1);
67
        $this->assertSame($this->user, $c1->getLogin());
68
        $this->assertSame($this->reseller, $c1->getSeller()->getLogin());
69
        $this->assertSame($c1, $c2);
70
        $this->assertSame($c1, $c3);
71
        $this->assertSame($c1, $c4);
72
        $this->assertSame($c1, $c5);
73
    }
74
75
    public function testGetType()
76
    {
77
        $t1 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]);
78
        $t2 = $this->factory->get('type', ['id' => $this->typeId, 'name' => $this->type]);
79
        $t3 = $this->factory->get('type', ['id' => $this->typeId]);
80
        $t4 = $this->factory->get('type', $this->type);
81
        $t5 = $this->factory->find('type', [$this->type]);
82
        $this->assertInstanceOf(Type::class, $t1);
83
        $this->assertSame($this->type, $t1->getName());
84
        $this->assertSame($this->typeId, $t1->getId());
85
        $this->assertSame($t1, $t2);
86
        $this->assertSame($t1, $t3);
87
        $this->assertSame($t1, $t4);
88
        $this->assertSame($t1, $t5);
89
    }
90
91
    public function testGetTarget()
92
    {
93
        $t1 = $this->factory->get('target', ['id' => $this->targetId, 'name' => $this->target, 'type' => 'simple']);
94
        $t2 = $this->factory->get('target', ['id' => $this->targetId, 'name' => $this->target, 'type' => 'simple']);
95
        $t3 = $this->factory->get('target', ['id' => $this->targetId]);
96
        $t4 = $this->factory->get('target', $this->targetId);
97
        $t5 = $this->factory->find('target', [$this->targetId]);
98
        $this->assertInstanceOf(Target::class, $t1);
99
        $this->assertSame($this->target, $t1->getName());
100
        $this->assertSame($this->targetId, $t1->getId());
101
        $this->assertSame($t1, $t2);
102
        $this->assertSame($t1, $t3);
103
        $this->assertSame($t1, $t4);
104
        $this->assertSame($t1, $t5);
105
    }
106
107
    public function testGetPlan()
108
    {
109
        $str = $this->plan . ' ' . $this->reseller;
110
        $p1 = $this->factory->get('plan', ['id' => $this->planId, 'name' => $this->plan, 'seller' => $this->reseller]);
111
        $p2 = $this->factory->get('plan', ['name' => $this->plan, 'seller' => $this->reseller]);
112
        $p3 = $this->factory->get('plan', ['id' => $this->planId]);
113
        $p4 = $this->factory->get('plan', $str);
114
        $p5 = $this->factory->find('plan', [$str]);
115
        $this->assertInstanceOf(Plan::class, $p1);
116
        $this->assertSame($this->plan, $p1->getName());
117
        $this->assertSame($this->reseller, $p1->getSeller()->getLogin());
118
        $this->assertSame($p1, $p2);
119
        $this->assertSame($p1, $p3);
120
        $this->assertSame($p1, $p4);
121
        $this->assertSame($p1, $p5);
122
    }
123
124
    public function testGetPrice()
125
    {
126
        $p1 = $this->factory->get('price', [
127
            'id' => $this->priceId,
128
            'type' => $this->type,
129
            'target' => $this->targetId,
130
            'price' => $this->priceSum . ' ' . $this->currency,
131
            'prepaid' => '0 ' . $this->unit,
132
            'currency' => $this->currency,
133
        ]);
134
        $p2 = $this->factory->get('price', ['id' => $this->priceId, 'target' => $this->targetId]);
135
        $p3 = $this->factory->get('price', ['id' => $this->priceId]);
136
        $p4 = $this->factory->get('price', $this->priceId);
137
        $p5 = $this->factory->find('price', [$this->priceId]);
138
        $this->assertInstanceOf(PriceInterface::class, $p1);
139
        $this->assertSame($this->priceId, $p1->getId());
140
        $this->assertSame($this->type, $p1->getType()->getName());
141
        $this->assertSame($this->targetId, $p1->getTarget()->getId());
142
        $this->assertSame($this->unit, $p1->getPrepaid()->getUnit()->getName());
143
        $this->assertEquals($this->priceSum*100, $p1->getPrice()->getAmount());
144
        $this->assertSame($p1, $p2);
145
        $this->assertSame($p1, $p3);
146
        $this->assertSame($p1, $p4);
147
        $this->assertSame($p1, $p5);
148
    }
149
150
    public function testGetMoney()
151
    {
152
        $str = $this->priceSum . ' ' . $this->currency;
153
        $m1 = $this->factory->get('money', ['amount' => $this->priceSum, 'currency' => $this->currency]);
154
        $m2 = $this->factory->get('money', $str);
155
        $m3 = $this->factory->find('money', [$str]);
156
        $this->assertEquals($this->priceSum*100, $m1->getAmount());
157
        $this->assertSame($this->currency, $m1->getCurrency()->getCode());
158
        $this->assertSame($m1, $m2);
159
        $this->assertSame($m1, $m3);
160
    }
161
162
    public function testGetQuantity()
163
    {
164
        $str = $this->quantity . ' ' . $this->unit;
165
        $m1 = $this->factory->get('quantity', ['quantity' => $this->quantity, 'unit' => $this->unit]);
166
        $m2 = $this->factory->get('quantity', $str);
167
        $m3 = $this->factory->find('quantity', [$str]);
168
        $this->assertSame($this->quantity, $m1->getQuantity());
169
        $this->assertSame($this->unit, $m1->getUnit()->getName());
170
        $this->assertSame($m1, $m2);
171
        $this->assertSame($m1, $m3);
172
    }
173
174
    public function testGetUnit()
175
    {
176
        $u1 = $this->factory->get('unit', ['name' => $this->unit]);
177
        $u2 = $this->factory->get('unit', ['name' => $this->unit]);
178
        $u3 = $this->factory->get('unit', ['name' => $this->unit]);
179
        $u4 = $this->factory->get('unit', $this->unit);
180
        $u5 = $this->factory->find('unit', [$this->unit]);
181
        $this->assertInstanceOf(Unit::class, $u1);
182
        $this->assertSame($this->unit, $u1->getName());
183
        $this->assertSame($u1, $u2);
184
        $this->assertSame($u1, $u3);
185
        $this->assertSame($u1, $u4);
186
        $this->assertSame($u1, $u5);
187
    }
188
189
}
190