3 namespace Wallabag\ImportBundle\Tests\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV2Import
;
6 use Wallabag\UserBundle\Entity\User
;
8 use Monolog\Handler\TestHandler
;
10 class WallabagV2ImportTest
extends \PHPUnit_Framework_TestCase
14 protected $logHandler;
15 protected $contentProxy;
17 private function getWallabagV2Import($unsetUser = false)
19 $this->user
= new User();
21 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
22 ->disableOriginalConstructor()
25 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
26 ->disableOriginalConstructor()
29 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
);
31 $this->logHandler
= new TestHandler();
32 $logger = new Logger('test', array($this->logHandler
));
33 $wallabag->setLogger($logger);
35 if (false === $unsetUser) {
36 $wallabag->setUser($this->user
);
42 public function testInit()
44 $wallabagV2Import = $this->getWallabagV2Import();
46 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
47 $this->assertNotEmpty($wallabagV2Import->getUrl());
48 $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription());
51 public function testImport()
53 $wallabagV2Import = $this->getWallabagV2Import();
54 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
56 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
57 ->disableOriginalConstructor()
60 $entryRepo->expects($this->exactly(3))
61 ->method('findByUrlAndUserId')
62 ->will($this->onConsecutiveCalls(false, true, false));
65 ->expects($this->any())
66 ->method('getRepository')
67 ->willReturn($entryRepo);
69 $res = $wallabagV2Import->import();
71 $this->assertTrue($res);
72 $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV2Import->getSummary());
75 public function testImportAndMarkAllAsRead()
77 $wallabagV2Import = $this->getWallabagV2Import();
78 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-read.json');
80 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
81 ->disableOriginalConstructor()
84 $entryRepo->expects($this->exactly(2))
85 ->method('findByUrlAndUserId')
86 ->will($this->onConsecutiveCalls(false, false));
89 ->expects($this->any())
90 ->method('getRepository')
91 ->willReturn($entryRepo);
93 // check that every entry persisted are archived
95 ->expects($this->any())
97 ->with($this->callback(function ($persistedEntry) {
98 return $persistedEntry->isArchived();
101 $res = $wallabagV2Import->setMarkAsRead(true)->import();
103 $this->assertTrue($res);
105 $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary());
108 public function testImportBadFile()
110 $wallabagV1Import = $this->getWallabagV2Import();
111 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.jsonx');
113 $res = $wallabagV1Import->import();
115 $this->assertFalse($res);
117 $records = $this->logHandler
->getRecords();
118 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
119 $this->assertEquals('ERROR', $records[0]['level_name']);
122 public function testImportUserNotDefined()
124 $wallabagV1Import = $this->getWallabagV2Import(true);
125 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
127 $res = $wallabagV1Import->import();
129 $this->assertFalse($res);
131 $records = $this->logHandler
->getRecords();
132 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
133 $this->assertEquals('ERROR', $records[0]['level_name']);