]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\ImportBundle\Import; | |
4 | ||
5 | use M6Web\Component\RedisMock\RedisMockFactory; | |
6 | use Monolog\Handler\TestHandler; | |
7 | use Monolog\Logger; | |
8 | use Simpleue\Queue\RedisQueue; | |
9 | use Wallabag\CoreBundle\Entity\Entry; | |
10 | use Wallabag\ImportBundle\Import\FirefoxImport; | |
11 | use Wallabag\ImportBundle\Redis\Producer; | |
12 | use Wallabag\UserBundle\Entity\User; | |
13 | ||
14 | class FirefoxImportTest extends \PHPUnit_Framework_TestCase | |
15 | { | |
16 | protected $user; | |
17 | protected $em; | |
18 | protected $logHandler; | |
19 | protected $contentProxy; | |
20 | protected $tagsAssigner; | |
21 | ||
22 | public function testInit() | |
23 | { | |
24 | $firefoxImport = $this->getFirefoxImport(); | |
25 | ||
26 | $this->assertSame('Firefox', $firefoxImport->getName()); | |
27 | $this->assertNotEmpty($firefoxImport->getUrl()); | |
28 | $this->assertSame('import.firefox.description', $firefoxImport->getDescription()); | |
29 | } | |
30 | ||
31 | public function testImport() | |
32 | { | |
33 | $firefoxImport = $this->getFirefoxImport(false, 2); | |
34 | $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json'); | |
35 | ||
36 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
37 | ->disableOriginalConstructor() | |
38 | ->getMock(); | |
39 | ||
40 | $entryRepo->expects($this->exactly(2)) | |
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 | |
54 | ->expects($this->exactly(2)) | |
55 | ->method('updateEntry') | |
56 | ->willReturn($entry); | |
57 | ||
58 | $res = $firefoxImport->import(); | |
59 | ||
60 | $this->assertTrue($res); | |
61 | $this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $firefoxImport->getSummary()); | |
62 | } | |
63 | ||
64 | public function testImportAndMarkAllAsRead() | |
65 | { | |
66 | $firefoxImport = $this->getFirefoxImport(false, 1); | |
67 | $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json'); | |
68 | ||
69 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
70 | ->disableOriginalConstructor() | |
71 | ->getMock(); | |
72 | ||
73 | $entryRepo->expects($this->exactly(2)) | |
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 | ||
99 | $this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $firefoxImport->getSummary()); | |
100 | } | |
101 | ||
102 | public function testImportWithRabbit() | |
103 | { | |
104 | $firefoxImport = $this->getFirefoxImport(); | |
105 | $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json'); | |
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 | |
131 | ->expects($this->exactly(1)) | |
132 | ->method('publish'); | |
133 | ||
134 | $firefoxImport->setProducer($producer); | |
135 | ||
136 | $res = $firefoxImport->setMarkAsRead(true)->import(); | |
137 | ||
138 | $this->assertTrue($res); | |
139 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary()); | |
140 | } | |
141 | ||
142 | public function testImportWithRedis() | |
143 | { | |
144 | $firefoxImport = $this->getFirefoxImport(); | |
145 | $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json'); | |
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); | |
177 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary()); | |
178 | ||
179 | $this->assertNotEmpty($redisMock->lpop('firefox')); | |
180 | } | |
181 | ||
182 | public function testImportBadFile() | |
183 | { | |
184 | $firefoxImport = $this->getFirefoxImport(); | |
185 | $firefoxImport->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.jsonx'); | |
186 | ||
187 | $res = $firefoxImport->import(); | |
188 | ||
189 | $this->assertFalse($res); | |
190 | ||
191 | $records = $this->logHandler->getRecords(); | |
192 | $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']); | |
193 | $this->assertSame('ERROR', $records[0]['level_name']); | |
194 | } | |
195 | ||
196 | public function testImportUserNotDefined() | |
197 | { | |
198 | $firefoxImport = $this->getFirefoxImport(true); | |
199 | $firefoxImport->setFilepath(__DIR__ . '/../fixtures/firefox-bookmarks.json'); | |
200 | ||
201 | $res = $firefoxImport->import(); | |
202 | ||
203 | $this->assertFalse($res); | |
204 | ||
205 | $records = $this->logHandler->getRecords(); | |
206 | $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']); | |
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; | |
245 | } | |
246 | } |