Passed
Pull Request — master (#99)
by
unknown
02:46
created

AggregatorTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
eloc 31
c 2
b 0
f 1
dl 0
loc 53
rs 10
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-2020, 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\charge\GeneralizerInterface;
16
use hiqdev\php\billing\order\Calculator;
17
use hiqdev\php\billing\order\CalculatorInterface;
18
use hiqdev\php\billing\order\Order;
19
use hiqdev\php\billing\order\OrderInterface;
20
use hiqdev\php\billing\tests\support\plan\SimplePlanRepository;
21
use hiqdev\php\billing\tests\unit\plan\CertificatePlan;
22
use hiqdev\php\billing\tests\unit\sale\SaleTest;
23
use hiqdev\php\billing\tools\Aggregator;
24
use hiqdev\php\billing\tools\AggregatorInterface;
25
use hiqdev\php\units\Quantity;
26
use Money\Money;
27
use hiqdev\php\billing\tools\CachedDateTimeProvider;
28
29
class AggregatorTest extends SaleTest
30
{
31
    protected GeneralizerInterface $generalizer;
32
33
    protected CalculatorInterface $calculator;
34
35
    protected AggregatorInterface $aggregator;
36
37
    protected OrderInterface $order;
38
39
    protected function setUp(): void
40
    {
41
        parent::setUp();
42
43
        $this->generalizer = new Generalizer();
44
        $planRepository = new SimplePlanRepository();
45
        $timeProvider = new CachedDateTimeProvider($this->time);
46
        $this->calculator = new Calculator($this->generalizer, $this->repository, $planRepository, $timeProvider);
47
        $this->aggregator = new Aggregator($this->generalizer);
48
        $this->order = $this->createOrder();
49
    }
50
51
    private function createOrder(): OrderInterface
52
    {
53
        $actions = [];
54
        foreach ($this->plan->types as $type) {
55
            foreach ($this->plan->targets as $target) {
56
                foreach ([1, 2, 3] as $years) {
57
                    $actions[] = new Action(null, $type, $target, Quantity::year($years), $this->plan->customer, $this->time);
58
                }
59
            }
60
        }
61
        shuffle($actions);
62
63
        return new Order(null, $this->plan->customer, $actions);
64
    }
65
66
    public function testCalculateCharges(): void
67
    {
68
        $charges = $this->calculator->calculateOrder($this->order);
69
        $bills = $this->aggregator->aggregateCharges($charges);
70
        $this->assertCount(4, $bills);
71
72
        foreach ($bills as $bill) {
73
            $prices = $this->plan->getRawPrices($bill->getType(), $bill->getTarget())->values();
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
79
            foreach ($bill->getCharges() as $charge) {
80
                $this->assertTrue($bill->getType()->equals($charge->getType()));
81
                $this->assertTrue($bill->getTarget()->equals($charge->getTarget()));
82
            }
83
        }
84
    }
85
}
86