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\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(): void |
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
|
|
|
*/ |
57
|
|
|
public function testMultiplyFailed($multiplier) |
58
|
|
|
{ |
59
|
|
|
$this->expectException(\Exception::class); |
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
|
|
|
|
74
|
|
|
return [ |
75
|
|
|
[$this->relative, 'aasd'], |
76
|
|
|
[$this->relative, '10a'], |
77
|
|
|
[$this->relative, Money::USD(12)], |
78
|
|
|
[$this->absolute, 'aasd'], |
79
|
|
|
[$this->absolute, '10b'], |
80
|
|
|
[$this->absolute, 10], |
81
|
|
|
]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @dataProvider badAddends |
86
|
|
|
*/ |
87
|
|
|
public function testAddFailed($discount, $addend) |
88
|
|
|
{ |
89
|
|
|
$this->expectException(\Exception::class); |
90
|
|
|
$discount->add($addend); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testCompare() |
94
|
|
|
{ |
95
|
|
|
$money = Money::USD(1); |
96
|
|
|
$this->assertTrue($this->absolute->compare($money) > 0); |
97
|
|
|
$this->assertTrue($this->relative->compare(1) > 0); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|