]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php
Merge pull request #1651 from wallabag/v2-fix-wllbgv2-import
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Tests / Import / WallabagV1ImportTest.php
CommitLineData
b1d05721
JB
1<?php
2
3namespace Wallabag\ImportBundle\Tests\Import;
4
5use Wallabag\UserBundle\Entity\User;
6use Wallabag\ImportBundle\Import\WallabagV1Import;
7use Monolog\Logger;
8use Monolog\Handler\TestHandler;
9
10class 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
6785f4aa 24 $wallabag = new WallabagV1Import($this->em);
b1d05721
JB
25
26 $this->logHandler = new TestHandler();
27 $logger = new Logger('test', array($this->logHandler));
6785f4aa 28 $wallabag->setLogger($logger);
b1d05721
JB
29
30 if (false === $unsetUser) {
6785f4aa 31 $wallabag->setUser($this->user);
b1d05721
JB
32 }
33
6785f4aa 34 return $wallabag;
b1d05721
JB
35 }
36
37 public function testInit()
38 {
39 $wallabagV1Import = $this->getWallabagV1Import();
40
b88cf91f 41 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
7019c7cf
JB
42 $this->assertNotEmpty($wallabagV1Import->getUrl());
43 $this->assertContains('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription());
b1d05721
JB
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))
78833672 56 ->method('findByUrlAndUserId')
b1d05721
JB
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();
6785f4aa 80 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
b1d05721
JB
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();
6785f4aa 94 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
b1d05721
JB
95 $this->assertEquals('ERROR', $records[0]['level_name']);
96 }
97}