Completed
Push — master ( f0c93a...888950 )
by Andrii
05:26
created

FixedDiscountTest::testAbsModifyCharge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 3
nc 1
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-2018, 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()
25
    {
26
        parent::setUp();
27
        $this->value = Money::USD(1000);
28
        $this->rate = '10';
29
    }
30
31
    public function testCreateAbsolute()
32
    {
33
        $abs = new FixedDiscount($this->value);
34
        $this->assertSame($this->value, $abs->getValue());
35
        $this->assertTrue($abs->isAbsolute());
36
        $this->assertFalse($abs->isRelative());
37
    }
38
39
    public function testCreateRelative()
40
    {
41
        $rel = new FixedDiscount($this->rate);
42
        $this->assertRelative($this->rate, $rel);
43
        $rel = new FixedDiscount($this->rate . '%');
44
        $this->assertRelative($this->rate, $rel);
45
    }
46
47
    protected function assertRelative($rate, $rel)
48
    {
49
        $this->assertSame($rate, $rel->getValue());
50
        $this->assertTrue($rel->isRelative());
51
        $this->assertFalse($rel->isAbsolute());
52
    }
53
54
    public function testAbsoluteModifyCharge()
55
    {
56
        $abs = new FixedDiscount($this->value);
57
        $this->assertFixedDiscountCharges($abs, $this->value);
58
    }
59
60
    public function testRelativeModifyCharge()
61
    {
62
        $rel = new FixedDiscount($this->rate);
63
        $this->assertFixedDiscountCharges($rel, $this->value);
64
    }
65
66
    public function assertFixedDiscountCharges($fd, $sum)
67
    {
68
        $action = $this->createAction($this->prepaid->multiply(2));
69
        $charge = $action->calculateCharge($this->price);
70
        $charges = $fd->modifyCharge($charge, $action);
71
        $this->assertInternalType('array', $charges);
72
        $this->assertSame(2, count($charges));
73
        $this->assertSame($charge, $charges[0]);
74
        $discount = $charges[1];
75
        $this->assertInstanceOf(Charge::class, $discount);
76
        $this->assertEquals(Quantity::items(1), $discount->getUsage());
77
        $this->assertEquals($sum, $discount->getSum());
78
    }
79
}
80