Passed
Push — master ( 4bb1d0...373e60 )
by Dmitry
11:42
created

AggregatorTest::createOrder()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 13
rs 10
cc 4
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-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;
0 ignored issues
show
Bug introduced by
The type hiqdev\php\billing\tests\unit\plan\CertificatePlan 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...
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) {
0 ignored issues
show
Bug introduced by
Accessing types on the interface hiqdev\php\billing\plan\PlanInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
55
            foreach ($this->plan->targets as $target) {
0 ignored issues
show
Bug introduced by
Accessing targets on the interface hiqdev\php\billing\plan\PlanInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
56
                foreach ([1, 2, 3] as $years) {
57
                    $actions[] = new Action(null, $type, $target, Quantity::year($years), $this->plan->customer, $this->time);
0 ignored issues
show
Bug introduced by
Accessing customer on the interface hiqdev\php\billing\plan\PlanInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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());
0 ignored issues
show
Bug introduced by
The method getRawPrices() does not exist on hiqdev\php\billing\plan\PlanInterface. Did you maybe mean getPrices()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            /** @scrutinizer ignore-call */ 
74
            $prices = $this->plan->getRawPrices($bill->getType(), $bill->getTarget());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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