aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag')
-rw-r--r--tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php212
1 files changed, 212 insertions, 0 deletions
diff --git a/tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php b/tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php
new file mode 100644
index 00000000..9e39bc81
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php
@@ -0,0 +1,212 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Helper;
4
5use Monolog\Handler\TestHandler;
6use Monolog\Logger;
7use PHPUnit\Framework\TestCase;
8use Wallabag\CoreBundle\Entity\Config;
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;
11use Wallabag\CoreBundle\Entity\IgnoreOriginUserRule;
12use Wallabag\CoreBundle\Helper\RuleBasedIgnoreOriginProcessor;
13use Wallabag\UserBundle\Entity\User;
14
15class RuleBasedIgnoreOriginProcessorTest extends TestCase
16{
17 private $rulerz;
18 private $processor;
19 private $ignoreOriginInstanceRuleRepository;
20 private $logger;
21 private $handler;
22
23 public function setUp()
24 {
25 $this->rulerz = $this->getRulerZMock();
26 $this->logger = $this->getLogger();
27 $this->ignoreOriginInstanceRuleRepository = $this->getIgnoreOriginInstanceRuleRepositoryMock();
28 $this->handler = new TestHandler();
29 $this->logger->pushHandler($this->handler);
30
31 $this->processor = new RuleBasedIgnoreOriginProcessor($this->rulerz, $this->logger, $this->ignoreOriginInstanceRuleRepository);
32 }
33
34 public function testProcessWithNoRule()
35 {
36 $user = $this->getUser();
37 $entry = new Entry($user);
38 $entry->setUrl('http://example.com/hello-world');
39
40 $this->ignoreOriginInstanceRuleRepository
41 ->expects($this->once())
42 ->method('findAll')
43 ->willReturn([]);
44
45 $this->rulerz
46 ->expects($this->never())
47 ->method('satisfies');
48
49 $result = $this->processor->process($entry);
50
51 $this->assertFalse($result);
52 }
53
54 public function testProcessWithNoMatchingRule()
55 {
56 $userRule = $this->getIgnoreOriginUserRule('rule as string');
57 $user = $this->getUser([$userRule]);
58 $entry = new Entry($user);
59 $entry->setUrl('http://example.com/hello-world');
60
61 $this->ignoreOriginInstanceRuleRepository
62 ->expects($this->once())
63 ->method('findAll')
64 ->willReturn([]);
65
66 $this->rulerz
67 ->expects($this->once())
68 ->method('satisfies')
69 ->willReturn(false);
70
71 $result = $this->processor->process($entry);
72
73 $this->assertFalse($result);
74 }
75
76 public function testProcessWithAMatchingRule()
77 {
78 $userRule = $this->getIgnoreOriginUserRule('rule as string');
79 $user = $this->getUser([$userRule]);
80 $entry = new Entry($user);
81 $entry->setUrl('http://example.com/hello-world');
82
83 $this->ignoreOriginInstanceRuleRepository
84 ->expects($this->once())
85 ->method('findAll')
86 ->willReturn([]);
87
88 $this->rulerz
89 ->expects($this->once())
90 ->method('satisfies')
91 ->willReturn(true);
92
93 $result = $this->processor->process($entry);
94
95 $this->assertTrue($result);
96 }
97
98 public function testProcessWithAMixOfMatchingRules()
99 {
100 $userRule = $this->getIgnoreOriginUserRule('rule as string');
101 $anotherUserRule = $this->getIgnoreOriginUserRule('another rule as string');
102 $user = $this->getUser([$userRule, $anotherUserRule]);
103 $entry = new Entry($user);
104 $entry->setUrl('http://example.com/hello-world');
105
106 $this->ignoreOriginInstanceRuleRepository
107 ->expects($this->once())
108 ->method('findAll')
109 ->willReturn([]);
110
111 $this->rulerz
112 ->method('satisfies')
113 ->will($this->onConsecutiveCalls(false, true));
114
115 $result = $this->processor->process($entry);
116
117 $this->assertTrue($result);
118 }
119
120 public function testProcessWithInstanceRules()
121 {
122 $user = $this->getUser();
123 $entry = new Entry($user);
124 $entry->setUrl('http://example.com/hello-world');
125
126 $instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
127 $this->ignoreOriginInstanceRuleRepository
128 ->expects($this->once())
129 ->method('findAll')
130 ->willReturn([$instanceRule]);
131
132 $this->rulerz
133 ->expects($this->once())
134 ->method('satisfies')
135 ->willReturn(true);
136
137 $result = $this->processor->process($entry);
138
139 $this->assertTrue($result);
140 }
141
142 public function testProcessWithMixedRules()
143 {
144 $userRule = $this->getIgnoreOriginUserRule('rule as string');
145 $user = $this->getUser([$userRule]);
146 $entry = new Entry($user);
147 $entry->setUrl('http://example.com/hello-world');
148
149 $instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
150 $this->ignoreOriginInstanceRuleRepository
151 ->expects($this->once())
152 ->method('findAll')
153 ->willReturn([$instanceRule]);
154
155 $this->rulerz
156 ->method('satisfies')
157 ->will($this->onConsecutiveCalls(false, true));
158
159 $result = $this->processor->process($entry);
160
161 $this->assertTrue($result);
162 }
163
164 private function getUser(array $ignoreOriginRules = [])
165 {
166 $user = new User();
167 $config = new Config($user);
168
169 $user->setConfig($config);
170
171 foreach ($ignoreOriginRules as $rule) {
172 $config->addIgnoreOriginRule($rule);
173 }
174
175 return $user;
176 }
177
178 private function getIgnoreOriginUserRule($rule)
179 {
180 $ignoreOriginUserRule = new IgnoreOriginUserRule();
181 $ignoreOriginUserRule->setRule($rule);
182
183 return $ignoreOriginUserRule;
184 }
185
186 private function getIgnoreOriginInstanceRule($rule)
187 {
188 $ignoreOriginInstanceRule = new IgnoreOriginInstanceRule();
189 $ignoreOriginInstanceRule->setRule($rule);
190
191 return $ignoreOriginInstanceRule;
192 }
193
194 private function getRulerZMock()
195 {
196 return $this->getMockBuilder('RulerZ\RulerZ')
197 ->disableOriginalConstructor()
198 ->getMock();
199 }
200
201 private function getIgnoreOriginInstanceRuleRepositoryMock()
202 {
203 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository')
204 ->disableOriginalConstructor()
205 ->getMock();
206 }
207
208 private function getLogger()
209 {
210 return new Logger('foo');
211 }
212}