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\Currency; |
18
|
|
|
use Money\Money; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class FixedDiscountTest extends ActionTest |
24
|
|
|
{ |
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
$this->value = Money::USD(1000); |
|
|
|
|
29
|
|
|
$this->rate = '10'; |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testCreateAbsolute() |
33
|
|
|
{ |
34
|
|
|
$abs = new FixedDiscount($this->value); |
35
|
|
|
$this->assertAbsolute($this->value, $abs); |
36
|
|
|
$abs = new FixedDiscount('10 USD'); |
37
|
|
|
$this->assertAbsolute($this->value, $abs); |
38
|
|
|
$abs = new FixedDiscount('10.00 USD'); |
39
|
|
|
$this->assertAbsolute($this->value, $abs); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function assertAbsolute($value, $abs) |
43
|
|
|
{ |
44
|
|
|
$this->assertEquals($value, $abs->getValue()); |
45
|
|
|
$this->assertTrue($abs->isAbsolute()); |
46
|
|
|
$this->assertFalse($abs->isRelative()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testCreateRelative() |
50
|
|
|
{ |
51
|
|
|
$rel = new FixedDiscount($this->rate); |
52
|
|
|
$this->assertRelative($this->rate, $rel); |
53
|
|
|
$rel = new FixedDiscount($this->rate . '%'); |
54
|
|
|
$this->assertRelative($this->rate, $rel); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function assertRelative($rate, $rel) |
58
|
|
|
{ |
59
|
|
|
$this->assertSame($rate, $rel->getValue()); |
60
|
|
|
$this->assertTrue($rel->isRelative()); |
61
|
|
|
$this->assertFalse($rel->isAbsolute()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testAbsoluteModifyCharge() |
65
|
|
|
{ |
66
|
|
|
$abs = new FixedDiscount($this->value); |
67
|
|
|
$this->assertFixedDiscountCharges($abs, $this->value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testRelativeModifyCharge() |
71
|
|
|
{ |
72
|
|
|
$rel = new FixedDiscount($this->rate); |
73
|
|
|
$this->assertFixedDiscountCharges($rel, $this->value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function assertFixedDiscountCharges($fd, $sum) |
77
|
|
|
{ |
78
|
|
|
$action = $this->createAction($this->prepaid->multiply(2)); |
|
|
|
|
79
|
|
|
$charge = $action->calculateCharge($this->price); |
80
|
|
|
$charges = $fd->modifyCharge($charge, $action); |
81
|
|
|
$this->assertInternalType('array', $charges); |
82
|
|
|
$this->assertSame(2, count($charges)); |
83
|
|
|
$this->assertSame($charge, $charges[0]); |
84
|
|
|
$discount = $charges[1]; |
85
|
|
|
$this->assertInstanceOf(Charge::class, $discount); |
86
|
|
|
$this->assertEquals(Quantity::items(1), $discount->getUsage()); |
87
|
|
|
$this->assertEquals($sum, $discount->getSum()); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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: