Completed
Push — master ( 428142...9f94b9 )
by Andrii
02:14
created

AggregatorTest::setUp()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 7
Ratio 43.75 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 7
loc 16
rs 9.2
c 1
b 0
f 1
cc 4
eloc 11
nc 4
nop 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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit\order;
12
13
use hiqdev\php\billing\action\SimpleAction;
14
use hiqdev\php\billing\charge\Aggregator;
15
use hiqdev\php\billing\order\Calculator;
16
use hiqdev\php\billing\order\Order;
17
use hiqdev\php\billing\tests\unit\plan\PlanTest;
18
use hiqdev\php\billing\tests\unit\plan\SimplePlanRepository;
19
use hiqdev\php\units\Quantity;
20
use Money\Money;
21
22
class AggregatorTest extends PlanTest
23
{
24
    protected function setUp()
25
    {
26
        parent::setUp();
27
        $this->repository = new SimplePlanRepository($this->plan);
28
        $this->calculator = new Calculator($this->repository);
29
        $this->aggregator = new Aggregator();
30
        $actions = [];
31 View Code Duplication
        foreach ($this->plan->types as $type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
            foreach ($this->plan->targets as $target) {
33
                foreach ([1, 2, 3] as $years) {
34
                    $actions[] = new SimpleAction(null, $type, $target, Quantity::year($years), $this->plan->customer);
35
                }
36
            }
37
        }
38
        $this->order = new Order(null, $this->plan->customer, $actions);
39
    }
40
41
    public function testCalculateCharges()
42
    {
43
        $charges = $this->calculator->calculateCharges($this->order);
44
        $bills = $this->aggregator->aggregateCharges($charges);
45
        $this->assertSame(4, count($bills));
46
        foreach ($bills as $bill) {
47
            $prices = $this->plan->getRawPrices($bill->getType(), $bill->getTarget());
48
            $sum = Money::USD(array_sum($prices));
49
            $this->assertTrue($sum->equals($bill->getSum()));
50
            $this->assertEquals(6, $bill->getQuantity()->getQuantity());
51
            $this->assertEquals(3, count($bill->getCharges()));
52
            foreach ($bill->getCharges() as $charge) {
53
                $this->assertTrue($bill->getType()->equals($charge->getPrice()->getType()));
54
                $this->assertTrue($bill->getTarget()->equals($charge->getTarget()));
55
            }
56
        }
57
    }
58
}
59