|
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
|
|
|
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()); |
|
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()); |
|
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
|
|
View Code Duplication |
public function assertCharges($fd, $sum) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$action = $this->createAction($this->prepaid->multiply(2)); |
|
|
|
|
|
|
83
|
|
|
$charge = $action->calculateCharge($this->price); |
|
84
|
|
|
$charges = $fd->modifyCharge($charge, $action); |
|
85
|
|
|
$this->assertInternalType('array', $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(1), $discount->getUsage()); |
|
91
|
|
|
$this->assertEquals($sum, $discount->getSum()); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: