Completed
Push — master ( 541120...854615 )
by Andrii
05:42
created

PlanTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 36
rs 10
c 2
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A checkCharges() 0 14 1
A testCalculateCharges() 0 13 4
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 testCalculateCharges()
25
    {
26
        foreach ($this->plan->types as $type) {
27
            foreach ($this->plan->targets as $target) {
28
                foreach ([1, 2, 3] as $years) {
29
                    $usage = Quantity::month($years * 12);
30
                    $action = new SimpleAction(null, $type, $target, $usage);
31
                    $charges = $this->plan->calculateCharges($action);
32
                    $this->checkCharges($action, $charges);
33
                }
34
            }
35
        }
36
    }
37
38
    public function checkCharges($action, $charges)
39
    {
40
        $this->assertTrue(is_array($charges));
41
        $this->assertSame(1, count($charges));
42
        $charge = reset($charges);
43
        $price = $this->plan->getRawPrice($action);
44
        $usage = $action->getQuantity();
45
        $this->assertInstanceOf(Charge::class, $charge);
46
        $this->assertSame($action, $charge->getAction());
47
        $this->assertSame($action->getType(), $charge->getType());
48
        $this->assertSame($action->getTarget(), $charge->getTarget());
49
        $this->assertTrue($price->equals($charge->getSum()));
50
        $this->assertTrue($usage->equals($charge->getUsage()));
51
    }
52
}
53