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