PriceHydratorTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 7
eloc 100
c 4
b 0
f 2
dl 0
loc 151
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testHydrateExistingEnumPrice() 0 10 1
A checkSinglePrice() 0 25 1
A testHydrateExistingSinglePrice() 0 9 1
A checkEnumPrice() 0 24 1
A setUp() 0 4 1
A testHydrateNewEnumPrice() 0 4 1
A testHydrateNewSinglePrice() 0 4 1
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\tests\unit\price;
12
13
use hiqdev\php\billing\price\EnumPrice;
14
use hiqdev\php\billing\price\PriceInterface;
15
use hiqdev\php\billing\price\SinglePrice;
16
use hiqdev\php\billing\target\Target;
17
use hiqdev\php\billing\type\Type;
18
use hiqdev\php\units\Quantity;
19
use hiqdev\php\units\Unit;
20
use hiqdev\yii\DataMapper\tests\unit\BaseHydratorTest;
21
use Money\Currency;
22
use Money\Money;
23
use yii\helpers\Json;
24
use Zend\Hydrator\HydratorInterface;
25
26
/**
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class PriceHydratorTest extends BaseHydratorTest
30
{
31
    const ID1   = '11111';
32
    const CUR1  = 'USD';
33
    const NAME1 = 'name-11111';
34
    const TYPE1 = 'server';
35
    const UNIT1 = 'MB';
36
37
    const ID2   = '22222';
38
    const CUR2  = 'EUR';
39
    const NAME2 = 'certificate_purchase';
40
    const TYPE2 = 'certificate';
41
    const UNIT2 = 'GB';
42
43
    protected $dataSinglePrice = [
44
        'id' => self::ID1,
45
        'type' => [
46
            'id'        => self::ID1,
47
            'name'      => self::NAME1,
48
        ],
49
        'target' => [
50
            'id'        => self::ID1,
51
            'type'      => self::TYPE1,
52
            'name'      => self::NAME1,
53
        ],
54
        'prepaid' => [
55
            'quantity'  => self::ID1,
56
            'unit'      => self::UNIT1,
57
        ],
58
        'price' => [
59
            'amount'    => self::ID1,
60
            'currency'  => self::CUR1,
61
        ],
62
    ];
63
64
    protected $dataEnumPrice = [
65
        'id' => self::ID2,
66
        'type' => [
67
            'id'        => self::ID2,
68
            'name'      => self::NAME2,
69
        ],
70
        'target' => [
71
            'id'        => self::ID2,
72
            'type'      => self::TYPE2,
73
            'name'      => self::NAME2,
74
        ],
75
        'prepaid' => [
76
            'unit'      => self::UNIT2,
77
        ],
78
        'price' => [
79
            'currency'  => self::CUR2,
80
        ],
81
    ];
82
83
    protected $sums = [
84
        1 => self::ID1,
85
        2 => self::ID2,
86
    ];
87
88
    public function setUp()
89
    {
90
        $this->hydrator = $this->getHydrator();
0 ignored issues
show
Bug Best Practice introduced by
The property hydrator does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
91
        $this->dataEnumPrice['data'] = Json::encode(['sums' => $this->sums]);
92
    }
93
94
    public function testHydrateNewSinglePrice()
95
    {
96
        $obj = $this->hydrator->hydrate($this->dataSinglePrice, PriceInterface::class);
97
        $this->checkSinglePrice($obj);
98
    }
99
100
    public function testHydrateExistingSinglePrice()
101
    {
102
        $type = new Type(self::ID2, self::NAME2);
103
        $target = new Target(self::ID2, self::TYPE2, self::NAME2);
104
        $price = new Money(self::ID2, new Currency(self::CUR2));
105
        $prepaid = Quantity::create(self::ID2, Unit::create(self::UNIT2));
106
        $obj = new SinglePrice(self::ID2, $type, $target, null, $prepaid, $price);
107
        $this->hydrator->hydrate($this->dataSinglePrice, $obj);
108
        $this->checkSinglePrice($obj);
109
    }
110
111
    public function checkSinglePrice($obj)
112
    {
113
        $this->assertInstanceOf(SinglePrice::class, $obj);
114
        $this->assertSame(self::ID1,    $obj->getId());
115
116
        $type = $obj->getType();
117
        $this->assertInstanceOf(Type::class, $type);
118
        $this->assertSame(self::ID1,    $type->getId());
119
        $this->assertSame(self::NAME1,  $type->getName());
120
121
        $target = $obj->getTarget();
122
        $this->assertInstanceOf(Target::class, $target);
123
        $this->assertSame(self::ID1,    $target->getId());
124
        $this->assertSame(self::TYPE1,  $target->getType());
125
        $this->assertSame(self::NAME1,  $target->getName());
126
127
        $prepaid = $obj->getPrepaid();
128
        $this->assertInstanceOf(Quantity::class, $prepaid);
129
        $this->assertSame(self::ID1,    $prepaid->getQuantity());
130
        $this->assertSame(self::UNIT1,  $prepaid->getUnit()->getName());
131
132
        $price = $obj->getPrice();
133
        $this->assertInstanceOf(Money::class, $price);
134
        $this->assertSame(self::ID1,    $price->getAmount());
135
        $this->assertSame(self::CUR1,   $price->getCurrency()->getCode());
136
    }
137
138
    public function testHydrateNewEnumPrice()
139
    {
140
        $obj = $this->hydrator->hydrate($this->dataEnumPrice, PriceInterface::class);
141
        $this->checkEnumPrice($obj);
142
    }
143
144
    public function testHydrateExistingEnumPrice()
145
    {
146
        $type = new Type(self::ID2, self::NAME2);
147
        $target = new Target(self::ID2, self::TYPE2, self::NAME2);
148
        $currency = new Currency(self::CUR2);
149
        $unit = Unit::create(self::UNIT2);
150
        $sums = array_reverse($this->sums);
151
        $obj = new EnumPrice(self::ID2, $type, $target, null, $unit, $currency, $sums);
152
        $this->hydrator->hydrate($this->dataEnumPrice, $obj);
153
        $this->checkEnumPrice($obj);
154
    }
155
156
    public function checkEnumPrice($obj)
157
    {
158
        $this->assertInstanceOf(EnumPrice::class, $obj);
159
        $this->assertSame(self::ID2,    $obj->getId());
160
        $this->assertSame($this->sums,  $obj->getSums());
161
162
        $type = $obj->getType();
163
        $this->assertInstanceOf(Type::class, $type);
164
        $this->assertSame(self::ID2,    $type->getId());
165
        $this->assertSame(self::NAME2,  $type->getName());
166
167
        $target = $obj->getTarget();
168
        $this->assertInstanceOf(Target::class, $target);
169
        $this->assertSame(self::ID2,    $target->getId());
170
        $this->assertSame(self::TYPE2,  $target->getType());
171
        $this->assertSame(self::NAME2,  $target->getName());
172
173
        $unit = $obj->getUnit();
174
        $this->assertInstanceOf(Unit::class, $unit);
175
        $this->assertSame(self::UNIT2,  $unit->getName());
176
177
        $currency = $obj->getCurrency();
178
        $this->assertInstanceOf(Currency::class, $currency);
179
        $this->assertSame(self::CUR2,   $currency->getCode());
180
    }
181
}
182