Passed
Pull Request — master (#67)
by
unknown
13:58
created

ProgressivePriceTest::testProgressivePrice()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 30
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 42
rs 9.44
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' => [
48
                    'thresholds' => $thresholds,
49
                ],
50
                'price' => [
51
                    "amount" => (string) (MoneyBuilder::calculatePriceMultiplier($startPrice) * $startPrice),
52
                    "currency" =>"EUR",
53
                ],
54
                'prepaid' => [
55
                    'unit' => 'mbps',
56
                    'quantity' => 0,
57
                ],
58
            ]),
59
            json_encode($price));
60
    }
61
62
    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...
63
    {
64
        yield [
65
            'thresholds' => [
66
                ['price' => '0.0085', 'currency' => 'EUR', 'quantity' => '0', 'unit' => 'mbps',],
67
                ['price' => '0.0080', 'currency' => 'EUR', 'quantity' => '500', 'unit' => 'mbps',],
68
                ['price' => '0.0075', 'currency' => 'EUR', 'quantity' => '600', 'unit' => 'mbps',],
69
                ['price' => '0.0070', 'currency' => 'EUR', 'quantity' => '700', 'unit' => 'mbps',],
70
                ['price' => '0.0065', 'currency' => 'EUR', 'quantity' => '800', 'unit' => 'mbps',],
71
            ],
72
            'money' => 594,
73
            'price' => '0.0085',
74
        ];
75
        yield [
76
            'thresholds' => [
77
                ['price' => '6', 'currency' => 'EUR', 'quantity' => '0', 'unit' => 'mbps', ],
78
                ['price' => '5', 'currency' => 'EUR', 'quantity' => '100', 'unit' => 'mbps', ],
79
                ['price' => '4', 'currency' => 'EUR', 'quantity' => '100', 'unit' => 'mbps', ],
80
                ['price' => '3', 'currency' => 'EUR', 'quantity' => '200', 'unit' => 'mbps', ],
81
            ],
82
            'money' => 266000,
83
            'price' => '6',
84
        ];
85
        yield [
86
            'thresholds' => [
87
                ['price' => '6', 'currency' => 'EUR', 'quantity' => '100', 'unit' => 'mbps', ],
88
                ['price' => '5', 'currency' => 'EUR', 'quantity' => '200', 'unit' => 'mbps', ],
89
                ['price' => '4', 'currency' => 'EUR', 'quantity' => '300', 'unit' => 'mbps', ],
90
                ['price' => '3', 'currency' => 'EUR', 'quantity' => '400', 'unit' => 'mbps', ],
91
            ],
92
            'money' => 246000,
93
            'price' => '6',
94
        ];
95
    }
96
}
97