Passed
Push — master ( bb3e2c...438d9c )
by Andrii
03:09
created

SinglePriceTest::testJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.8333
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\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
    /**
26
     * @var SinglePrice
27
     */
28
    protected $price;
29
30
    /**
31
     * @var Action
32
     */
33
    protected $action;
34
35
    /**
36
     * @var Money
37
     */
38
    protected $money;
39
40
    protected function setUp()
41
    {
42
        $this->target   = new Target(1, 'server');
0 ignored issues
show
Bug Best Practice introduced by
The property target does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->type     = new Type(null, 'server_traf');
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
        $this->quantity = Quantity::gigabyte(10);
0 ignored issues
show
Bug Best Practice introduced by
The property quantity does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
        $this->money    = Money::USD(15);
46
        $this->price    = new SinglePrice(null, $this->type, $this->target, null, $this->quantity, $this->money);
47
    }
48
49
    protected function tearDown()
50
    {
51
    }
52
53
    public function testCalculateUsage()
54
    {
55
        $this->assertNull($this->price->calculateUsage(Quantity::byte(1)));
56
        $this->assertEquals(Quantity::gigabyte(90), $this->price->calculateUsage(Quantity::gigabyte(100)));
57
    }
58
59
    public function testCalculatePrice()
60
    {
61
        $this->assertEquals($this->money, $this->price->calculatePrice(Quantity::byte(1)));
62
        $this->assertEquals($this->money, $this->price->calculatePrice(Quantity::megabyte(1)));
63
    }
64
65
    public function testJsonSerialize()
66
    {
67
        $this->assertEquals([
68
            'type' => [
69
                'name' => 'server_traf',
70
            ],
71
            'prepaid' => [
72
                'unit' => 'gigabyte',
73
                'quantity' => 10,
74
            ],
75
            'price' => [
76
                'amount' => '15',
77
                'currency' => 'USD',
78
            ],
79
            'target' => [
80
                'id' => '1',
81
                'type' => 'server',
82
            ],
83
        ], json_decode(json_encode($this->price), true));
84
    }
85
}
86