Completed
Push — master ( bf0cfa...c734e4 )
by Andrii
02:35
created

DiscountTest::testEnsureValidValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\addons;
12
13
use hiqdev\php\billing\charge\modifiers\addons\Discount;
14
use Money\Money;
15
16
/**
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class DiscountTest extends \PHPUnit\Framework\TestCase
20
{
21
    protected $absolute;
22
    protected $relative;
23
    protected $rate = 11;
24
    protected $sum = 1234;
25
    protected $currency = 'USD';
26
27
    protected function setUp()
28
    {
29
        $this->absolute = new Discount($this->sum/100 . ' ' . $this->currency);
30
        $this->relative = new Discount($this->rate . '%');
31
    }
32
33
    public function testEnsureValidValue()
34
    {
35
        $money = Money::USD($this->sum);
36
        $this->assertEquals($money, $this->absolute->getValue());
37
        $this->assertEquals($this->rate, $this->relative->getValue());
38
    }
39
40
    public function testMultiply()
41
    {
42
        $money = Money::USD($this->sum*10);
43
        $this->assertEquals($money, $this->absolute->multiply(10)->getValue());
44
        $this->assertEquals($this->rate*10, $this->relative->multiply(10)->getValue());
45
    }
46
47
    public function badMultipliers()
48
    {
49
        return [
50
            ['aasd'], ['10%'], [Money::USD(12)],
51
        ];
52
    }
53
54
    /**
55
     * @dataProvider badMultipliers
56
     * @expectedException Exception
57
     */
58
    public function testMultiplyFailed($multiplier)
59
    {
60
        $this->absolute->multiply($multiplier);
61
    }
62
63
    public function testAdd()
64
    {
65
        $money = Money::USD($this->sum+10);
66
        $this->assertEquals($money, $this->absolute->add(Money::USD(10))->getValue());
67
        $this->assertEquals($this->rate+10, $this->relative->add(10)->getValue());
68
    }
69
70
    public function badAddends()
71
    {
72
        $this->setUp();
73
        return [
74
            [$this->relative, 'aasd'],
75
            [$this->relative, '10a'],
76
            [$this->relative, Money::USD(12)],
77
            [$this->absolute, 'aasd'],
78
            [$this->absolute, '10b'],
79
            [$this->absolute, 10],
80
        ];
81
    }
82
83
    /**
84
     * @dataProvider badAddends
85
     * @expectedException Exception
86
     */
87
    public function testAddFailed($discount, $addend)
88
    {
89
        $discount->add($addend);
90
    }
91
92
    public function testCompare()
93
    {
94
        $money = Money::USD(1);
95
        $this->assertTrue($this->absolute->compare($money) > 0);
96
        $this->assertTrue($this->relative->compare(1) > 0);
97
    }
98
99
}
100