Completed
Push — master ( 4e5ef4...37558b )
by De Cramer
02:05
created

ConditionTest::constraintsAndResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 2
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\RuleEngine\Tests\Rules;
4
5
use Oliverde8\Component\RuleEngine\Exceptions\Condition\UnSupportedOperationException;
6
use Oliverde8\Component\RuleEngine\Exceptions\RuleOptionMissingException;
7
use Oliverde8\Component\RuleEngine\Rules\Condition;
8
use Psr\Log\NullLogger;
9
10
/**
11
 * Class ConditionTest
12
 *
13
 * @author    de Cramer Oliver<[email protected]>
14
 * @copyright 2018 Smile
15
 * @package Oliverde8\Component\RuleEngine\Tests\Rules
16
 */
17
class ConditionTest extends AbstractRule
18
{
19
    /** @var \PHPUnit_Framework_MockObject_MockObject */
20
    protected $mockRuleApplier;
21
22
    protected function setUp()
23
    {
24
        $this->mockRuleApplier = new TestRuleApplier(new NullLogger(), [], false);
25
26
        parent::setUp();
27
    }
28
29
    /**
30
     * Test that the expected results is returned in all cases.
31
     *
32
     * @dataProvider constraintsAndResults
33
     */
34
    public function testConstraints($options, $expected = '')
35
    {
36
        $this->rule->setApplier($this->mockRuleApplier);
37
38
        $options['then'] = 'true';
39
        $options['else'] = 'false';
40
41
        $data = [];
42
        $this->assertRuleResults([], $data, $options, $expected);
43
    }
44
45
    /**
46
     * Test that proper exception is thrown when operation is invalid.
47
     */
48
    public function testInvalidOperation()
49
    {
50
        $this->expectException(UnSupportedOperationException::class);
51
52
        $data = [];
53
        $this->rule->apply([], $data, ['if' => 1, 'value' => 1, 'operation' => 'toto']);
54
    }
55
56
    /**
57
     * Test that the rules code is the one expected.
58
     */
59
    public function testRuleCode()
60
    {
61
        $this->assertEquals('condition', $this->rule->getRuleCode());
62
    }
63
64
    /**
65
     * Test that if options are missing proper exception is thrown.
66
     */
67
    public function testMissingOption()
68
    {
69
        $this->expectException(RuleOptionMissingException::class);
70
        $this->rule->validate(['test']);
71
    }
72
73
    /**
74
     * List of text cases.
75
     *
76
     * @return array
77
     */
78
    public function constraintsAndResults()
79
    {
80
        return [
81
            [['if' => 1, 'value' => 2,      'operation' => 'eq', ], 'false'],
82
            [['if' => 1, 'value' => 1,      'operation' => 'eq', ], 'true'],
83
            [['if' => 1, 'value' => 1,      'operation' => 'neq',], 'false'],
84
            [['if' => 1, 'value' => [1, 2], 'operation' => 'in', ], 'true'],
85
            [['if' => 3, 'value' => [1, 2], 'operation' => 'in', ], 'false'],
86
        ];
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    function getRule()
93
    {
94
        return new Condition(new NullLogger());
95
    }
96
}