Completed
Push — master ( 6d0a1b...161548 )
by Andrii
03:57
created

AggregatorTest::setUp()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 0
dl 0
loc 16
rs 9.9
c 0
b 0
f 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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit\tools;
12
13
use hiqdev\php\billing\action\Action;
14
use hiqdev\php\billing\charge\Generalizer;
15
use hiqdev\php\billing\order\Calculator;
16
use hiqdev\php\billing\order\CalculatorInterface;
17
use hiqdev\php\billing\order\Order;
18
use hiqdev\php\billing\order\OrderInterface;
19
use hiqdev\php\billing\tests\unit\plan\CertificatePlan;
20
use hiqdev\php\billing\tests\unit\sale\SaleTest;
21
use hiqdev\php\billing\tools\Aggregator;
22
use hiqdev\php\billing\tools\AggregatorInterface;
23
use hiqdev\php\units\Quantity;
24
use Money\Money;
25
26
class AggregatorTest extends SaleTest
27
{
28
    /**
29
     * @var Generalizer|GeneralizerInterface
0 ignored issues
show
Bug introduced by Andrii Vasyliev
The type hiqdev\php\billing\tests...ls\GeneralizerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     */
31
    protected $generalizer;
32
    /**
33
     * @var Calculator|CalculatorInterface
34
     */
35
    protected $calculator;
36
    /**
37
     * @var Aggregator|AggregatorInterface
38
     */
39
    protected $aggregator;
40
    /**
41
     * @var Order|OrderInterface
42
     */
43
    protected $order;
44
    /**
45
     * @var CertificatePlan
46
     */
47
    protected $plan;
48
49
    protected function setUp()
50
    {
51
        parent::setUp();
52
        $this->generalizer = new Generalizer();
53
        $this->calculator = new Calculator($this->generalizer, $this->repository, null);
54
        $this->aggregator = new Aggregator($this->generalizer);
55
        $actions = [];
56
        foreach ($this->plan->types as $type) {
57
            foreach ($this->plan->targets as $target) {
58
                foreach ([1, 2, 3] as $years) {
59
                    $actions[] = new Action(null, $type, $target, Quantity::year($years), $this->plan->customer, $this->time);
60
                }
61
            }
62
        }
63
        shuffle($actions);
64
        $this->order = new Order(null, $this->plan->customer, $actions);
65
    }
66
67
    public function testCalculateCharges()
68
    {
69
        $charges = $this->calculator->calculateOrder($this->order);
70
        $bills = $this->aggregator->aggregateCharges($charges);
71
        $this->assertCount(4, $bills);
72
        foreach ($bills as $bill) {
73
            $prices = $this->plan->getRawPrices($bill->getType(), $bill->getTarget());
74
            $sum = Money::USD(array_sum($prices));
75
            $this->assertTrue($sum->negative()->equals($bill->getSum()));
76
            $this->assertEquals(6, $bill->getQuantity()->getQuantity());
77
            $this->assertEquals(3, count($bill->getCharges()));
78
            foreach ($bill->getCharges() as $charge) {
79
                $this->assertTrue($bill->getType()->equals($charge->getType()));
80
                $this->assertTrue($bill->getTarget()->equals($charge->getTarget()));
81
            }
82
        }
83
    }
84
}
85