Passed
Pull Request — master (#99)
by
unknown
13:18
created

PriceFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 44
c 1
b 0
f 1
dl 0
loc 63
rs 10
wmc 5
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-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit;
12
13
use hiqdev\php\billing\price\EnumPrice;
14
use hiqdev\php\billing\price\PriceCreationDto;
15
use hiqdev\php\billing\price\PriceFactory;
16
use hiqdev\php\billing\price\SinglePrice;
17
use hiqdev\php\billing\target\Target;
18
use hiqdev\php\billing\type\Type;
19
use hiqdev\php\units\Quantity;
20
use hiqdev\php\units\Unit;
21
use Money\Money;
22
23
/**
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class PriceFactoryTest extends \PHPUnit\Framework\TestCase
27
{
28
    private string $id;
29
30
    private Type $single;
31
32
    private Type $enum;
33
34
    private Target $target;
35
36
    private $prepaid;
37
38
    private Money $price;
39
40
    private $unit;
41
42
    private array $sums;
43
44
    private PriceFactory $factory;
45
46
    protected function setUp(): void
47
    {
48
        $this->id       = 'foo:bar';
49
        $this->single   = new Type(null, 'server_traf');
50
        $this->enum     = new Type(null, 'certificate_purchase');
51
        $this->target   = new Target(1, 'server');
52
        $this->prepaid  = Quantity::gigabyte(10);
53
        $this->price    = Money::USD(15);
54
        $this->unit     = Unit::gigabyte();
55
        $this->sums     = [];
56
        $this->factory  = new PriceFactory([
57
            $this->single->getName()    => SinglePrice::class,
58
            $this->enum->getName()      => EnumPrice::class,
59
            'other'                     => 'other',
60
        ], SinglePrice::class);
61
    }
62
63
    public function testEnumPrice()
64
    {
65
        $price = $this->factory->create($this->createDto([
66
            'id'        => $this->id,
67
            'type'      => $this->enum,
68
            'target'    => $this->target,
69
            'unit'      => $this->unit,
70
            'currency'  => $this->price->getCurrency(),
71
            'sums'      => $this->sums,
72
        ]));
73
        $this->assertInstanceOf(EnumPrice::class, $price);
74
        $this->assertSame($this->id,        $price->getId());
75
        $this->assertSame($this->enum,      $price->getType());
76
        $this->assertSame($this->target,    $price->getTarget());
77
        $this->assertSame($this->unit,      $price->getUnit());
78
        $this->assertSame($this->sums,      $price->getSums()->values());
79
        $this->assertSame($this->price->getCurrency(),  $price->getCurrency());
80
    }
81
82
    public function testSinglePrice()
83
    {
84
        $price = $this->factory->create($this->createDto([
85
            'id'        => $this->id,
86
            'type'      => $this->single,
87
            'target'    => $this->target,
88
            'prepaid'   => $this->prepaid,
89
            'price'     => $this->price,
90
        ]));
91
        $this->assertInstanceOf(SinglePrice::class, $price);
92
        $this->assertSame($this->id,        $price->getId());
93
        $this->assertSame($this->single,    $price->getType());
94
        $this->assertSame($this->target,    $price->getTarget());
95
        $this->assertSame($this->prepaid,   $price->getPrepaid());
96
        $this->assertSame($this->price,     $price->getPrice());
97
    }
98
99
    public function createDto(array $data)
100
    {
101
        $dto = new PriceCreationDto();
102
        foreach ($data as $key => $value) {
103
            $dto->{$key} = $value;
104
        }
105
106
        return $dto;
107
    }
108
}
109