Passed
Push — master ( e39bb0...54e3cc )
by Dmitry
14:48
created

ProgressivePriceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A progressivePriceProvider() 0 32 1
A testProgressivePrice() 0 40 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\tests\unit\price;
6
7
use hiqdev\php\billing\price\MoneyBuilder;
8
use hiqdev\php\billing\price\ProgressivePrice;
9
use hiqdev\php\billing\target\Target;
10
use hiqdev\php\billing\type\Type;
11
use hiqdev\php\units\Quantity;
12
use Money\Money;
13
14
15
class ProgressivePriceTest extends \PHPUnit\Framework\TestCase
16
{
17
    /** @dataProvider progressivePriceProvider */
18
    public function testProgressivePrice(array $thresholds, int $resultMoney, string $startPrice)
19
    {
20
        $type = new Type('2222', 'cdn_traf95_max');
21
        $target = new Target('2222', 'overuse,cdn_traf95_max', 'ProgressivePrice');
22
        $prepaid = Quantity::mbps(0);
23
        $money = MoneyBuilder::buildMoney((string)$startPrice, 'EUR');
24
        $price = new ProgressivePrice('2222', $type, $target, $prepaid, $money, $thresholds);
25
        $this->assertSame(720, $price->calculateUsage(Quantity::mbps(720))->getQuantity());
26
        usort($thresholds, function($a, $b)
27
            {
28
                if ($b['quantity'] === $a['quantity']) {
29
                    return $b['price'] <=> $a['price'];
30
                }
31
                return $b['quantity'] <=> $a['quantity'];
32
            }
33
        );
34
        $this->assertSame(json_encode(Money::EUR($resultMoney)), json_encode($price->calculateSum(Quantity::mbps(720))));
35
        $this->assertSame(
36
            json_encode([
37
                'id' => '2222',
38
                'type' => [
39
                    'id' => '2222',
40
                    'name' => 'cdn_traf95_max',
41
                ],
42
                'target' => [
43
                    'id' => '2222',
44
                    'type' => 'overuse,cdn_traf95_max',
45
                    'name' => 'ProgressivePrice'
46
                ],
47
                'thresholds' => $thresholds,
48
                'price' => [
49
                    "amount" => (string) (MoneyBuilder::calculatePriceMultiplier($startPrice) * $startPrice),
50
                    "currency" =>"EUR",
51
                ],
52
                'prepaid' => [
53
                    'unit' => 'mbps',
54
                    'quantity' => 0,
55
                ],
56
            ]),
57
            json_encode($price));
58
    }
59
60
    private function progressivePriceProvider(): \Generator
0 ignored issues
show
Unused Code introduced by
The method progressivePriceProvider() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
61
    {
62
        yield [
63
            'thresholds' => [
64
                ['price' => '0.0085', 'currency' => 'EUR', 'quantity' => '0', 'unit' => 'mbps',],
65
                ['price' => '0.0080', 'currency' => 'EUR', 'quantity' => '500', 'unit' => 'mbps',],
66
                ['price' => '0.0075', 'currency' => 'EUR', 'quantity' => '600', 'unit' => 'mbps',],
67
                ['price' => '0.0070', 'currency' => 'EUR', 'quantity' => '700', 'unit' => 'mbps',],
68
                ['price' => '0.0065', 'currency' => 'EUR', 'quantity' => '800', 'unit' => 'mbps',],
69
            ],
70
            'money' => 594,
71
            'price' => '0.0085',
72
        ];
73
        yield [
74
            'thresholds' => [
75
                ['price' => '6', 'currency' => 'EUR', 'quantity' => '0', 'unit' => 'mbps', ],
76
                ['price' => '5', 'currency' => 'EUR', 'quantity' => '100', 'unit' => 'mbps', ],
77
                ['price' => '4', 'currency' => 'EUR', 'quantity' => '100', 'unit' => 'mbps', ],
78
                ['price' => '3', 'currency' => 'EUR', 'quantity' => '200', 'unit' => 'mbps', ],
79
            ],
80
            'money' => 266000,
81
            'price' => '6',
82
        ];
83
        yield [
84
            'thresholds' => [
85
                ['price' => '6', 'currency' => 'EUR', 'quantity' => '100', 'unit' => 'mbps', ],
86
                ['price' => '5', 'currency' => 'EUR', 'quantity' => '200', 'unit' => 'mbps', ],
87
                ['price' => '4', 'currency' => 'EUR', 'quantity' => '300', 'unit' => 'mbps', ],
88
                ['price' => '3', 'currency' => 'EUR', 'quantity' => '400', 'unit' => 'mbps', ],
89
            ],
90
            'money' => 246000,
91
            'price' => '6',
92
        ];
93
    }
94
}
95