]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php
Add a real configuration for CS-Fixer
[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;
59201088 8use Simpleue\Queue\RedisQueue;
f808b016
JB
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\ImportBundle\Import\FirefoxImport;
11use Wallabag\ImportBundle\Redis\Producer;
12use Wallabag\UserBundle\Entity\User;
59201088
TC
13
14class FirefoxImportTest extends \PHPUnit_Framework_TestCase
15{
16 protected $user;
17 protected $em;
18 protected $logHandler;
19 protected $contentProxy;
6bc6fb1f 20 protected $tagsAssigner;
59201088 21
59201088
TC
22 public function testInit()
23 {
24 $firefoxImport = $this->getFirefoxImport();
25
f808b016 26 $this->assertSame('Firefox', $firefoxImport->getName());
59201088 27 $this->assertNotEmpty($firefoxImport->getUrl());
f808b016 28 $this->assertSame('import.firefox.description', $firefoxImport->getDescription());
59201088
TC
29 }
30
31 public function testImport()
32 {
7816eb62 33 $firefoxImport = $this->getFirefoxImport(false, 2);
f808b016 34 $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json');
59201088
TC
35
36 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
37 ->disableOriginalConstructor()
38 ->getMock();
39
12d93e68 40 $entryRepo->expects($this->exactly(2))
59201088
TC
41 ->method('findByUrlAndUserId')
42 ->willReturn(false);
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
12d93e68 54 ->expects($this->exactly(2))
59201088
TC
55 ->method('updateEntry')
56 ->willReturn($entry);
57
58 $res = $firefoxImport->import();
59
60 $this->assertTrue($res);
f808b016 61 $this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $firefoxImport->getSummary());
59201088
TC
62 }
63
64 public function testImportAndMarkAllAsRead()
65 {
7816eb62 66 $firefoxImport = $this->getFirefoxImport(false, 1);
f808b016 67 $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json');
59201088
TC
68
69 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
70 ->disableOriginalConstructor()
71 ->getMock();
72
12d93e68 73 $entryRepo->expects($this->exactly(2))
59201088
TC
74 ->method('findByUrlAndUserId')
75 ->will($this->onConsecutiveCalls(false, true));
76
77 $this->em
78 ->expects($this->any())
79 ->method('getRepository')
80 ->willReturn($entryRepo);
81
82 $this->contentProxy
83 ->expects($this->exactly(1))
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 = $firefoxImport->setMarkAsRead(true)->import();
96
97 $this->assertTrue($res);
98
f808b016 99 $this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $firefoxImport->getSummary());
59201088
TC
100 }
101
102 public function testImportWithRabbit()
103 {
104 $firefoxImport = $this->getFirefoxImport();
f808b016 105 $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json');
59201088
TC
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
64b1229b 131 ->expects($this->exactly(1))
59201088
TC
132 ->method('publish');
133
134 $firefoxImport->setProducer($producer);
135
64b1229b 136 $res = $firefoxImport->setMarkAsRead(true)->import();
59201088
TC
137
138 $this->assertTrue($res);
f808b016 139 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
59201088
TC
140 }
141
142 public function testImportWithRedis()
143 {
64b1229b 144 $firefoxImport = $this->getFirefoxImport();
f808b016 145 $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json');
59201088
TC
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, 'firefox');
170 $producer = new Producer($queue);
171
172 $firefoxImport->setProducer($producer);
173
174 $res = $firefoxImport->setMarkAsRead(true)->import();
175
176 $this->assertTrue($res);
f808b016 177 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary());
59201088
TC
178
179 $this->assertNotEmpty($redisMock->lpop('firefox'));
180 }
181
182 public function testImportBadFile()
183 {
184 $firefoxImport = $this->getFirefoxImport();
f808b016 185 $firefoxImport->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.jsonx');
59201088
TC
186
187 $res = $firefoxImport->import();
188
189 $this->assertFalse($res);
190
191 $records = $this->logHandler->getRecords();
64b1229b 192 $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']);
f808b016 193 $this->assertSame('ERROR', $records[0]['level_name']);
59201088
TC
194 }
195
196 public function testImportUserNotDefined()
197 {
198 $firefoxImport = $this->getFirefoxImport(true);
f808b016 199 $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json');
59201088
TC
200
201 $res = $firefoxImport->import();
202
203 $this->assertFalse($res);
204
205 $records = $this->logHandler->getRecords();
64b1229b 206 $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']);
f808b016
JB
207 $this->assertSame('ERROR', $records[0]['level_name']);
208 }
209
210 private function getFirefoxImport($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 FirefoxImport($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;
59201088
TC
245 }
246}