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