Completed
Push — master ( cb4835...b23375 )
by Andrii
03:01
created

PriceHydratorTest::testHydrateNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
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\PriceInterface;
14
use hiqdev\php\billing\price\SinglePrice;
15
use hiqdev\php\billing\type\Type;
16
use hiqdev\php\billing\target\Target;
17
use hiqdev\php\units\Quantity;
18
use hiqdev\php\units\Unit;
19
use Money\Currency;
20
use Money\Money;
21
use Yii;
22
use Zend\Hydrator\HydratorInterface;
23
24
/**
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class PriceHydratorTest extends \PHPUnit\Framework\TestCase
28
{
29
    const ID1   = '11111';
30
    const CUR1  = 'USD';
31
    const NAME1 = 'name-11111';
32
    const TYPE1 = 'type-11111';
33
    const UNIT1 = 'MB';
34
35
    const ID2   = '22222';
36
    const CUR2  = 'EUR';
37
    const NAME2 = 'name-22222';
38
    const TYPE2 = 'type-22222';
39
    const UNIT2 = 'GB';
40
41
    protected $data = [
42
        'id' => self::ID1,
43
        'type' => [
44
            'id'        => self::ID1,
45
            'name'      => self::NAME1,
46
        ],
47
        'target' => [
48
            'id'        => self::ID1,
49
            'type'      => self::TYPE1,
50
            'name'      => self::NAME1,
51
        ],
52
        'prepaid' => [
53
            'quantity'  => self::ID1,
54
            'unit'      => self::UNIT1,
55
        ],
56
        'price' => [
57
            'amount'    => self::ID1,
58
            'currency'  => self::CUR1,
59
        ],
60
    ];
61
62
    public function setUp()
63
    {
64
        $this->hydrator = Yii::$container->get(HydratorInterface::class);
65
    }
66
67
    public function testHydrateNew()
68
    {
69
        $obj = $this->hydrator->hydrate($this->data, PriceInterface::class);
70
        $this->checkSimplePrice($obj);
71
    }
72
73
    public function testHydrateOld()
74
    {
75
        $type = new Type(self::ID2, self::NAME2);
76
        $target = new Target(self::ID2, self::TYPE2, self::NAME2);
77
        $price = new Money(self::ID2, new Currency(self::CUR2));
78
        $prepaid = Quantity::create(self::ID2, Unit::create(self::UNIT2));
79
        $obj = new SinglePrice(self::ID2, $type, $target, null, $prepaid, $price);
80
        $this->hydrator->hydrate($this->data, $obj);
81
        $this->checkSimplePrice($obj);
82
    }
83
84
    public function checkSimplePrice($obj)
85
    {
86
        $this->assertInstanceOf(SinglePrice::class, $obj);
87
        $this->assertSame(self::ID1,    $obj->getId());
88
89
        $type = $obj->getType();
90
        $this->assertInstanceOf(Type::class, $type);
91
        $this->assertSame(self::ID1,    $type->getId());
92
        $this->assertSame(self::NAME1,  $type->getName());
93
94
        $target = $obj->getTarget();
95
        $this->assertInstanceOf(Target::class, $target);
96
        $this->assertSame(self::ID1,    $target->getId());
97
        $this->assertSame(self::TYPE1,  $target->getType());
98
        $this->assertSame(self::NAME1,  $target->getName());
99
100
        $prepaid = $obj->getPrepaid();
101
        $this->assertInstanceOf(Quantity::class, $prepaid);
102
        $this->assertSame(self::ID1,    $prepaid->getQuantity());
103
        $this->assertSame(self::UNIT1,  $prepaid->getUnit()->getName());
104
105
        $price = $obj->getPrice();
106
        $this->assertInstanceOf(Money::class, $price);
107
        $this->assertSame(self::ID1,    $price->getAmount());
108
        $this->assertSame(self::CUR1,   $price->getCurrency()->getCode());
109
    }
110
}
111