Completed
Push — master ( df1a77...53322f )
by Dmitry
03:46
created

FormulaEngineTest::testSimpleLeasing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
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 View Code Duplication
    public function testSimpleDiscount()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function testSimpleLeasing()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
    {
55
        $date = '2018-08-01';
56
        $num = 2;
57
        $reason = 'test reason';
58
        $formula = $this->engine->build("leasing.since('$date').lasts('$num months').reason('$reason')");
59
60
        $this->assertInstanceOf(Leasing::class, $formula);
61
        $this->assertInstanceOf(MonthPeriod::class, $formula->getTerm());
62
        $this->assertSame($num, $formula->getTerm()->getValue());
63
        $this->assertInstanceOf(Since::class, $formula->getSince());
64
        $this->assertEquals(new DateTimeImmutable($date), $formula->getSince()->getValue());
65
        $this->assertInstanceOf(Reason::class, $formula->getReason());
66
        $this->assertSame($reason, $formula->getReason()->getValue());
67
        $this->assertNull($formula->getTill());
68
    }
69
70
    public function normalizeDataProvider()
71
    {
72
        return [
73
            ["ab\ncd", "ab AND cd"],
74
            [" ab  \n  \n cd", "ab AND cd"],
75
            ["\n\n\n", ''],
76
            ["", ''],
77
            [" ", ''],
78
            ['ab', 'ab'],
79
            ['ab and cd', 'ab and cd'],
80
            [true, '1'],
81
        ];
82
    }
83
84
    /**
85
     * @dataProvider normalizeDataProvider
86
     */
87
    public function testNormalize($formula, $expected)
88
    {
89
        return $this->assertSame($expected, $this->engine->normalize($formula));
90
    }
91
92
    /**
93
     * @dataProvider validateDataProvider
94
     */
95
    public function testValidate($formula, $error)
96
    {
97
        return $this->assertSame($error, $this->engine->validate($formula));
98
    }
99
100
    public function validateDataProvider()
101
    {
102
        return [
103
            ['', "Unexpected token \"EOF\" (EOF) at line 1 and column 1:\n\n↑"],
104
            ['true', "Formula run returned unexpected result"],
105
            ['discount.fixed("50%")', null],
106
            ["discount.fixed(\"50%\")\ndiscount.fixed(\"5 USD\")", null],
107
        ];
108
    }
109
}
110