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;
16 private function getWallabagV2Import($unsetUser = false)
18 $this->user
= new User();
20 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
21 ->disableOriginalConstructor()
24 $wallabag = new WallabagV2Import($this->em
);
26 $this->logHandler
= new TestHandler();
27 $logger = new Logger('test', array($this->logHandler
));
28 $wallabag->setLogger($logger);
30 if (false === $unsetUser) {
31 $wallabag->setUser($this->user
);
37 public function testInit()
39 $wallabagV2Import = $this->getWallabagV2Import();
41 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
42 $this->assertNotEmpty($wallabagV2Import->getUrl());
43 $this->assertContains('This importer will import all your wallabag v2 articles.', $wallabagV2Import->getDescription());
46 public function testImport()
48 $wallabagV2Import = $this->getWallabagV2Import();
49 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
51 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
52 ->disableOriginalConstructor()
55 $entryRepo->expects($this->exactly(2))
56 ->method('findByUrlAndUserId')
57 ->will($this->onConsecutiveCalls(false, true, false));
60 ->expects($this->any())
61 ->method('getRepository')
62 ->willReturn($entryRepo);
64 $res = $wallabagV2Import->import();
66 $this->assertTrue($res);
67 $this->assertEquals(['skipped' => 1, 'imported' => 1], $wallabagV2Import->getSummary());
70 public function testImportBadFile()
72 $wallabagV1Import = $this->getWallabagV2Import();
73 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.jsonx');
75 $res = $wallabagV1Import->import();
77 $this->assertFalse($res);
79 $records = $this->logHandler
->getRecords();
80 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
81 $this->assertEquals('ERROR', $records[0]['level_name']);
84 public function testImportUserNotDefined()
86 $wallabagV1Import = $this->getWallabagV2Import(true);
87 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
89 $res = $wallabagV1Import->import();
91 $this->assertFalse($res);
93 $records = $this->logHandler
->getRecords();
94 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
95 $this->assertEquals('ERROR', $records[0]['level_name']);