Completed
Push — master ( 8debe9...7ec561 )
by Dmitry
02:47
created

AggregatorTest::setUp()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 0
dl 0
loc 15
rs 9.9332
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\charge;
12
13
use hiqdev\php\billing\action\Action;
14
use hiqdev\php\billing\charge\Aggregator;
15
use hiqdev\php\billing\charge\AggregatorInterface;
16
use hiqdev\php\billing\charge\Generalizer;
17
use hiqdev\php\billing\order\Calculator;
18
use hiqdev\php\billing\order\CalculatorInterface;
19
use hiqdev\php\billing\order\Order;
20
use hiqdev\php\billing\order\OrderInterface;
21
use hiqdev\php\billing\tests\unit\plan\CertificatePlan;
22
use hiqdev\php\billing\tests\unit\sale\SaleTest;
23
use hiqdev\php\units\Quantity;
24
use Money\Money;
25
26
class AggregatorTest extends SaleTest
27
{
28
    /**
29
     * @var Calculator|CalculatorInterface
30
     */
31
    protected $calculator;
32
    /**
33
     * @var Aggregator|AggregatorInterface
34
     */
35
    protected $aggregator;
36
    /**
37
     * @var Order|OrderInterface
38
     */
39
    protected $order;
40
    /**
41
     * @var CertificatePlan
42
     */
43
    protected $plan;
44
45
    protected function setUp()
46
    {
47
        parent::setUp();
48
        $this->calculator = new Calculator($this->repository, null);
49
        $this->aggregator = new Aggregator(new Generalizer());
50
        $actions = [];
51
        foreach ($this->plan->types as $type) {
52
            foreach ($this->plan->targets as $target) {
53
                foreach ([1, 2, 3] as $years) {
54
                    $actions[] = new Action(null, $type, $target, Quantity::year($years), $this->plan->customer, $this->time);
55
                }
56
            }
57
        }
58
        shuffle($actions);
59
        $this->order = new Order(null, $this->plan->customer, $actions);
60
    }
61
62
    public function testCalculateCharges()
63
    {
64
        $charges = $this->calculator->calculateCharges($this->order);
65
        $bills = $this->aggregator->aggregateCharges($charges);
66
        $this->assertCount(4, $bills);
67
        foreach ($bills as $bill) {
68
            $prices = $this->plan->getRawPrices($bill->getType(), $bill->getTarget());
69
            $sum = Money::USD(array_sum($prices));
70
            $this->assertTrue($sum->negative()->equals($bill->getSum()));
71
            $this->assertEquals(6, $bill->getQuantity()->getQuantity());
72
            $this->assertEquals(3, count($bill->getCharges()));
73
            foreach ($bill->getCharges() as $charge) {
74
                $this->assertTrue($bill->getType()->equals($charge->getPrice()->getType()));
75
                $this->assertTrue($bill->getTarget()->equals($charge->getAction()->getTarget()));
76
            }
77
        }
78
    }
79
}
80