Completed
Push — master ( 4a945d...f56bff )
by Andrii
02:24
created

PlanTest::getPrices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\action\SimpleAction;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\units\Quantity;
16
17
class PlanTest extends \PHPUnit\Framework\TestCase
18
{
19
    protected function setUp()
20
    {
21
        $this->plan = CertificatePlan::get();
22
    }
23
24
    public function testCalculateCharge()
25
    {
26
        foreach ($this->plan->types as $typeName => $type) {
27
            foreach ($this->plan->targets as $targetName => $target) {
28
                foreach ([1, 2, 3] as $years) {
29
                    $price = $this->plan->getRawPrices($typeName, $targetName)[$years];
30
                    $this->checkCharge($type, $target, $years, $price);
31
                }
32
            }
33
        }
34
    }
35
36
    public function checkCharge($type, $target, $years, $sum)
37
    {
38
        $usage = Quantity::month($years * 12);
39
        $action = new SimpleAction(null, $type, $target, $usage);
40
        $charges = $this->plan->calculateCharges($action);
41
        $this->assertTrue(is_array($charges));
42
        $this->assertSame(1, count($charges));
43
        $charge = reset($charges);
44
        $this->assertInstanceOf(Charge::class, $charge);
45
        $this->assertSame($action, $charge->getAction());
46
        $this->assertSame($type, $charge->getType());
47
        $this->assertSame($target, $charge->getTarget());
48
        $this->assertTrue($sum->equals($charge->getSum()));
49
        $this->assertTrue($usage->equals($charge->getUsage()));
50
    }
51
}
52