aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php')
-rw-r--r--tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php154
1 files changed, 80 insertions, 74 deletions
diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php
index 0e50b8b2..fab50a45 100644
--- a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php
+++ b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php
@@ -2,14 +2,14 @@
2 2
3namespace Tests\Wallabag\ImportBundle\Import; 3namespace Tests\Wallabag\ImportBundle\Import;
4 4
5use Wallabag\ImportBundle\Import\WallabagV2Import; 5use M6Web\Component\RedisMock\RedisMockFactory;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\ImportBundle\Redis\Producer;
9use Monolog\Logger;
10use Monolog\Handler\TestHandler; 6use Monolog\Handler\TestHandler;
7use Monolog\Logger;
11use Simpleue\Queue\RedisQueue; 8use Simpleue\Queue\RedisQueue;
12use M6Web\Component\RedisMock\RedisMockFactory; 9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\ImportBundle\Import\WallabagV2Import;
11use Wallabag\ImportBundle\Redis\Producer;
12use Wallabag\UserBundle\Entity\User;
13 13
14class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase 14class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
15{ 15{
@@ -17,73 +17,28 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
17 protected $em; 17 protected $em;
18 protected $logHandler; 18 protected $logHandler;
19 protected $contentProxy; 19 protected $contentProxy;
20 20 protected $tagsAssigner;
21 private function getWallabagV2Import($unsetUser = false, $dispatched = 0) 21 protected $uow;
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 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
48 ->disableOriginalConstructor()
49 ->getMock();
50
51 $dispatcher
52 ->expects($this->exactly($dispatched))
53 ->method('dispatch');
54
55 $wallabag = new WallabagV2Import($this->em, $this->contentProxy, $dispatcher);
56
57 $this->logHandler = new TestHandler();
58 $logger = new Logger('test', [$this->logHandler]);
59 $wallabag->setLogger($logger);
60
61 if (false === $unsetUser) {
62 $wallabag->setUser($this->user);
63 }
64
65 return $wallabag;
66 }
67 22
68 public function testInit() 23 public function testInit()
69 { 24 {
70 $wallabagV2Import = $this->getWallabagV2Import(); 25 $wallabagV2Import = $this->getWallabagV2Import();
71 26
72 $this->assertEquals('wallabag v2', $wallabagV2Import->getName()); 27 $this->assertSame('wallabag v2', $wallabagV2Import->getName());
73 $this->assertNotEmpty($wallabagV2Import->getUrl()); 28 $this->assertNotEmpty($wallabagV2Import->getUrl());
74 $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription()); 29 $this->assertSame('import.wallabag_v2.description', $wallabagV2Import->getDescription());
75 } 30 }
76 31
77 public function testImport() 32 public function testImport()
78 { 33 {
79 $wallabagV2Import = $this->getWallabagV2Import(false, 2); 34 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
80 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); 35 $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json');
81 36
82 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') 37 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
83 ->disableOriginalConstructor() 38 ->disableOriginalConstructor()
84 ->getMock(); 39 ->getMock();
85 40
86 $entryRepo->expects($this->exactly(24)) 41 $entryRepo->expects($this->exactly(6))
87 ->method('findByUrlAndUserId') 42 ->method('findByUrlAndUserId')
88 ->will($this->onConsecutiveCalls(false, true, false)); 43 ->will($this->onConsecutiveCalls(false, true, false));
89 44
@@ -100,13 +55,13 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
100 $res = $wallabagV2Import->import(); 55 $res = $wallabagV2Import->import();
101 56
102 $this->assertTrue($res); 57 $this->assertTrue($res);
103 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); 58 $this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
104 } 59 }
105 60
106 public function testImportAndMarkAllAsRead() 61 public function testImportAndMarkAllAsRead()
107 { 62 {
108 $wallabagV2Import = $this->getWallabagV2Import(false, 2); 63 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
109 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-read.json'); 64 $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2-read.json');
110 65
111 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') 66 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
112 ->disableOriginalConstructor() 67 ->disableOriginalConstructor()
@@ -138,13 +93,13 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
138 93
139 $this->assertTrue($res); 94 $this->assertTrue($res);
140 95
141 $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); 96 $this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
142 } 97 }
143 98
144 public function testImportWithRabbit() 99 public function testImportWithRabbit()
145 { 100 {
146 $wallabagV2Import = $this->getWallabagV2Import(); 101 $wallabagV2Import = $this->getWallabagV2Import();
147 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); 102 $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json');
148 103
149 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') 104 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
150 ->disableOriginalConstructor() 105 ->disableOriginalConstructor()
@@ -166,7 +121,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
166 ->getMock(); 121 ->getMock();
167 122
168 $producer 123 $producer
169 ->expects($this->exactly(24)) 124 ->expects($this->exactly(6))
170 ->method('publish'); 125 ->method('publish');
171 126
172 $wallabagV2Import->setProducer($producer); 127 $wallabagV2Import->setProducer($producer);
@@ -174,13 +129,13 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
174 $res = $wallabagV2Import->setMarkAsRead(true)->import(); 129 $res = $wallabagV2Import->setMarkAsRead(true)->import();
175 130
176 $this->assertTrue($res); 131 $this->assertTrue($res);
177 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary()); 132 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
178 } 133 }
179 134
180 public function testImportWithRedis() 135 public function testImportWithRedis()
181 { 136 {
182 $wallabagV2Import = $this->getWallabagV2Import(); 137 $wallabagV2Import = $this->getWallabagV2Import();
183 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); 138 $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json');
184 139
185 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') 140 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
186 ->disableOriginalConstructor() 141 ->disableOriginalConstructor()
@@ -208,7 +163,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
208 $res = $wallabagV2Import->setMarkAsRead(true)->import(); 163 $res = $wallabagV2Import->setMarkAsRead(true)->import();
209 164
210 $this->assertTrue($res); 165 $this->assertTrue($res);
211 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary()); 166 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
212 167
213 $this->assertNotEmpty($redisMock->lpop('wallabag_v2')); 168 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
214 } 169 }
@@ -216,7 +171,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
216 public function testImportBadFile() 171 public function testImportBadFile()
217 { 172 {
218 $wallabagV1Import = $this->getWallabagV2Import(); 173 $wallabagV1Import = $this->getWallabagV2Import();
219 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.jsonx'); 174 $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.jsonx');
220 175
221 $res = $wallabagV1Import->import(); 176 $res = $wallabagV1Import->import();
222 177
@@ -224,13 +179,13 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
224 179
225 $records = $this->logHandler->getRecords(); 180 $records = $this->logHandler->getRecords();
226 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); 181 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
227 $this->assertEquals('ERROR', $records[0]['level_name']); 182 $this->assertSame('ERROR', $records[0]['level_name']);
228 } 183 }
229 184
230 public function testImportUserNotDefined() 185 public function testImportUserNotDefined()
231 { 186 {
232 $wallabagV1Import = $this->getWallabagV2Import(true); 187 $wallabagV1Import = $this->getWallabagV2Import(true);
233 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); 188 $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json');
234 189
235 $res = $wallabagV1Import->import(); 190 $res = $wallabagV1Import->import();
236 191
@@ -238,30 +193,30 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
238 193
239 $records = $this->logHandler->getRecords(); 194 $records = $this->logHandler->getRecords();
240 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); 195 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
241 $this->assertEquals('ERROR', $records[0]['level_name']); 196 $this->assertSame('ERROR', $records[0]['level_name']);
242 } 197 }
243 198
244 public function testImportEmptyFile() 199 public function testImportEmptyFile()
245 { 200 {
246 $wallabagV2Import = $this->getWallabagV2Import(); 201 $wallabagV2Import = $this->getWallabagV2Import();
247 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-empty.json'); 202 $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2-empty.json');
248 203
249 $res = $wallabagV2Import->import(); 204 $res = $wallabagV2Import->import();
250 205
251 $this->assertFalse($res); 206 $this->assertFalse($res);
252 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary()); 207 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
253 } 208 }
254 209
255 public function testImportWithExceptionFromGraby() 210 public function testImportWithExceptionFromGraby()
256 { 211 {
257 $wallabagV2Import = $this->getWallabagV2Import(false, 2); 212 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
258 $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); 213 $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json');
259 214
260 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') 215 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
261 ->disableOriginalConstructor() 216 ->disableOriginalConstructor()
262 ->getMock(); 217 ->getMock();
263 218
264 $entryRepo->expects($this->exactly(24)) 219 $entryRepo->expects($this->exactly(6))
265 ->method('findByUrlAndUserId') 220 ->method('findByUrlAndUserId')
266 ->will($this->onConsecutiveCalls(false, true, false)); 221 ->will($this->onConsecutiveCalls(false, true, false));
267 222
@@ -278,6 +233,57 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
278 $res = $wallabagV2Import->import(); 233 $res = $wallabagV2Import->import();
279 234
280 $this->assertTrue($res); 235 $this->assertTrue($res);
281 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); 236 $this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
237 }
238
239 private function getWallabagV2Import($unsetUser = false, $dispatched = 0)
240 {
241 $this->user = new User();
242
243 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
244 ->disableOriginalConstructor()
245 ->getMock();
246
247 $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
248 ->disableOriginalConstructor()
249 ->getMock();
250
251 $this->em
252 ->expects($this->any())
253 ->method('getUnitOfWork')
254 ->willReturn($this->uow);
255
256 $this->uow
257 ->expects($this->any())
258 ->method('getScheduledEntityInsertions')
259 ->willReturn([]);
260
261 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
262 ->disableOriginalConstructor()
263 ->getMock();
264
265 $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
266 ->disableOriginalConstructor()
267 ->getMock();
268
269 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
270 ->disableOriginalConstructor()
271 ->getMock();
272
273 $dispatcher
274 ->expects($this->exactly($dispatched))
275 ->method('dispatch');
276
277 $wallabag = new WallabagV2Import($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher);
278
279 $this->logHandler = new TestHandler();
280 $logger = new Logger('test', [$this->logHandler]);
281 $wallabag->setLogger($logger);
282
283 if (false === $unsetUser) {
284 $wallabag->setUser($this->user);
285 }
286
287 return $wallabag;
282 } 288 }
283} 289}