Passed
Pull Request — master (#78)
by
unknown
14:55
created

FixedDiscountTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 3
Metric Value
eloc 35
c 4
b 1
f 3
dl 0
loc 70
rs 10
wmc 9
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\charge\modifiers;
12
13
use hiqdev\php\billing\charge\Charge;
14
use hiqdev\php\billing\charge\modifiers\FixedDiscount;
15
use hiqdev\php\billing\tests\unit\action\ActionTest;
16
use hiqdev\php\units\Quantity;
17
use Money\Money;
18
19
/**
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class FixedDiscountTest extends ActionTest
23
{
24
    protected function setUp(): void
25
    {
26
        parent::setUp();
27
        $this->value = Money::USD(1000);
28
        $this->rate = '10';
29
    }
30
31
    protected function buildDiscount($value)
32
    {
33
        return new FixedDiscount($value);
34
    }
35
36
    public function testCreateAbsolute()
37
    {
38
        $abs = $this->buildDiscount($this->value);
39
        $this->assertAbsolute($this->value, $abs);
40
        $abs = $this->buildDiscount('10 USD');
41
        $this->assertAbsolute($this->value, $abs);
42
        $abs = $this->buildDiscount('10.00 USD');
43
        $this->assertAbsolute($this->value, $abs);
44
    }
45
46
    public function assertAbsolute($value, $abs)
47
    {
48
        $this->assertEquals($value, $abs->getValue()->getValue());
49
        $this->assertTrue($abs->isAbsolute());
50
        $this->assertFalse($abs->isRelative());
51
    }
52
53
    public function testCreateRelative()
54
    {
55
        $rel = $this->buildDiscount($this->rate);
56
        $this->assertRelative($this->rate, $rel);
57
        $rel = $this->buildDiscount($this->rate . '%');
58
        $this->assertRelative($this->rate, $rel);
59
    }
60
61
    protected function assertRelative($rate, $rel)
62
    {
63
        $this->assertSame($rate, $rel->getValue()->getValue());
64
        $this->assertTrue($rel->isRelative());
65
        $this->assertFalse($rel->isAbsolute());
66
    }
67
68
    public function testAbsoluteModifyCharge()
69
    {
70
        $abs = $this->buildDiscount($this->value);
71
        $this->assertCharges($abs, $this->value);
72
    }
73
74
    public function testRelativeModifyCharge()
75
    {
76
        $rel = $this->buildDiscount($this->rate);
77
        $this->assertCharges($rel, $this->value);
78
    }
79
80
    public function assertCharges($fd, $sum)
81
    {
82
        $action = $this->createAction($this->prepaid->multiply(2));
83
        $charge = $this->calculator->calculateCharge($this->price, $action);
84
        $charges = $fd->modifyCharge($charge, $action);
85
        $this->assertIsArray($charges);
86
        $this->assertSame(2, count($charges));
87
        $this->assertSame($charge, $charges[0]);
88
        $discount = $charges[1];
89
        $this->assertInstanceOf(Charge::class, $discount);
90
        $this->assertEquals(Quantity::items(0), $discount->getUsage());
91
        $this->assertEquals($sum->multiply(-1), $discount->getSum());
92
    }
93
}
94