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
|
|
|
private Money $value; |
25
|
|
|
|
26
|
|
|
private string $rate; |
27
|
|
|
|
28
|
|
|
protected function setUp(): void |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$this->value = Money::USD(1000); |
33
|
|
|
$this->rate = '10'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function buildDiscount($value) |
37
|
|
|
{ |
38
|
|
|
return new FixedDiscount($value); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCreateAbsolute() |
42
|
|
|
{ |
43
|
|
|
$abs = $this->buildDiscount($this->value); |
44
|
|
|
$this->assertAbsolute($this->value, $abs); |
45
|
|
|
$abs = $this->buildDiscount('10 USD'); |
46
|
|
|
$this->assertAbsolute($this->value, $abs); |
47
|
|
|
$abs = $this->buildDiscount('10.00 USD'); |
48
|
|
|
$this->assertAbsolute($this->value, $abs); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function assertAbsolute($value, $abs) |
52
|
|
|
{ |
53
|
|
|
$this->assertEquals($value, $abs->getValue()->getValue()); |
54
|
|
|
$this->assertTrue($abs->isAbsolute()); |
55
|
|
|
$this->assertFalse($abs->isRelative()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testCreateRelative() |
59
|
|
|
{ |
60
|
|
|
$rel = $this->buildDiscount($this->rate); |
61
|
|
|
$this->assertRelative($this->rate, $rel); |
62
|
|
|
$rel = $this->buildDiscount($this->rate . '%'); |
63
|
|
|
$this->assertRelative($this->rate, $rel); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function assertRelative($rate, $rel) |
67
|
|
|
{ |
68
|
|
|
$this->assertSame($rate, $rel->getValue()->getValue()); |
69
|
|
|
$this->assertTrue($rel->isRelative()); |
70
|
|
|
$this->assertFalse($rel->isAbsolute()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testAbsoluteModifyCharge() |
74
|
|
|
{ |
75
|
|
|
$abs = $this->buildDiscount($this->value); |
76
|
|
|
$this->assertCharges($abs, $this->value); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testRelativeModifyCharge() |
80
|
|
|
{ |
81
|
|
|
$rel = $this->buildDiscount($this->rate); |
82
|
|
|
$this->assertCharges($rel, $this->value); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function assertCharges($fd, $sum) |
86
|
|
|
{ |
87
|
|
|
$action = $this->createAction($this->prepaid->multiply(2)); |
88
|
|
|
$charge = $this->calculator->calculateCharge($this->price, $action); |
89
|
|
|
$charges = $fd->modifyCharge($charge, $action); |
90
|
|
|
$this->assertIsArray($charges); |
91
|
|
|
$this->assertSame(2, count($charges)); |
92
|
|
|
$this->assertSame($charge, $charges[0]); |
93
|
|
|
$discount = $charges[1]; |
94
|
|
|
$this->assertInstanceOf(Charge::class, $discount); |
95
|
|
|
$this->assertEquals(Quantity::items(0), $discount->getUsage()); |
96
|
|
|
$this->assertEquals($sum->multiply(-1), $discount->getSum()); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|