aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php
blob: 9e39bc8110b103f2e4ce31be6d162b2eb7ddfd14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php

namespace Tests\Wallabag\CoreBundle\Helper;

use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;
use Wallabag\CoreBundle\Entity\IgnoreOriginUserRule;
use Wallabag\CoreBundle\Helper\RuleBasedIgnoreOriginProcessor;
use Wallabag\UserBundle\Entity\User;

class RuleBasedIgnoreOriginProcessorTest extends TestCase
{
    private $rulerz;
    private $processor;
    private $ignoreOriginInstanceRuleRepository;
    private $logger;
    private $handler;

    public function setUp()
    {
        $this->rulerz = $this->getRulerZMock();
        $this->logger = $this->getLogger();
        $this->ignoreOriginInstanceRuleRepository = $this->getIgnoreOriginInstanceRuleRepositoryMock();
        $this->handler = new TestHandler();
        $this->logger->pushHandler($this->handler);

        $this->processor = new RuleBasedIgnoreOriginProcessor($this->rulerz, $this->logger, $this->ignoreOriginInstanceRuleRepository);
    }

    public function testProcessWithNoRule()
    {
        $user = $this->getUser();
        $entry = new Entry($user);
        $entry->setUrl('http://example.com/hello-world');

        $this->ignoreOriginInstanceRuleRepository
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([]);

        $this->rulerz
            ->expects($this->never())
            ->method('satisfies');

        $result = $this->processor->process($entry);

        $this->assertFalse($result);
    }

    public function testProcessWithNoMatchingRule()
    {
        $userRule = $this->getIgnoreOriginUserRule('rule as string');
        $user = $this->getUser([$userRule]);
        $entry = new Entry($user);
        $entry->setUrl('http://example.com/hello-world');

        $this->ignoreOriginInstanceRuleRepository
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([]);

        $this->rulerz
            ->expects($this->once())
            ->method('satisfies')
            ->willReturn(false);

        $result = $this->processor->process($entry);

        $this->assertFalse($result);
    }

    public function testProcessWithAMatchingRule()
    {
        $userRule = $this->getIgnoreOriginUserRule('rule as string');
        $user = $this->getUser([$userRule]);
        $entry = new Entry($user);
        $entry->setUrl('http://example.com/hello-world');

        $this->ignoreOriginInstanceRuleRepository
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([]);

        $this->rulerz
            ->expects($this->once())
            ->method('satisfies')
            ->willReturn(true);

        $result = $this->processor->process($entry);

        $this->assertTrue($result);
    }

    public function testProcessWithAMixOfMatchingRules()
    {
        $userRule = $this->getIgnoreOriginUserRule('rule as string');
        $anotherUserRule = $this->getIgnoreOriginUserRule('another rule as string');
        $user = $this->getUser([$userRule, $anotherUserRule]);
        $entry = new Entry($user);
        $entry->setUrl('http://example.com/hello-world');

        $this->ignoreOriginInstanceRuleRepository
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([]);

        $this->rulerz
            ->method('satisfies')
            ->will($this->onConsecutiveCalls(false, true));

        $result = $this->processor->process($entry);

        $this->assertTrue($result);
    }

    public function testProcessWithInstanceRules()
    {
        $user = $this->getUser();
        $entry = new Entry($user);
        $entry->setUrl('http://example.com/hello-world');

        $instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
        $this->ignoreOriginInstanceRuleRepository
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([$instanceRule]);

        $this->rulerz
            ->expects($this->once())
            ->method('satisfies')
            ->willReturn(true);

        $result = $this->processor->process($entry);

        $this->assertTrue($result);
    }

    public function testProcessWithMixedRules()
    {
        $userRule = $this->getIgnoreOriginUserRule('rule as string');
        $user = $this->getUser([$userRule]);
        $entry = new Entry($user);
        $entry->setUrl('http://example.com/hello-world');

        $instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
        $this->ignoreOriginInstanceRuleRepository
            ->expects($this->once())
            ->method('findAll')
            ->willReturn([$instanceRule]);

        $this->rulerz
            ->method('satisfies')
            ->will($this->onConsecutiveCalls(false, true));

        $result = $this->processor->process($entry);

        $this->assertTrue($result);
    }

    private function getUser(array $ignoreOriginRules = [])
    {
        $user = new User();
        $config = new Config($user);

        $user->setConfig($config);

        foreach ($ignoreOriginRules as $rule) {
            $config->addIgnoreOriginRule($rule);
        }

        return $user;
    }

    private function getIgnoreOriginUserRule($rule)
    {
        $ignoreOriginUserRule = new IgnoreOriginUserRule();
        $ignoreOriginUserRule->setRule($rule);

        return $ignoreOriginUserRule;
    }

    private function getIgnoreOriginInstanceRule($rule)
    {
        $ignoreOriginInstanceRule = new IgnoreOriginInstanceRule();
        $ignoreOriginInstanceRule->setRule($rule);

        return $ignoreOriginInstanceRule;
    }

    private function getRulerZMock()
    {
        return $this->getMockBuilder('RulerZ\RulerZ')
            ->disableOriginalConstructor()
            ->getMock();
    }

    private function getIgnoreOriginInstanceRuleRepositoryMock()
    {
        return $this->getMockBuilder('Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository')
            ->disableOriginalConstructor()
            ->getMock();
    }

    private function getLogger()
    {
        return new Logger('foo');
    }
}