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