Completed
Push — master ( 5a40d9...1fc46b )
by Andrii
03:05
created

FactoryTest::testGetQuantity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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