3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV1Import
;
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 WallabagV1ImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
20 protected $tagsAssigner;
23 private function getWallabagV1Import($unsetUser = false, $dispatched = 0)
25 $this->user
= new User();
27 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
28 ->disableOriginalConstructor()
31 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
32 ->disableOriginalConstructor()
36 ->expects($this->any())
37 ->method('getUnitOfWork')
38 ->willReturn($this->uow
);
41 ->expects($this->any())
42 ->method('getScheduledEntityInsertions')
45 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
46 ->disableOriginalConstructor()
49 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
50 ->disableOriginalConstructor()
53 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
54 ->disableOriginalConstructor()
58 ->expects($this->exactly($dispatched))
61 $wallabag = new WallabagV1Import($this->em
, $this->contentProxy
, $this->tagsAssigner
, $dispatcher);
63 $this->logHandler
= new TestHandler();
64 $logger = new Logger('test', [$this->logHandler
]);
65 $wallabag->setLogger($logger);
67 if (false === $unsetUser) {
68 $wallabag->setUser($this->user
);
74 public function testInit()
76 $wallabagV1Import = $this->getWallabagV1Import();
78 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
79 $this->assertNotEmpty($wallabagV1Import->getUrl());
80 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
83 public function testImport()
85 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
86 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
88 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
89 ->disableOriginalConstructor()
92 $entryRepo->expects($this->exactly(4))
93 ->method('findByUrlAndUserId')
94 ->will($this->onConsecutiveCalls(false, true, false, false));
97 ->expects($this->any())
98 ->method('getRepository')
99 ->willReturn($entryRepo);
101 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
102 ->disableOriginalConstructor()
106 ->expects($this->exactly(3))
107 ->method('updateEntry')
108 ->willReturn($entry);
110 $res = $wallabagV1Import->import();
112 $this->assertTrue($res);
113 $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
116 public function testImportAndMarkAllAsRead()
118 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
119 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1-read.json');
121 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
122 ->disableOriginalConstructor()
125 $entryRepo->expects($this->exactly(3))
126 ->method('findByUrlAndUserId')
127 ->will($this->onConsecutiveCalls(false, false, false));
130 ->expects($this->any())
131 ->method('getRepository')
132 ->willReturn($entryRepo);
135 ->expects($this->exactly(3))
136 ->method('updateEntry')
137 ->willReturn(new Entry($this->user
));
139 // check that every entry persisted are archived
141 ->expects($this->any())
143 ->with($this->callback(function ($persistedEntry) {
144 return $persistedEntry->isArchived();
147 $res = $wallabagV1Import->setMarkAsRead(true)->import();
149 $this->assertTrue($res);
151 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
154 public function testImportWithRabbit()
156 $wallabagV1Import = $this->getWallabagV1Import();
157 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
159 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
160 ->disableOriginalConstructor()
163 $entryRepo->expects($this->never())
164 ->method('findByUrlAndUserId');
167 ->expects($this->never())
168 ->method('getRepository');
170 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
171 ->disableOriginalConstructor()
175 ->expects($this->never())
176 ->method('updateEntry');
178 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
179 ->disableOriginalConstructor()
183 ->expects($this->exactly(4))
186 $wallabagV1Import->setProducer($producer);
188 $res = $wallabagV1Import->setMarkAsRead(true)->import();
190 $this->assertTrue($res);
191 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
194 public function testImportWithRedis()
196 $wallabagV1Import = $this->getWallabagV1Import();
197 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
199 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
200 ->disableOriginalConstructor()
203 $entryRepo->expects($this->never())
204 ->method('findByUrlAndUserId');
207 ->expects($this->never())
208 ->method('getRepository');
210 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
211 ->disableOriginalConstructor()
215 ->expects($this->never())
216 ->method('updateEntry');
218 $factory = new RedisMockFactory();
219 $redisMock = $factory->getAdapter('Predis\Client', true);
221 $queue = new RedisQueue($redisMock, 'wallabag_v1');
222 $producer = new Producer($queue);
224 $wallabagV1Import->setProducer($producer);
226 $res = $wallabagV1Import->setMarkAsRead(true)->import();
228 $this->assertTrue($res);
229 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
231 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
234 public function testImportBadFile()
236 $wallabagV1Import = $this->getWallabagV1Import();
237 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
239 $res = $wallabagV1Import->import();
241 $this->assertFalse($res);
243 $records = $this->logHandler
->getRecords();
244 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
245 $this->assertEquals('ERROR', $records[0]['level_name']);
248 public function testImportUserNotDefined()
250 $wallabagV1Import = $this->getWallabagV1Import(true);
251 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
253 $res = $wallabagV1Import->import();
255 $this->assertFalse($res);
257 $records = $this->logHandler
->getRecords();
258 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
259 $this->assertEquals('ERROR', $records[0]['level_name']);