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;
21 private function getWallabagV1Import($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 WallabagV1Import($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 $wallabagV1Import = $this->getWallabagV1Import();
50 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
51 $this->assertNotEmpty($wallabagV1Import->getUrl());
52 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
55 public function testImport()
57 $wallabagV1Import = $this->getWallabagV1Import();
58 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
60 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
61 ->disableOriginalConstructor()
64 $entryRepo->expects($this->exactly(4))
65 ->method('findByUrlAndUserId')
66 ->will($this->onConsecutiveCalls(false, true, false, false));
69 ->expects($this->any())
70 ->method('getRepository')
71 ->willReturn($entryRepo);
73 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
74 ->disableOriginalConstructor()
78 ->expects($this->exactly(3))
79 ->method('updateEntry')
82 $res = $wallabagV1Import->import();
84 $this->assertTrue($res);
85 $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
88 public function testImportAndMarkAllAsRead()
90 $wallabagV1Import = $this->getWallabagV1Import();
91 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1-read.json');
93 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
94 ->disableOriginalConstructor()
97 $entryRepo->expects($this->exactly(3))
98 ->method('findByUrlAndUserId')
99 ->will($this->onConsecutiveCalls(false, false, false));
102 ->expects($this->any())
103 ->method('getRepository')
104 ->willReturn($entryRepo);
107 ->expects($this->exactly(3))
108 ->method('updateEntry')
109 ->willReturn(new Entry($this->user
));
111 // check that every entry persisted are archived
113 ->expects($this->any())
115 ->with($this->callback(function ($persistedEntry) {
116 return $persistedEntry->isArchived();
119 $res = $wallabagV1Import->setMarkAsRead(true)->import();
121 $this->assertTrue($res);
123 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
126 public function testImportWithRabbit()
128 $wallabagV1Import = $this->getWallabagV1Import();
129 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
131 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
132 ->disableOriginalConstructor()
135 $entryRepo->expects($this->never())
136 ->method('findByUrlAndUserId');
139 ->expects($this->never())
140 ->method('getRepository');
142 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
143 ->disableOriginalConstructor()
147 ->expects($this->never())
148 ->method('updateEntry');
150 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
151 ->disableOriginalConstructor()
155 ->expects($this->exactly(4))
158 $wallabagV1Import->setProducer($producer);
160 $res = $wallabagV1Import->setMarkAsRead(true)->import();
162 $this->assertTrue($res);
163 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
166 public function testImportWithRedis()
168 $wallabagV1Import = $this->getWallabagV1Import();
169 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
171 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
172 ->disableOriginalConstructor()
175 $entryRepo->expects($this->never())
176 ->method('findByUrlAndUserId');
179 ->expects($this->never())
180 ->method('getRepository');
182 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
183 ->disableOriginalConstructor()
187 ->expects($this->never())
188 ->method('updateEntry');
190 $factory = new RedisMockFactory();
191 $redisMock = $factory->getAdapter('Predis\Client', true);
193 $queue = new RedisQueue($redisMock, 'wallabag_v1');
194 $producer = new Producer($queue);
196 $wallabagV1Import->setProducer($producer);
198 $res = $wallabagV1Import->setMarkAsRead(true)->import();
200 $this->assertTrue($res);
201 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
203 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
206 public function testImportBadFile()
208 $wallabagV1Import = $this->getWallabagV1Import();
209 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
211 $res = $wallabagV1Import->import();
213 $this->assertFalse($res);
215 $records = $this->logHandler
->getRecords();
216 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
217 $this->assertEquals('ERROR', $records[0]['level_name']);
220 public function testImportUserNotDefined()
222 $wallabagV1Import = $this->getWallabagV1Import(true);
223 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
225 $res = $wallabagV1Import->import();
227 $this->assertFalse($res);
229 $records = $this->logHandler
->getRecords();
230 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
231 $this->assertEquals('ERROR', $records[0]['level_name']);