Completed
Push — master ( 610136...82575c )
by De Cramer
06:15 queued 03:00
created

RuleApplierTest::testNoResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 19
ccs 15
cts 15
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\RuleEngine\Tests;
4
5
use Oliverde8\Component\RuleEngine\Exceptions\RuleException;
6
use Oliverde8\Component\RuleEngine\Exceptions\UnknownRuleException;
7
use Oliverde8\Component\RuleEngine\RuleApplier;
8
use Oliverde8\Component\RuleEngine\Rules\RuleInterface;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Log\LoggerInterface;
11
12
/**
13
 * Class RuleApplierTest
14
 *
15
 * @author    de Cramer Oliver<[email protected]>
16
 * @copyright 2018 Smile
17
 * @package Smile\Components\RuleEngine\Tests
18
 */
19
class RuleApplierTest extends TestCase
20
{
21
    /** @var \PHPUnit_Framework_MockObject_MockObject */
22
    protected $mockRule1;
23
24
    /** @var \PHPUnit_Framework_MockObject_MockObject */
25
    protected $mockRule2;
26
27
    /** @var \PHPUnit_Framework_MockObject_MockObject */
28
    protected $mockLogger;
29
30
    /** @var RuleApplier */
31
    protected $ruleApplier;
32
33
    /**
34
     * @inheritdoc
35
     */
36 7
    protected function setUp()
37
    {
38 7
        parent::setUp();
39
40 7
        $this->mockRule1 = $this->getMockBuilder(RuleInterface::class)->getMock();
41 7
        $this->mockRule1->method('getRuleCode')->willReturn('rule1');
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->mockRule1->/** @scrutinizer ignore-call */ 
42
                          method('getRuleCode')->willReturn('rule1');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43 7
        $this->mockRule2 = $this->getMockBuilder(RuleInterface::class)->getMock();
44 7
        $this->mockRule2->method('getRuleCode')->willReturn('rule2');
45
46 7
        $this->mockLogger = $this->getMockBuilder(LoggerInterface::class)->getMock();
47
48 7
        $this->ruleApplier = new RuleApplier($this->mockLogger, [], true);
49 7
        $this->ruleApplier->registerRule($this->mockRule1);
50 7
        $this->ruleApplier->registerRule($this->mockRule2);
51 7
    }
52
53 1
    public function testApplyingSimpleRule()
54
    {
55 1
        $this->mockRule1
56 1
            ->expects($this->once())
57 1
            ->method('apply')
58 1
            ->with([], [], ['test'])
59 1
            ->willReturn('my value');
60
61 1
        $this->assertEquals(
62 1
            'my value',
63 1
            $this->ruleApplier->apply([], [], [['rule1' => ['test']]], [], ['id' => 'test'])
64
        );
65 1
    }
66
67 1
    public function testApplyConstantValue()
68
    {
69 1
        $this->assertEquals(
70 1
            'my value',
71 1
            $this->ruleApplier->apply([], [], 'my value', [], ['id' => 'test'])
72
        );
73 1
    }
74
75 1
    public function testApplyConsecutiveRules()
76
    {
77 1
        $this->mockRule1
78 1
            ->expects($this->once())
79 1
            ->method('apply')
80 1
            ->with([], [], ['test'])
81 1
            ->willReturn(null);
82
83 1
        $this->mockRule2
84 1
            ->expects($this->once())
85 1
            ->method('apply')
86 1
            ->with([], [], ['test'])
87 1
            ->willReturn('my value');
88
89 1
        $this->assertEquals(
90 1
            'my value',
91 1
            $this->ruleApplier->apply([], [], [['rule1' => ['test']], ['rule2' => ['test']]], [], ['id' => 'test'])
92
        );
93 1
    }
94
95 1
    public function testApplyConsecutiveRulesWithConstant()
96
    {
97 1
        $this->mockRule1
98 1
            ->expects($this->once())
99 1
            ->method('apply')
100 1
            ->willReturn(null);
101
102 1
        $this->assertEquals(
103 1
            'my value',
104 1
            $this->ruleApplier->apply([], [], [['rule1' => ['test']], 'my value'], [], ['id' => 'test'])
105
        );
106 1
    }
107
108 1
    public function testNoResults()
109
    {
110 1
        $this->mockRule1
111 1
            ->expects($this->once())
112 1
            ->method('apply')
113 1
            ->with([], [], ['test'])
114 1
            ->willReturn(null);
115
116 1
        $this->mockRule2
117 1
            ->expects($this->once())
118 1
            ->method('apply')
119 1
            ->with([], [], ['test'])
120 1
            ->willReturn(null);
121
122 1
        $this->mockLogger->expects($this->once())->method('warning');
123
124 1
        $this->assertEquals(
125 1
            '',
126 1
            $this->ruleApplier->apply([], [], [['rule1' => ['test']], ['rule2' => ['test']]], [], ['id' => 'test'])
127
        );
128 1
    }
129
130 1
    public function testUnknownRule()
131
    {
132 1
        $this->expectException(UnknownRuleException::class);
133
134 1
        $this->ruleApplier->apply([], [], [['rule3' => ['test']]], [], ['id' => 'test']);
135
    }
136
137 1
    public function testValidationError()
138
    {
139 1
        $this->mockRule1->method('validate')->willThrowException(new RuleException());
140 1
        $this->expectException(RuleException::class);
141 1
        $this->mockLogger->expects($this->once())->method('error');
142
143 1
        $this->ruleApplier->apply([], [], [['rule1' => ['test']]], [], ['id' => 'test']);
144
    }
145
}