Completed
Push — master ( 14a2bb...4a945d )
by Andrii
02:47
created

PlanTest::testCalculateCharge()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 0
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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit\plan;
12
13
use hiqdev\php\billing\charge\Charge;
14
use hiqdev\php\billing\customer\Customer;
15
use hiqdev\php\billing\action\SimpleAction;
16
use hiqdev\php\billing\plan\Plan;
17
use hiqdev\php\billing\price\EnumPrice;
18
use hiqdev\php\billing\target\Target;
19
use hiqdev\php\billing\type\Type;
20
use hiqdev\php\units\Quantity;
21
use hiqdev\php\units\Unit;
22
use Money\Money;
23
24
class PlanTest extends \PHPUnit\Framework\TestCase
25
{
26
    protected function setUp()
27
    {
28
        $this->seller   = new Customer(1, 'seller');
29
        $this->customer = new Customer(2, 'client', $this->seller);
30
        $this->purchase = new Type('certificate_purchase');
31
        $this->renewal  = new Type('certificate_renewal');
32
        $this->rapidssl = new Target('certificate_type', 'rapidssl_standard');
33
        $this->verisign = new Target('certificate_type', 'verisign_standard');
34
        $this->money    = Money::USD(15);
35
        $this->types = [
36
            'purchase'  => $this->purchase,
37
            'renewal'   => $this->renewal,
38
        ];
39
        $this->targets = [
40
            'rapidssl'  => $this->rapidssl,
41
            'verisign'  => $this->verisign,
42
        ];
43
        $this->prices   = [
44
            'purchase_rapidssl' => [
45
                1 => Money::USD(1129),
46
                2 => Money::USD(1219),
47
                3 => Money::USD(1309),
48
            ],
49
            'renewal_rapidssl' => [
50
                1 => Money::USD(1125),
51
                2 => Money::USD(1215),
52
                3 => Money::USD(1305),
53
            ],
54
            'purchase_verisign' => [
55
                1 => Money::USD(2129),
56
                2 => Money::USD(2219),
57
                3 => Money::USD(2309),
58
            ],
59
            'renewal_verisign' => [
60
                1 => Money::USD(2125),
61
                2 => Money::USD(2215),
62
                3 => Money::USD(2305),
63
            ],
64
        ];
65
        $prices = [];
66
        foreach ($this->types as $typeName => $type) {
67
            foreach ($this->targets as $targetName => $target) {
68
                $prices[] = new EnumPrice(null, $type, $target, Unit::year(), $this->getPrices($typeName, $targetName));
69
            }
70
        }
71
        $this->plan = new Plan(null, 'Test Certificate Plan', $this->seller, $prices);
72
    }
73
74
    public function getPrices($typeName, $targetName) {
75
        return $this->prices[$typeName . '_' . $targetName];
76
    }
77
78
    public function testCalculateCharge()
79
    {
80
        foreach ($this->types as $typeName => $type) {
81
            foreach ($this->targets as $targetName => $target) {
82
                foreach ([1, 2, 3] as $years) {
83
                    $price = $this->getPrices($typeName, $targetName)[$years];
84
                    $this->checkCharge($type, $target, $years, $price);
85
                }
86
            }
87
        }
88
    }
89
90
    public function checkCharge($type, $target, $years, $sum)
91
    {
92
        $usage = Quantity::month($years*12);
93
        $action = new SimpleAction(null, $type, $target, $usage);
94
        $charges = $this->plan->calculateCharges($action);
95
        $this->assertTrue(is_array($charges));
96
        $this->assertSame(1, count($charges));
97
        $charge = reset($charges);
98
        $this->assertInstanceOf(Charge::class, $charge);
99
        $this->assertSame($action, $charge->getAction());
100
        $this->assertSame($type, $charge->getType());
101
        $this->assertSame($target, $charge->getTarget());
102
        $this->assertTrue($sum->equals($charge->getSum()));
103
        $this->assertTrue($usage->equals($charge->getUsage()));
104
    }
105
}
106