3 namespace Wallabag\ImportBundle\Tests\Import
;
5 use Wallabag\UserBundle\Entity\User
;
6 use Wallabag\ImportBundle\Import\WallabagV1Import
;
8 use Monolog\Handler\TestHandler
;
10 class WallabagV1ImportTest
extends \PHPUnit_Framework_TestCase
14 protected $logHandler;
16 private function getWallabagV1Import($unsetUser = false)
18 $this->user
= new User();
20 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
21 ->disableOriginalConstructor()
24 $pocket = new WallabagV1Import($this->em
);
26 $this->logHandler
= new TestHandler();
27 $logger = new Logger('test', array($this->logHandler
));
28 $pocket->setLogger($logger);
30 if (false === $unsetUser) {
31 $pocket->setUser($this->user
);
37 public function testInit()
39 $wallabagV1Import = $this->getWallabagV1Import();
41 $this->assertEquals('Wallabag v1', $wallabagV1Import->getName());
42 $this->assertEquals('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription());
45 public function testImport()
47 $wallabagV1Import = $this->getWallabagV1Import();
48 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
50 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
51 ->disableOriginalConstructor()
54 $entryRepo->expects($this->exactly(3))
55 ->method('existByUrlAndUserId')
56 ->will($this->onConsecutiveCalls(false, true, false));
59 ->expects($this->any())
60 ->method('getRepository')
61 ->willReturn($entryRepo);
63 $res = $wallabagV1Import->import();
65 $this->assertTrue($res);
66 $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV1Import->getSummary());
69 public function testImportBadFile()
71 $wallabagV1Import = $this->getWallabagV1Import();
72 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
74 $res = $wallabagV1Import->import();
76 $this->assertFalse($res);
78 $records = $this->logHandler
->getRecords();
79 $this->assertContains('WallabagV1Import: unable to read file', $records[0]['message']);
80 $this->assertEquals('ERROR', $records[0]['level_name']);
83 public function testImportUserNotDefined()
85 $wallabagV1Import = $this->getWallabagV1Import(true);
86 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
88 $res = $wallabagV1Import->import();
90 $this->assertFalse($res);
92 $records = $this->logHandler
->getRecords();
93 $this->assertContains('WallabagV1Import: user is not defined', $records[0]['message']);
94 $this->assertEquals('ERROR', $records[0]['level_name']);