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