]>
Commit | Line | Data |
---|---|---|
a1a10770 JB |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\ImportBundle\Import; | |
4 | ||
f808b016 | 5 | use M6Web\Component\RedisMock\RedisMockFactory; |
a1a10770 | 6 | use Monolog\Handler\TestHandler; |
f808b016 | 7 | use Monolog\Logger; |
bd91bd5c | 8 | use PHPUnit\Framework\TestCase; |
b3437d58 | 9 | use Simpleue\Queue\RedisQueue; |
f808b016 JB |
10 | use Wallabag\CoreBundle\Entity\Entry; |
11 | use Wallabag\ImportBundle\Import\ReadabilityImport; | |
12 | use Wallabag\ImportBundle\Redis\Producer; | |
13 | use Wallabag\UserBundle\Entity\User; | |
a1a10770 | 14 | |
bd91bd5c | 15 | class ReadabilityImportTest extends TestCase |
a1a10770 JB |
16 | { |
17 | protected $user; | |
18 | protected $em; | |
19 | protected $logHandler; | |
20 | protected $contentProxy; | |
6bc6fb1f | 21 | protected $tagsAssigner; |
a1a10770 | 22 | |
a1a10770 JB |
23 | public function testInit() |
24 | { | |
25 | $readabilityImport = $this->getReadabilityImport(); | |
26 | ||
f808b016 | 27 | $this->assertSame('Readability', $readabilityImport->getName()); |
a1a10770 | 28 | $this->assertNotEmpty($readabilityImport->getUrl()); |
f808b016 | 29 | $this->assertSame('import.readability.description', $readabilityImport->getDescription()); |
a1a10770 JB |
30 | } |
31 | ||
32 | public function testImport() | |
33 | { | |
cf05a1ae | 34 | $readabilityImport = $this->getReadabilityImport(false, 3); |
f808b016 | 35 | $readabilityImport->setFilepath(__DIR__ . '/../fixtures/readability.json'); |
a1a10770 JB |
36 | |
37 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
38 | ->disableOriginalConstructor() | |
39 | ->getMock(); | |
40 | ||
cf05a1ae | 41 | $entryRepo->expects($this->exactly(3)) |
a1a10770 | 42 | ->method('findByUrlAndUserId') |
7230e4c3 | 43 | ->willReturn(false); |
a1a10770 JB |
44 | |
45 | $this->em | |
46 | ->expects($this->any()) | |
47 | ->method('getRepository') | |
48 | ->willReturn($entryRepo); | |
49 | ||
50 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
51 | ->disableOriginalConstructor() | |
52 | ->getMock(); | |
53 | ||
54 | $this->contentProxy | |
cf05a1ae | 55 | ->expects($this->exactly(3)) |
a1a10770 JB |
56 | ->method('updateEntry') |
57 | ->willReturn($entry); | |
58 | ||
59 | $res = $readabilityImport->import(); | |
60 | ||
61 | $this->assertTrue($res); | |
f808b016 | 62 | $this->assertSame(['skipped' => 0, 'imported' => 3, 'queued' => 0], $readabilityImport->getSummary()); |
a1a10770 JB |
63 | } |
64 | ||
65 | public function testImportAndMarkAllAsRead() | |
66 | { | |
7816eb62 | 67 | $readabilityImport = $this->getReadabilityImport(false, 1); |
f808b016 | 68 | $readabilityImport->setFilepath(__DIR__ . '/../fixtures/readability-read.json'); |
a1a10770 JB |
69 | |
70 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
71 | ->disableOriginalConstructor() | |
72 | ->getMock(); | |
73 | ||
74 | $entryRepo->expects($this->exactly(2)) | |
75 | ->method('findByUrlAndUserId') | |
7230e4c3 | 76 | ->will($this->onConsecutiveCalls(false, true)); |
a1a10770 JB |
77 | |
78 | $this->em | |
79 | ->expects($this->any()) | |
80 | ->method('getRepository') | |
81 | ->willReturn($entryRepo); | |
82 | ||
83 | $this->contentProxy | |
7230e4c3 | 84 | ->expects($this->exactly(1)) |
a1a10770 JB |
85 | ->method('updateEntry') |
86 | ->willReturn(new Entry($this->user)); | |
87 | ||
88 | // check that every entry persisted are archived | |
89 | $this->em | |
90 | ->expects($this->any()) | |
91 | ->method('persist') | |
92 | ->with($this->callback(function ($persistedEntry) { | |
93 | return $persistedEntry->isArchived(); | |
94 | })); | |
95 | ||
96 | $res = $readabilityImport->setMarkAsRead(true)->import(); | |
97 | ||
98 | $this->assertTrue($res); | |
99 | ||
f808b016 | 100 | $this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $readabilityImport->getSummary()); |
13470c35 JB |
101 | } |
102 | ||
103 | public function testImportWithRabbit() | |
104 | { | |
105 | $readabilityImport = $this->getReadabilityImport(); | |
f808b016 | 106 | $readabilityImport->setFilepath(__DIR__ . '/../fixtures/readability.json'); |
13470c35 JB |
107 | |
108 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
109 | ->disableOriginalConstructor() | |
110 | ->getMock(); | |
111 | ||
112 | $entryRepo->expects($this->never()) | |
113 | ->method('findByUrlAndUserId'); | |
114 | ||
115 | $this->em | |
116 | ->expects($this->never()) | |
117 | ->method('getRepository'); | |
118 | ||
119 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
120 | ->disableOriginalConstructor() | |
121 | ->getMock(); | |
122 | ||
123 | $this->contentProxy | |
124 | ->expects($this->never()) | |
125 | ->method('updateEntry'); | |
126 | ||
127 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | |
128 | ->disableOriginalConstructor() | |
129 | ->getMock(); | |
130 | ||
131 | $producer | |
cf05a1ae | 132 | ->expects($this->exactly(3)) |
13470c35 JB |
133 | ->method('publish'); |
134 | ||
b3437d58 | 135 | $readabilityImport->setProducer($producer); |
13470c35 JB |
136 | |
137 | $res = $readabilityImport->setMarkAsRead(true)->import(); | |
138 | ||
139 | $this->assertTrue($res); | |
f808b016 | 140 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 3], $readabilityImport->getSummary()); |
a1a10770 JB |
141 | } |
142 | ||
b3437d58 JB |
143 | public function testImportWithRedis() |
144 | { | |
145 | $readabilityImport = $this->getReadabilityImport(); | |
f808b016 | 146 | $readabilityImport->setFilepath(__DIR__ . '/../fixtures/readability.json'); |
b3437d58 JB |
147 | |
148 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
149 | ->disableOriginalConstructor() | |
150 | ->getMock(); | |
151 | ||
152 | $entryRepo->expects($this->never()) | |
153 | ->method('findByUrlAndUserId'); | |
154 | ||
155 | $this->em | |
156 | ->expects($this->never()) | |
157 | ->method('getRepository'); | |
158 | ||
159 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
160 | ->disableOriginalConstructor() | |
161 | ->getMock(); | |
162 | ||
163 | $this->contentProxy | |
164 | ->expects($this->never()) | |
165 | ->method('updateEntry'); | |
166 | ||
167 | $factory = new RedisMockFactory(); | |
168 | $redisMock = $factory->getAdapter('Predis\Client', true); | |
169 | ||
170 | $queue = new RedisQueue($redisMock, 'readability'); | |
171 | $producer = new Producer($queue); | |
172 | ||
173 | $readabilityImport->setProducer($producer); | |
174 | ||
175 | $res = $readabilityImport->setMarkAsRead(true)->import(); | |
176 | ||
177 | $this->assertTrue($res); | |
f808b016 | 178 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 3], $readabilityImport->getSummary()); |
b3437d58 JB |
179 | |
180 | $this->assertNotEmpty($redisMock->lpop('readability')); | |
181 | } | |
182 | ||
a1a10770 JB |
183 | public function testImportBadFile() |
184 | { | |
185 | $readabilityImport = $this->getReadabilityImport(); | |
f808b016 | 186 | $readabilityImport->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.jsonx'); |
a1a10770 JB |
187 | |
188 | $res = $readabilityImport->import(); | |
189 | ||
190 | $this->assertFalse($res); | |
191 | ||
192 | $records = $this->logHandler->getRecords(); | |
193 | $this->assertContains('ReadabilityImport: unable to read file', $records[0]['message']); | |
f808b016 | 194 | $this->assertSame('ERROR', $records[0]['level_name']); |
a1a10770 JB |
195 | } |
196 | ||
197 | public function testImportUserNotDefined() | |
198 | { | |
199 | $readabilityImport = $this->getReadabilityImport(true); | |
f808b016 | 200 | $readabilityImport->setFilepath(__DIR__ . '/../fixtures/readability.json'); |
a1a10770 JB |
201 | |
202 | $res = $readabilityImport->import(); | |
203 | ||
204 | $this->assertFalse($res); | |
205 | ||
206 | $records = $this->logHandler->getRecords(); | |
207 | $this->assertContains('ReadabilityImport: user is not defined', $records[0]['message']); | |
f808b016 JB |
208 | $this->assertSame('ERROR', $records[0]['level_name']); |
209 | } | |
210 | ||
211 | private function getReadabilityImport($unsetUser = false, $dispatched = 0) | |
212 | { | |
213 | $this->user = new User(); | |
214 | ||
215 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
216 | ->disableOriginalConstructor() | |
217 | ->getMock(); | |
218 | ||
219 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | |
220 | ->disableOriginalConstructor() | |
221 | ->getMock(); | |
222 | ||
223 | $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner') | |
224 | ->disableOriginalConstructor() | |
225 | ->getMock(); | |
226 | ||
227 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') | |
228 | ->disableOriginalConstructor() | |
229 | ->getMock(); | |
230 | ||
231 | $dispatcher | |
232 | ->expects($this->exactly($dispatched)) | |
233 | ->method('dispatch'); | |
234 | ||
235 | $wallabag = new ReadabilityImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher); | |
236 | ||
237 | $this->logHandler = new TestHandler(); | |
238 | $logger = new Logger('test', [$this->logHandler]); | |
239 | $wallabag->setLogger($logger); | |
240 | ||
241 | if (false === $unsetUser) { | |
242 | $wallabag->setUser($this->user); | |
243 | } | |
244 | ||
245 | return $wallabag; | |
a1a10770 JB |
246 | } |
247 | } |