Completed
Push — master ( 2bd21a...de5d06 )
by Dmitry
03:00
created

ActionTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testChargesForFutureSalesAreNotCalculated() 0 10 1
A setUp() 0 9 1
A createAction() 0 3 1
A testCalculateCharge() 0 10 1
A testCalculateChargeNull() 0 5 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\customer\Customer;
17
use hiqdev\php\billing\customer\CustomerInterface;
18
use hiqdev\php\billing\plan\Plan;
19
use hiqdev\php\billing\price\SinglePrice;
20
use hiqdev\php\billing\sale\Sale;
21
use hiqdev\php\billing\target\Target;
22
use hiqdev\php\billing\type\Type;
23
use hiqdev\php\units\Quantity;
24
use hiqdev\php\units\QuantityInterface;
25
use Money\Money;
26
27
/**
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
class ActionTest extends \PHPUnit\Framework\TestCase
31
{
32
    /**
33
     * @var SinglePrice
34
     */
35
    protected $price;
36
37
    /**
38
     * @var Action
39
     */
40
    protected $action;
41
42
    /**
43
     * @var Money
44
     */
45
    protected $money;
46
    /**
47
     * @var Type
48
     */
49
    protected $type;
50
    /**
51
     * @var Target
52
     */
53
    protected $target;
54
    /**
55
     * @var QuantityInterface
56
     */
57
    protected $prepaid;
58
    /**
59
     * @var Customer|CustomerInterface
60
     */
61
    protected $customer;
62
    /**
63
     * @var DateTimeImmutable
64
     */
65
    protected $time;
66
67
    protected function setUp()
68
    {
69
        $this->type     = new Type(null, 'server_traf');
70
        $this->target   = new Target(2, 'server');
71
        $this->prepaid  = Quantity::gigabyte(1);
72
        $this->money    = Money::USD(10000);
73
        $this->price    = new SinglePrice(5, $this->type, $this->target, null, $this->prepaid, $this->money);
74
        $this->customer = new Customer(2, 'client');
75
        $this->time     = new DateTimeImmutable('now');
76
    }
77
78
    protected function createAction(QuantityInterface $quantity)
79
    {
80
        return new Action(null, $this->type, $this->target, $quantity, $this->customer, $this->time);
81
    }
82
83
    protected function tearDown()
84
    {
85
    }
86
87
    public function testCalculateCharge()
88
    {
89
        $action = $this->createAction($this->prepaid->multiply(2));
90
        $charge = $action->calculateCharge($this->price);
91
        $this->assertInstanceOf(Charge::class, $charge);
92
        $this->assertSame($action, $charge->getAction());
93
        //$this->assertSame($this->target, $charge->getTarget());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
        $this->assertSame($this->type, $charge->getPrice()->getType());
95
        $this->assertEquals($this->prepaid, $charge->getUsage());
96
        $this->assertEquals($this->money->multiply($this->prepaid->getQuantity()), $charge->getSum());
97
    }
98
99
    public function testCalculateChargeNull()
100
    {
101
        $action = $this->createAction($this->prepaid);
102
        $charge = $action->calculateCharge($this->price);
103
        $this->assertNull($charge);
104
    }
105
106
    public function testChargesForFutureSalesAreNotCalculated()
107
    {
108
        $action = $this->createAction($this->prepaid->multiply(2));
109
110
        $plan = new Plan(null, '', $this->customer, [$this->price]);
111
        $futureSale = new Sale(null, $this->target, $this->customer, $plan, $this->time->add(new \DateInterval('P1D')));
112
        $action->setSale($futureSale);
113
114
        $charge = $action->calculateCharge($this->price);
115
        $this->assertNull($charge);
116
    }
117
}
118