]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php
d5b41777caf041f0f6b46b5571bf2f2eb21fd1e9
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Tests / Import / WallabagV1ImportTest.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Tests\Import;
4
5 use Wallabag\UserBundle\Entity\User;
6 use Wallabag\ImportBundle\Import\WallabagV1Import;
7 use Monolog\Logger;
8 use Monolog\Handler\TestHandler;
9
10 class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
11 {
12 protected $user;
13 protected $em;
14 protected $logHandler;
15
16 private function getWallabagV1Import($unsetUser = false)
17 {
18 $this->user = new User();
19
20 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
21 ->disableOriginalConstructor()
22 ->getMock();
23
24 $pocket = new WallabagV1Import($this->em);
25
26 $this->logHandler = new TestHandler();
27 $logger = new Logger('test', array($this->logHandler));
28 $pocket->setLogger($logger);
29
30 if (false === $unsetUser) {
31 $pocket->setUser($this->user);
32 }
33
34 return $pocket;
35 }
36
37 public function testInit()
38 {
39 $wallabagV1Import = $this->getWallabagV1Import();
40
41 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
42 $this->assertNotEmpty($wallabagV1Import->getUrl());
43 $this->assertContains('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription());
44 }
45
46 public function testImport()
47 {
48 $wallabagV1Import = $this->getWallabagV1Import();
49 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
50
51 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
52 ->disableOriginalConstructor()
53 ->getMock();
54
55 $entryRepo->expects($this->exactly(3))
56 ->method('existByUrlAndUserId')
57 ->will($this->onConsecutiveCalls(false, true, false));
58
59 $this->em
60 ->expects($this->any())
61 ->method('getRepository')
62 ->willReturn($entryRepo);
63
64 $res = $wallabagV1Import->import();
65
66 $this->assertTrue($res);
67 $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV1Import->getSummary());
68 }
69
70 public function testImportBadFile()
71 {
72 $wallabagV1Import = $this->getWallabagV1Import();
73 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
74
75 $res = $wallabagV1Import->import();
76
77 $this->assertFalse($res);
78
79 $records = $this->logHandler->getRecords();
80 $this->assertContains('WallabagV1Import: unable to read file', $records[0]['message']);
81 $this->assertEquals('ERROR', $records[0]['level_name']);
82 }
83
84 public function testImportUserNotDefined()
85 {
86 $wallabagV1Import = $this->getWallabagV1Import(true);
87 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
88
89 $res = $wallabagV1Import->import();
90
91 $this->assertFalse($res);
92
93 $records = $this->logHandler->getRecords();
94 $this->assertContains('WallabagV1Import: user is not defined', $records[0]['message']);
95 $this->assertEquals('ERROR', $records[0]['level_name']);
96 }
97 }