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

SinglePriceTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 26
c 2
b 1
f 0
dl 0
loc 61
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\price;
12
13
use hiqdev\php\billing\action\Action;
14
use hiqdev\php\billing\price\SinglePrice;
15
use hiqdev\php\billing\target\Target;
16
use hiqdev\php\billing\type\Type;
17
use hiqdev\php\units\Quantity;
18
use Money\Money;
19
20
/**
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class SinglePriceTest extends \PHPUnit\Framework\TestCase
24
{
25
    protected SinglePrice $price;
26
27
    protected Action $action;
28
29
    protected Money $money;
30
31
    private Target $target;
32
33
    private Type $type;
34
35
    private $quantity;
36
37
    protected function setUp(): void
38
    {
39
        $this->target   = new Target(1, 'server');
40
        $this->type     = new Type(null, 'server_traf');
41
        $this->quantity = Quantity::gigabyte(10);
42
        $this->money    = Money::USD(15);
43
        $this->price    = new SinglePrice(null, $this->type, $this->target, null, $this->quantity, $this->money);
44
    }
45
46
    protected function tearDown(): void
47
    {
48
    }
49
50
    public function testCalculateUsage()
51
    {
52
        $this->assertEquals(0, $this->price->calculateUsage(Quantity::byte(1))->getQuantity());
53
        $this->assertEquals(Quantity::gigabyte(90), $this->price->calculateUsage(Quantity::gigabyte(100)));
54
    }
55
56
    public function testCalculatePrice()
57
    {
58
        $this->assertEquals($this->money, $this->price->calculatePrice(Quantity::byte(1)));
59
        $this->assertEquals($this->money, $this->price->calculatePrice(Quantity::megabyte(1)));
60
    }
61
62
    public function testJsonSerialize()
63
    {
64
        $this->assertEquals([
65
            'type' => [
66
                'name' => 'server_traf',
67
            ],
68
            'prepaid' => [
69
                'unit' => 'gigabyte',
70
                'quantity' => 10,
71
            ],
72
            'price' => [
73
                'amount' => '15',
74
                'currency' => 'USD',
75
            ],
76
            'target' => [
77
                'id' => '1',
78
                'type' => 'server',
79
            ],
80
        ], json_decode(json_encode($this->price), true));
81
    }
82
}
83