3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV2Import
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\ImportBundle\Redis\Producer
;
10 use Monolog\Handler\TestHandler
;
11 use Simpleue\Queue\RedisQueue
;
12 use M6Web\Component\RedisMock\RedisMockFactory
;
14 class WallabagV2ImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
21 private function getWallabagV2Import($unsetUser = false)
23 $this->user
= new User();
25 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
26 ->disableOriginalConstructor()
29 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
30 ->disableOriginalConstructor()
33 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
);
35 $this->logHandler
= new TestHandler();
36 $logger = new Logger('test', [$this->logHandler
]);
37 $wallabag->setLogger($logger);
39 if (false === $unsetUser) {
40 $wallabag->setUser($this->user
);
46 public function testInit()
48 $wallabagV2Import = $this->getWallabagV2Import();
50 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
51 $this->assertNotEmpty($wallabagV2Import->getUrl());
52 $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription());
55 public function testImport()
57 $wallabagV2Import = $this->getWallabagV2Import();
58 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
60 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
61 ->disableOriginalConstructor()
64 $entryRepo->expects($this->exactly(24))
65 ->method('findByUrlAndUserId')
66 ->will($this->onConsecutiveCalls(false, true, false));
69 ->expects($this->any())
70 ->method('getRepository')
71 ->willReturn($entryRepo);
74 ->expects($this->exactly(2))
75 ->method('updateEntry')
76 ->willReturn(new Entry($this->user
));
78 $res = $wallabagV2Import->import();
80 $this->assertTrue($res);
81 $this->assertEquals(['skipped' => 22, 'imported' => 2], $wallabagV2Import->getSummary());
84 public function testImportAndMarkAllAsRead()
86 $wallabagV2Import = $this->getWallabagV2Import();
87 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-read.json');
89 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
90 ->disableOriginalConstructor()
93 $entryRepo->expects($this->exactly(2))
94 ->method('findByUrlAndUserId')
95 ->will($this->onConsecutiveCalls(false, false));
98 ->expects($this->any())
99 ->method('getRepository')
100 ->willReturn($entryRepo);
103 ->expects($this->exactly(2))
104 ->method('updateEntry')
105 ->willReturn(new Entry($this->user
));
107 // check that every entry persisted are archived
109 ->expects($this->any())
111 ->with($this->callback(function ($persistedEntry) {
112 return $persistedEntry->isArchived();
115 $res = $wallabagV2Import->setMarkAsRead(true)->import();
117 $this->assertTrue($res);
119 $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary());
122 public function testImportWithRabbit()
124 $wallabagV2Import = $this->getWallabagV2Import();
125 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
127 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
128 ->disableOriginalConstructor()
131 $entryRepo->expects($this->never())
132 ->method('findByUrlAndUserId');
135 ->expects($this->never())
136 ->method('getRepository');
139 ->expects($this->never())
140 ->method('updateEntry');
142 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
143 ->disableOriginalConstructor()
147 ->expects($this->exactly(24))
150 $wallabagV2Import->setProducer($producer);
152 $res = $wallabagV2Import->setMarkAsRead(true)->import();
154 $this->assertTrue($res);
155 $this->assertEquals(['skipped' => 0, 'imported' => 24], $wallabagV2Import->getSummary());
158 public function testImportWithRedis()
160 $wallabagV2Import = $this->getWallabagV2Import();
161 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
163 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
164 ->disableOriginalConstructor()
167 $entryRepo->expects($this->never())
168 ->method('findByUrlAndUserId');
171 ->expects($this->never())
172 ->method('getRepository');
175 ->expects($this->never())
176 ->method('updateEntry');
178 $factory = new RedisMockFactory();
179 $redisMock = $factory->getAdapter('Predis\Client', true);
181 $queue = new RedisQueue($redisMock, 'wallabag_v2');
182 $producer = new Producer($queue);
184 $wallabagV2Import->setProducer($producer);
186 $res = $wallabagV2Import->setMarkAsRead(true)->import();
188 $this->assertTrue($res);
189 $this->assertEquals(['skipped' => 0, 'imported' => 24], $wallabagV2Import->getSummary());
191 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
194 public function testImportBadFile()
196 $wallabagV1Import = $this->getWallabagV2Import();
197 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.jsonx');
199 $res = $wallabagV1Import->import();
201 $this->assertFalse($res);
203 $records = $this->logHandler
->getRecords();
204 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
205 $this->assertEquals('ERROR', $records[0]['level_name']);
208 public function testImportUserNotDefined()
210 $wallabagV1Import = $this->getWallabagV2Import(true);
211 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
213 $res = $wallabagV1Import->import();
215 $this->assertFalse($res);
217 $records = $this->logHandler
->getRecords();
218 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
219 $this->assertEquals('ERROR', $records[0]['level_name']);
222 public function testImportEmptyFile()
224 $wallabagV2Import = $this->getWallabagV2Import();
225 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-empty.json');
227 $res = $wallabagV2Import->import();
229 $this->assertFalse($res);
230 $this->assertEquals(['skipped' => 0, 'imported' => 0], $wallabagV2Import->getSummary());
233 public function testImportWithExceptionFromGraby()
235 $wallabagV2Import = $this->getWallabagV2Import();
236 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
238 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
239 ->disableOriginalConstructor()
242 $entryRepo->expects($this->exactly(24))
243 ->method('findByUrlAndUserId')
244 ->will($this->onConsecutiveCalls(false, true, false));
247 ->expects($this->any())
248 ->method('getRepository')
249 ->willReturn($entryRepo);
252 ->expects($this->exactly(2))
253 ->method('updateEntry')
254 ->will($this->throwException(new \
Exception()));
256 $res = $wallabagV2Import->import();
258 $this->assertTrue($res);
259 $this->assertEquals(['skipped' => 24, 'imported' => 0], $wallabagV2Import->getSummary());