3 namespace Tests\Wallabag\CoreBundle\Helper
;
5 use Monolog\Handler\TestHandler
;
7 use PHPUnit\Framework\TestCase
;
8 use Wallabag\CoreBundle\Entity\Config
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule
;
11 use Wallabag\CoreBundle\Entity\IgnoreOriginUserRule
;
12 use Wallabag\CoreBundle\Helper\RuleBasedIgnoreOriginProcessor
;
13 use Wallabag\UserBundle\Entity\User
;
15 class RuleBasedIgnoreOriginProcessorTest
extends TestCase
19 private $ignoreOriginInstanceRuleRepository;
23 public function setUp()
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
);
31 $this->processor
= new RuleBasedIgnoreOriginProcessor($this->rulerz
, $this->logger
, $this->ignoreOriginInstanceRuleRepository
);
34 public function testProcessWithNoRule()
36 $user = $this->getUser();
37 $entry = new Entry($user);
38 $entry->setUrl('http://example.com/hello-world');
40 $this->ignoreOriginInstanceRuleRepository
41 ->expects($this->once())
46 ->expects($this->never())
47 ->method('satisfies');
49 $result = $this->processor
->process($entry);
51 $this->assertFalse($result);
54 public function testProcessWithNoMatchingRule()
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');
61 $this->ignoreOriginInstanceRuleRepository
62 ->expects($this->once())
67 ->expects($this->once())
71 $result = $this->processor
->process($entry);
73 $this->assertFalse($result);
76 public function testProcessWithAMatchingRule()
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');
83 $this->ignoreOriginInstanceRuleRepository
84 ->expects($this->once())
89 ->expects($this->once())
93 $result = $this->processor
->process($entry);
95 $this->assertTrue($result);
98 public function testProcessWithAMixOfMatchingRules()
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');
106 $this->ignoreOriginInstanceRuleRepository
107 ->expects($this->once())
112 ->method('satisfies')
113 ->will($this->onConsecutiveCalls(false, true));
115 $result = $this->processor
->process($entry);
117 $this->assertTrue($result);
120 public function testProcessWithInstanceRules()
122 $user = $this->getUser();
123 $entry = new Entry($user);
124 $entry->setUrl('http://example.com/hello-world');
126 $instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
127 $this->ignoreOriginInstanceRuleRepository
128 ->expects($this->once())
130 ->willReturn([$instanceRule]);
133 ->expects($this->once())
134 ->method('satisfies')
137 $result = $this->processor
->process($entry);
139 $this->assertTrue($result);
142 public function testProcessWithMixedRules()
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');
149 $instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
150 $this->ignoreOriginInstanceRuleRepository
151 ->expects($this->once())
153 ->willReturn([$instanceRule]);
156 ->method('satisfies')
157 ->will($this->onConsecutiveCalls(false, true));
159 $result = $this->processor
->process($entry);
161 $this->assertTrue($result);
164 private function getUser(array $ignoreOriginRules = [])
167 $config = new Config($user);
169 $user->setConfig($config);
171 foreach ($ignoreOriginRules as $rule) {
172 $config->addIgnoreOriginRule($rule);
178 private function getIgnoreOriginUserRule($rule)
180 $ignoreOriginUserRule = new IgnoreOriginUserRule();
181 $ignoreOriginUserRule->setRule($rule);
183 return $ignoreOriginUserRule;
186 private function getIgnoreOriginInstanceRule($rule)
188 $ignoreOriginInstanceRule = new IgnoreOriginInstanceRule();
189 $ignoreOriginInstanceRule->setRule($rule);
191 return $ignoreOriginInstanceRule;
194 private function getRulerZMock()
196 return $this->getMockBuilder('RulerZ\RulerZ')
197 ->disableOriginalConstructor()
201 private function getIgnoreOriginInstanceRuleRepositoryMock()
203 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository')
204 ->disableOriginalConstructor()
208 private function getLogger()
210 return new Logger('foo');