3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use M6Web\Component\RedisMock\RedisMockFactory
;
6 use Monolog\Handler\TestHandler
;
8 use Simpleue\Queue\RedisQueue
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\ImportBundle\Import\WallabagV1Import
;
11 use Wallabag\ImportBundle\Redis\Producer
;
12 use Wallabag\UserBundle\Entity\User
;
14 class WallabagV1ImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
20 protected $tagsAssigner;
22 protected $fetchingErrorMessageTitle = 'No title found';
23 protected $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/master/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.';
25 public function testInit()
27 $wallabagV1Import = $this->getWallabagV1Import();
29 $this->assertSame('wallabag v1', $wallabagV1Import->getName());
30 $this->assertNotEmpty($wallabagV1Import->getUrl());
31 $this->assertSame('import.wallabag_v1.description', $wallabagV1Import->getDescription());
34 public function testImport()
36 $wallabagV1Import = $this->getWallabagV1Import(false, 1);
37 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
39 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
40 ->disableOriginalConstructor()
43 $entryRepo->expects($this->exactly(2))
44 ->method('findByUrlAndUserId')
45 ->will($this->onConsecutiveCalls(false, true, false, false));
48 ->expects($this->any())
49 ->method('getRepository')
50 ->willReturn($entryRepo);
52 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
53 ->disableOriginalConstructor()
57 ->expects($this->exactly(1))
58 ->method('updateEntry')
61 $res = $wallabagV1Import->import();
63 $this->assertTrue($res);
64 $this->assertSame(['skipped' => 1, 'imported' => 1, 'queued' => 0], $wallabagV1Import->getSummary());
67 public function testImportAndMarkAllAsRead()
69 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
70 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1-read.json');
72 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
73 ->disableOriginalConstructor()
76 $entryRepo->expects($this->exactly(3))
77 ->method('findByUrlAndUserId')
78 ->will($this->onConsecutiveCalls(false, false, false));
81 ->expects($this->any())
82 ->method('getRepository')
83 ->willReturn($entryRepo);
86 ->expects($this->exactly(3))
87 ->method('updateEntry')
88 ->willReturn(new Entry($this->user
));
90 // check that every entry persisted are archived
92 ->expects($this->any())
94 ->with($this->callback(function ($persistedEntry) {
95 return $persistedEntry->isArchived();
98 $res = $wallabagV1Import->setMarkAsRead(true)->import();
100 $this->assertTrue($res);
102 $this->assertSame(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
105 public function testImportWithRabbit()
107 $wallabagV1Import = $this->getWallabagV1Import();
108 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
110 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
111 ->disableOriginalConstructor()
114 $entryRepo->expects($this->never())
115 ->method('findByUrlAndUserId');
118 ->expects($this->never())
119 ->method('getRepository');
121 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
122 ->disableOriginalConstructor()
126 ->expects($this->never())
127 ->method('updateEntry');
129 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
130 ->disableOriginalConstructor()
134 ->expects($this->exactly(2))
137 $wallabagV1Import->setProducer($producer);
139 $res = $wallabagV1Import->setMarkAsRead(true)->import();
141 $this->assertTrue($res);
142 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
145 public function testImportWithRedis()
147 $wallabagV1Import = $this->getWallabagV1Import();
148 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
150 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
151 ->disableOriginalConstructor()
154 $entryRepo->expects($this->never())
155 ->method('findByUrlAndUserId');
158 ->expects($this->never())
159 ->method('getRepository');
161 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
162 ->disableOriginalConstructor()
166 ->expects($this->never())
167 ->method('updateEntry');
169 $factory = new RedisMockFactory();
170 $redisMock = $factory->getAdapter('Predis\Client', true);
172 $queue = new RedisQueue($redisMock, 'wallabag_v1');
173 $producer = new Producer($queue);
175 $wallabagV1Import->setProducer($producer);
177 $res = $wallabagV1Import->setMarkAsRead(true)->import();
179 $this->assertTrue($res);
180 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 2], $wallabagV1Import->getSummary());
182 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
185 public function testImportBadFile()
187 $wallabagV1Import = $this->getWallabagV1Import();
188 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.jsonx');
190 $res = $wallabagV1Import->import();
192 $this->assertFalse($res);
194 $records = $this->logHandler
->getRecords();
195 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
196 $this->assertSame('ERROR', $records[0]['level_name']);
199 public function testImportUserNotDefined()
201 $wallabagV1Import = $this->getWallabagV1Import(true);
202 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.json');
204 $res = $wallabagV1Import->import();
206 $this->assertFalse($res);
208 $records = $this->logHandler
->getRecords();
209 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
210 $this->assertSame('ERROR', $records[0]['level_name']);
213 private function getWallabagV1Import($unsetUser = false, $dispatched = 0)
215 $this->user
= new User();
217 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
218 ->disableOriginalConstructor()
221 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
222 ->disableOriginalConstructor()
226 ->expects($this->any())
227 ->method('getUnitOfWork')
228 ->willReturn($this->uow
);
231 ->expects($this->any())
232 ->method('getScheduledEntityInsertions')
235 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
236 ->disableOriginalConstructor()
239 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
240 ->disableOriginalConstructor()
243 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
244 ->disableOriginalConstructor()
248 ->expects($this->exactly($dispatched))
249 ->method('dispatch');
251 $wallabag = new WallabagV1Import(
256 $this->fetchingErrorMessageTitle
,
257 $this->fetchingErrorMessage
260 $this->logHandler
= new TestHandler();
261 $logger = new Logger('test', [$this->logHandler
]);
262 $wallabag->setLogger($logger);
264 if (false === $unsetUser) {
265 $wallabag->setUser($this->user
);