Passed
Push ā€” master ( ab853b...cc4319 )
by Dmitry
29:31 queued 12:17
created

tests/unit/formula/FormulaEngineTest.php (2 issues)

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-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\tests\unit\formula;
12
13
use Cache\Adapter\PHPArray\ArrayCachePool;
14
use DateTimeImmutable;
15
use hiqdev\php\billing\charge\modifiers\addons\MonthPeriod;
16
use hiqdev\php\billing\charge\modifiers\addons\Reason;
17
use hiqdev\php\billing\charge\modifiers\addons\Since;
18
use hiqdev\php\billing\charge\modifiers\FixedDiscount;
19
use hiqdev\php\billing\charge\modifiers\Leasing;
20
use hiqdev\php\billing\formula\FormulaEngine;
21
use PHPUnit\Framework\TestCase;
22
23
/**
24
 * @author Andrii Vasyliev <[email protected]>
25
 */
26
class FormulaEngineTest extends TestCase
27
{
28
    /**
29
     * @var FormulaEngine
30
     */
31
    protected $engine;
32
33
    public function setUp(): void
34
    {
35
        $this->engine = new FormulaEngine(new ArrayCachePool());
36
    }
37
38
    public function testSimpleDiscount()
39
    {
40
        $date = '2018-08-01';
41
        $rate = '2';
42
        $reason = 'test reason';
43
        $formula = $this->engine->build("discount.fixed('$rate%').since('$date').reason('$reason')");
44
45
        $this->assertInstanceOf(FixedDiscount::class, $formula);
46
        $this->assertSame($rate, $formula->getValue()->getValue());
47
        $this->assertTrue($formula->isRelative());
48
        $this->assertInstanceOf(Since::class, $formula->getSince());
49
        $this->assertEquals(new DateTimeImmutable($date), $formula->getSince()->getValue());
50
        $this->assertInstanceOf(Reason::class, $formula->getReason());
51
        $this->assertSame($reason, $formula->getReason()->getValue());
52
        $this->assertNull($formula->getTill());
53
    }
54
55
    public function testSimpleLeasing()
56
    {
57
        $this->checkSimpleLeasing('2018-08-01', 2, 'test reason');
58
        $this->checkSimpleLeasing('2018-09-01', 3, 'test reason');
59
    }
60
61
    protected function checkSimpleLeasing($date, $num, $reason)
62
    {
63
        $formula = $this->engine->build("leasing.since('$date').lasts('$num months').reason('$reason')");
64
65
        $this->assertInstanceOf(Leasing::class, $formula);
66
        $this->assertInstanceOf(MonthPeriod::class, $formula->getTerm());
67
        $this->assertSame($num, $formula->getTerm()->getValue());
68
        $this->assertInstanceOf(Since::class, $formula->getSince());
69
        $this->assertEquals(new DateTimeImmutable($date), $formula->getSince()->getValue());
70
        $this->assertInstanceOf(Reason::class, $formula->getReason());
71
        $this->assertSame($reason, $formula->getReason()->getValue());
72
        $this->assertNull($formula->getTill());
73
    }
74
75
    public function normalizeDataProvider()
76
    {
77
        return [
78
            ["ab\ncd", "ab\ncd"],
79
            [" ab  \n  \n cd", "ab\ncd"],
80
            ['', null],
81
            [' ', null],
82
            ["\n\n\n", null],
83
            ['ab', 'ab'],
84
            ["ab\ncd", "ab\ncd"],
85
            [true, '1'],
86
        ];
87
    }
88
89
    /**
90
     * @dataProvider normalizeDataProvider
91
     */
92
    public function testNormalize($formula, $expected)
93
    {
94
        return $this->assertSame($expected, $this->engine->normalize($formula));
0 ignored issues
show
Are you sure the usage of $this->assertSame($expec...e->normalize($formula)) targeting PHPUnit\Framework\Assert::assertSame() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
95
    }
96
97
    /**
98
     * @dataProvider validateDataProvider
99
     */
100
    public function testValidate($formula, $error)
101
    {
102
        return $this->assertSame($error, $this->engine->validate($formula));
0 ignored issues
show
Are you sure the usage of $this->assertSame($error...ne->validate($formula)) targeting PHPUnit\Framework\Assert::assertSame() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

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.

Loading history...
103
    }
104
105
    public function validateDataProvider()
106
    {
107
        return [
108
            ['', "Unexpected token \"EOF\" (EOF) at line 1 and column 1:\n\nā†‘ : "],
109
            //['', 'Failed to interpret formula : '],
110
            ['true', 'Formula run returned unexpected result : true'],
111
            ['discount.fixed("50%")', null],
112
            ["discount.fixed(\"50%\")\ndiscount.fixed(\"5 USD\")", null],
113
        ];
114
    }
115
}
116