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\formula; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\MonthPeriod; |
15
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Reason; |
16
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Since; |
17
|
|
|
use hiqdev\php\billing\charge\modifiers\FixedDiscount; |
18
|
|
|
use hiqdev\php\billing\charge\modifiers\Leasing; |
19
|
|
|
use hiqdev\php\billing\formula\FormulaEngine; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Andrii Vasyliev <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class FormulaEngineTest extends \PHPUnit\Framework\TestCase |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var FormulaEngine |
28
|
|
|
*/ |
29
|
|
|
protected $engine; |
30
|
|
|
|
31
|
|
|
public function setUp() |
32
|
|
|
{ |
33
|
|
|
$this->engine = new FormulaEngine(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testSimpleDiscount() |
37
|
|
|
{ |
38
|
|
|
$date = '2018-08-01'; |
39
|
|
|
$rate = '2'; |
40
|
|
|
$reason = 'test reason'; |
41
|
|
|
$formula = $this->engine->build("discount.fixed('$rate%').since('$date').reason('$reason')"); |
42
|
|
|
|
43
|
|
|
$this->assertInstanceOf(FixedDiscount::class, $formula); |
44
|
|
|
$this->assertSame($rate, $formula->getValue()->getValue()); |
45
|
|
|
$this->assertTrue($formula->isRelative()); |
46
|
|
|
$this->assertInstanceOf(Since::class, $formula->getSince()); |
47
|
|
|
$this->assertEquals(new DateTimeImmutable($date), $formula->getSince()->getValue()); |
48
|
|
|
$this->assertInstanceOf(Reason::class, $formula->getReason()); |
49
|
|
|
$this->assertSame($reason, $formula->getReason()->getValue()); |
50
|
|
|
$this->assertNull($formula->getTill()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testSimpleLeasing() |
54
|
|
|
{ |
55
|
|
|
$this->checkSimpleLeasing('2018-08-01', 2, 'test reason'); |
56
|
|
|
$this->checkSimpleLeasing('2018-09-01', 3, 'test reason'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function checkSimpleLeasing($date, $num, $reason) |
60
|
|
|
{ |
61
|
|
|
$formula = $this->engine->build("leasing.since('$date').lasts('$num months').reason('$reason')"); |
62
|
|
|
|
63
|
|
|
$this->assertInstanceOf(Leasing::class, $formula); |
64
|
|
|
$this->assertInstanceOf(MonthPeriod::class, $formula->getTerm()); |
65
|
|
|
$this->assertSame($num, $formula->getTerm()->getValue()); |
66
|
|
|
$this->assertInstanceOf(Since::class, $formula->getSince()); |
67
|
|
|
$this->assertEquals(new DateTimeImmutable($date), $formula->getSince()->getValue()); |
68
|
|
|
$this->assertInstanceOf(Reason::class, $formula->getReason()); |
69
|
|
|
$this->assertSame($reason, $formula->getReason()->getValue()); |
70
|
|
|
$this->assertNull($formula->getTill()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function normalizeDataProvider() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
|
|
["ab\ncd", "ab\ncd"], |
77
|
|
|
[" ab \n \n cd", "ab\ncd"], |
78
|
|
|
['', null], |
79
|
|
|
[' ', null], |
80
|
|
|
["\n\n\n", null], |
81
|
|
|
['ab', 'ab'], |
82
|
|
|
["ab\ncd", "ab\ncd"], |
83
|
|
|
[true, '1'], |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @dataProvider normalizeDataProvider |
89
|
|
|
*/ |
90
|
|
|
public function testNormalize($formula, $expected) |
91
|
|
|
{ |
92
|
|
|
return $this->assertSame($expected, $this->engine->normalize($formula)); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @dataProvider validateDataProvider |
97
|
|
|
*/ |
98
|
|
|
public function testValidate($formula, $error) |
99
|
|
|
{ |
100
|
|
|
return $this->assertSame($error, $this->engine->validate($formula)); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function validateDataProvider() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
['', "Unexpected token \"EOF\" (EOF) at line 1 and column 1:\n\nā: "], |
107
|
|
|
['true', 'Formula run returned unexpected result: true'], |
108
|
|
|
['discount.fixed("50%")', null], |
109
|
|
|
["discount.fixed(\"50%\")\ndiscount.fixed(\"5 USD\")", null], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.