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
|
|
|
public function testCreateAbsolute() |
32
|
|
|
{ |
33
|
|
|
$abs = new FixedDiscount($this->value); |
34
|
|
|
$this->assertSame($this->value, $abs->getValue()); |
35
|
|
|
$this->assertTrue($abs->isAbsolute()); |
36
|
|
|
$this->assertFalse($abs->isRelative()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testCreateRelative() |
40
|
|
|
{ |
41
|
|
|
$rel = new FixedDiscount($this->rate); |
42
|
|
|
$this->assertRelative($this->rate, $rel); |
43
|
|
|
$rel = new FixedDiscount($this->rate . '%'); |
44
|
|
|
$this->assertRelative($this->rate, $rel); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function assertRelative($rate, $rel) |
48
|
|
|
{ |
49
|
|
|
$this->assertSame($rate, $rel->getValue()); |
50
|
|
|
$this->assertTrue($rel->isRelative()); |
51
|
|
|
$this->assertFalse($rel->isAbsolute()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testAbsoluteModifyCharge() |
55
|
|
|
{ |
56
|
|
|
$abs = new FixedDiscount($this->value); |
57
|
|
|
$this->assertFixedDiscountCharges($abs, $this->value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testRelativeModifyCharge() |
61
|
|
|
{ |
62
|
|
|
$rel = new FixedDiscount($this->rate); |
63
|
|
|
$this->assertFixedDiscountCharges($rel, $this->value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function assertFixedDiscountCharges($fd, $sum) |
67
|
|
|
{ |
68
|
|
|
$action = $this->createAction($this->prepaid->multiply(2)); |
69
|
|
|
$charge = $action->calculateCharge($this->price); |
70
|
|
|
$charges = $fd->modifyCharge($charge, $action); |
71
|
|
|
$this->assertInternalType('array', $charges); |
72
|
|
|
$this->assertSame(2, count($charges)); |
73
|
|
|
$this->assertSame($charge, $charges[0]); |
74
|
|
|
$discount = $charges[1]; |
75
|
|
|
$this->assertInstanceOf(Charge::class, $discount); |
76
|
|
|
$this->assertEquals(Quantity::items(1), $discount->getUsage()); |
77
|
|
|
$this->assertEquals($sum, $discount->getSum()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|