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