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