]>
Commit | Line | Data |
---|---|---|
b1d05721 JB |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
b1d05721 | 4 | |
8f336fda | 5 | use Wallabag\ImportBundle\Import\WallabagV1Import; |
b1d05721 | 6 | use Wallabag\UserBundle\Entity\User; |
4d0ec0e7 | 7 | use Wallabag\CoreBundle\Entity\Entry; |
b3437d58 | 8 | use Wallabag\ImportBundle\Redis\Producer; |
b1d05721 JB |
9 | use Monolog\Logger; |
10 | use Monolog\Handler\TestHandler; | |
b3437d58 JB |
11 | use Simpleue\Queue\RedisQueue; |
12 | use M6Web\Component\RedisMock\RedisMockFactory; | |
b1d05721 JB |
13 | |
14 | class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | |
15 | { | |
16 | protected $user; | |
17 | protected $em; | |
18 | protected $logHandler; | |
0783c99a TC |
19 | protected $contentProxy; |
20 | ||
b1d05721 JB |
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 | ||
40113585 JB |
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 | ||
0783c99a TC |
43 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
44 | ->disableOriginalConstructor() | |
45 | ->getMock(); | |
46 | ||
da0a9e01 | 47 | $wallabag = new WallabagV1Import($this->em, $this->contentProxy); |
b1d05721 JB |
48 | |
49 | $this->logHandler = new TestHandler(); | |
4094ea47 | 50 | $logger = new Logger('test', [$this->logHandler]); |
6785f4aa | 51 | $wallabag->setLogger($logger); |
b1d05721 JB |
52 | |
53 | if (false === $unsetUser) { | |
6785f4aa | 54 | $wallabag->setUser($this->user); |
b1d05721 JB |
55 | } |
56 | ||
6785f4aa | 57 | return $wallabag; |
b1d05721 JB |
58 | } |
59 | ||
60 | public function testInit() | |
61 | { | |
62 | $wallabagV1Import = $this->getWallabagV1Import(); | |
63 | ||
b88cf91f | 64 | $this->assertEquals('wallabag v1', $wallabagV1Import->getName()); |
7019c7cf | 65 | $this->assertNotEmpty($wallabagV1Import->getUrl()); |
0d42217e | 66 | $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription()); |
b1d05721 JB |
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 | ||
eaf9dad7 | 78 | $entryRepo->expects($this->exactly(4)) |
78833672 | 79 | ->method('findByUrlAndUserId') |
eaf9dad7 | 80 | ->will($this->onConsecutiveCalls(false, true, false, false)); |
b1d05721 JB |
81 | |
82 | $this->em | |
83 | ->expects($this->any()) | |
84 | ->method('getRepository') | |
85 | ->willReturn($entryRepo); | |
86 | ||
eaf9dad7 TC |
87 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') |
88 | ->disableOriginalConstructor() | |
89 | ->getMock(); | |
90 | ||
91 | $this->contentProxy | |
4d0ec0e7 | 92 | ->expects($this->exactly(3)) |
eaf9dad7 TC |
93 | ->method('updateEntry') |
94 | ->willReturn($entry); | |
95 | ||
b1d05721 JB |
96 | $res = $wallabagV1Import->import(); |
97 | ||
98 | $this->assertTrue($res); | |
c80cc01a | 99 | $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary()); |
79d0e38e JB |
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 | ||
4d0ec0e7 JB |
120 | $this->contentProxy |
121 | ->expects($this->exactly(3)) | |
122 | ->method('updateEntry') | |
123 | ->willReturn(new Entry($this->user)); | |
124 | ||
79d0e38e JB |
125 | // check that every entry persisted are archived |
126 | $this->em | |
127 | ->expects($this->any()) | |
128 | ->method('persist') | |
cebb4223 | 129 | ->with($this->callback(function ($persistedEntry) { |
79d0e38e JB |
130 | return $persistedEntry->isArchived(); |
131 | })); | |
132 | ||
133 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | |
134 | ||
135 | $this->assertTrue($res); | |
136 | ||
c80cc01a | 137 | $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary()); |
13470c35 JB |
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 | ||
b3437d58 | 172 | $wallabagV1Import->setProducer($producer); |
13470c35 JB |
173 | |
174 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | |
175 | ||
176 | $this->assertTrue($res); | |
c80cc01a | 177 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary()); |
b1d05721 JB |
178 | } |
179 | ||
b3437d58 JB |
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); | |
c80cc01a | 215 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary()); |
b3437d58 JB |
216 | |
217 | $this->assertNotEmpty($redisMock->lpop('wallabag_v1')); | |
218 | } | |
219 | ||
b1d05721 JB |
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(); | |
6785f4aa | 230 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); |
b1d05721 JB |
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(); | |
6785f4aa | 244 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); |
b1d05721 JB |
245 | $this->assertEquals('ERROR', $records[0]['level_name']); |
246 | } | |
247 | } |