|
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
|
|
|
public function setUp() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->engine = new FormulaEngine(); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
View Code Duplication |
public function testSimpleDiscount() |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
$date = '2018-08-01'; |
|
34
|
|
|
$rate = '2'; |
|
35
|
|
|
$reason = 'test reason'; |
|
36
|
|
|
$formula = $this->engine->build("discount.fixed('$rate%').since('$date').reason('$reason')"); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertInstanceOf(FixedDiscount::class, $formula); |
|
39
|
|
|
$this->assertSame($rate, $formula->getValue()->getValue()); |
|
40
|
|
|
$this->assertTrue($formula->isRelative()); |
|
41
|
|
|
$this->assertInstanceOf(Since::class, $formula->getSince()); |
|
42
|
|
|
$this->assertEquals(new DateTimeImmutable($date), $formula->getSince()->getValue()); |
|
43
|
|
|
$this->assertInstanceOf(Reason::class, $formula->getReason()); |
|
44
|
|
|
$this->assertSame($reason, $formula->getReason()->getValue()); |
|
45
|
|
|
$this->assertNull($formula->getTill()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public function testSimpleLeasing() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$date = '2018-08-01'; |
|
51
|
|
|
$num = 2; |
|
52
|
|
|
$reason = 'test reason'; |
|
53
|
|
|
$formula = $this->engine->build("leasing.since('$date').lasts('$num months').reason('$reason')"); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertInstanceOf(Leasing::class, $formula); |
|
56
|
|
|
$this->assertInstanceOf(MonthPeriod::class, $formula->getTerm()); |
|
57
|
|
|
$this->assertSame($num, $formula->getTerm()->getValue()); |
|
58
|
|
|
$this->assertInstanceOf(Since::class, $formula->getSince()); |
|
59
|
|
|
$this->assertEquals(new DateTimeImmutable($date), $formula->getSince()->getValue()); |
|
60
|
|
|
$this->assertInstanceOf(Reason::class, $formula->getReason()); |
|
61
|
|
|
$this->assertSame($reason, $formula->getReason()->getValue()); |
|
62
|
|
|
$this->assertNull($formula->getTill()); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
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: