Passed
Push — master ( 87e7ce...9330d2 )
by Andrii
03:25
created

ActionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 54
dl 0
loc 127
rs 10
c 2
b 0
f 0
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A testChargesForNextMonthSalesAreNotCalculated() 0 10 1
A testChargesForThisMonthSalesAreCalculated() 0 10 1
A createAction() 0 3 1
A testCalculateCharge() 0 10 1
A testCalculateChargeNull() 0 5 1
A testGetHasSetId() 0 12 1
A tearDown() 0 2 1
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\action;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\action\Action;
15
use hiqdev\php\billing\charge\Charge;
16
use hiqdev\php\billing\charge\Generalizer;
17
use hiqdev\php\billing\customer\Customer;
18
use hiqdev\php\billing\customer\CustomerInterface;
19
use hiqdev\php\billing\order\Calculator;
20
use hiqdev\php\billing\plan\Plan;
21
use hiqdev\php\billing\price\SinglePrice;
22
use hiqdev\php\billing\sale\Sale;
23
use hiqdev\php\billing\target\Target;
24
use hiqdev\php\billing\type\Type;
25
use hiqdev\php\units\Quantity;
26
use hiqdev\php\units\QuantityInterface;
27
use Money\Money;
28
29
/**
30
 * @author Andrii Vasyliev <[email protected]>
31
 */
32
class ActionTest extends \PHPUnit\Framework\TestCase
33
{
34
    /**
35
     * @var SinglePrice
36
     */
37
    protected $price;
38
39
    /**
40
     * @var Action
41
     */
42
    protected $action;
43
44
    /**
45
     * @var Money
46
     */
47
    protected $money;
48
    /**
49
     * @var Type
50
     */
51
    protected $type;
52
    /**
53
     * @var Target
54
     */
55
    protected $target;
56
    /**
57
     * @var QuantityInterface
58
     */
59
    protected $prepaid;
60
    /**
61
     * @var Customer|CustomerInterface
62
     */
63
    protected $customer;
64
    /**
65
     * @var DateTimeImmutable
66
     */
67
    protected $time;
68
    /**
69
     * @var Generalizer
70
     */
71
    protected $generalizer;
72
    /**
73
     * @var Calculator
74
     */
75
    protected $calculator;
76
77
    protected $testId = 12321;
78
79
    protected function setUp()
80
    {
81
        $this->type     = new Type(null, 'server_traf');
82
        $this->target   = new Target(2, 'server');
83
        $this->prepaid  = Quantity::gigabyte(1);
84
        $this->money    = Money::USD(10000);
85
        $this->price    = new SinglePrice(5, $this->type, $this->target, null, $this->prepaid, $this->money);
86
        $this->customer = new Customer(2, 'client');
87
        $this->time     = new DateTimeImmutable('now');
88
        $this->generalizer = new Generalizer();
89
        $this->calculator = new Calculator($this->generalizer, null, null);
90
    }
91
92
    protected function createAction(QuantityInterface $quantity)
93
    {
94
        return new Action(null, $this->type, $this->target, $quantity, $this->customer, $this->time);
95
    }
96
97
    protected function tearDown()
98
    {
99
    }
100
101
    public function testCalculateCharge()
102
    {
103
        $action = $this->createAction($this->prepaid->multiply(2));
104
        $charge = $this->calculator->calculateCharge($this->price, $action);
105
        $this->assertInstanceOf(Charge::class, $charge);
106
        $this->assertSame($action, $charge->getAction());
107
        //$this->assertSame($this->target, $charge->getTarget());
108
        $this->assertSame($this->type, $charge->getType());
109
        $this->assertEquals($this->prepaid, $charge->getUsage());
110
        $this->assertEquals($this->money->multiply($this->prepaid->getQuantity()), $charge->getSum());
111
    }
112
113
    public function testCalculateChargeNull()
114
    {
115
        $action = $this->createAction($this->prepaid);
116
        $charge = $this->calculator->calculateCharge($this->price, $action);
117
        $this->assertNull($charge);
118
    }
119
120
    public function testChargesForNextMonthSalesAreNotCalculated()
121
    {
122
        $action = $this->createAction($this->prepaid->multiply(2));
123
124
        $plan = new Plan(null, '', $this->customer, [$this->price]);
125
        $futureSale = new Sale(null, $this->target, $this->customer, $plan, $this->time->add(new \DateInterval('P1M')));
126
        $action->setSale($futureSale);
127
128
        $charge = $this->calculator->calculateCharge($this->price, $action);
129
        $this->assertNull($charge);
130
    }
131
132
    public function testChargesForThisMonthSalesAreCalculated()
133
    {
134
        $action = $this->createAction($this->prepaid->multiply(2));
135
136
        $plan = new Plan(null, '', $this->customer, [$this->price]);
137
        $futureSale = new Sale(null, $this->target, $this->customer, $plan, $this->time->add(new \DateInterval('PT2S')));
138
        $action->setSale($futureSale);
139
140
        $charge = $this->calculator->calculateCharge($this->price, $action);
141
        $this->assertNotNull($charge);
142
    }
143
144
    /**
145
     * @expectedException \hiqdev\php\billing\Exception\CannotReassignException
146
     */
147
    public function testGetHasSetId()
148
    {
149
        $action = $this->createAction($this->prepaid->multiply(2));
150
        $this->assertFalse($action->hasId());
151
        $action->setId($this->testId);
152
        $this->assertTrue($action->hasId());
153
        $this->assertSame($this->testId, $action->getId());
154
        $action->setId((string)$this->testId);
155
        $this->assertSame($this->testId, $action->getId());
156
        $action->setId((int)$this->testId);
157
        $this->assertSame($this->testId, $action->getId());
158
        $action->setId('other id cannot be set');
159
    }
160
}
161