]>
Commit | Line | Data |
---|---|---|
b1d05721 JB |
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; | |
0783c99a TC |
15 | protected $contentProxy; |
16 | ||
b1d05721 JB |
17 | |
18 | private function getWallabagV1Import($unsetUser = false) | |
19 | { | |
20 | $this->user = new User(); | |
21 | ||
22 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
23 | ->disableOriginalConstructor() | |
24 | ->getMock(); | |
25 | ||
0783c99a TC |
26 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
27 | ->disableOriginalConstructor() | |
28 | ->getMock(); | |
29 | ||
30 | $wallabag = new WallabagV1Import($this->em,$this->contentProxy); | |
b1d05721 JB |
31 | |
32 | $this->logHandler = new TestHandler(); | |
33 | $logger = new Logger('test', array($this->logHandler)); | |
6785f4aa | 34 | $wallabag->setLogger($logger); |
b1d05721 JB |
35 | |
36 | if (false === $unsetUser) { | |
6785f4aa | 37 | $wallabag->setUser($this->user); |
b1d05721 JB |
38 | } |
39 | ||
6785f4aa | 40 | return $wallabag; |
b1d05721 JB |
41 | } |
42 | ||
43 | public function testInit() | |
44 | { | |
45 | $wallabagV1Import = $this->getWallabagV1Import(); | |
46 | ||
b88cf91f | 47 | $this->assertEquals('wallabag v1', $wallabagV1Import->getName()); |
7019c7cf JB |
48 | $this->assertNotEmpty($wallabagV1Import->getUrl()); |
49 | $this->assertContains('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription()); | |
b1d05721 JB |
50 | } |
51 | ||
52 | public function testImport() | |
53 | { | |
54 | $wallabagV1Import = $this->getWallabagV1Import(); | |
55 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | |
56 | ||
57 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
58 | ->disableOriginalConstructor() | |
59 | ->getMock(); | |
60 | ||
61 | $entryRepo->expects($this->exactly(3)) | |
78833672 | 62 | ->method('findByUrlAndUserId') |
b1d05721 JB |
63 | ->will($this->onConsecutiveCalls(false, true, false)); |
64 | ||
65 | $this->em | |
66 | ->expects($this->any()) | |
67 | ->method('getRepository') | |
68 | ->willReturn($entryRepo); | |
69 | ||
70 | $res = $wallabagV1Import->import(); | |
71 | ||
72 | $this->assertTrue($res); | |
73 | $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV1Import->getSummary()); | |
74 | } | |
75 | ||
76 | public function testImportBadFile() | |
77 | { | |
78 | $wallabagV1Import = $this->getWallabagV1Import(); | |
79 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | |
80 | ||
81 | $res = $wallabagV1Import->import(); | |
82 | ||
83 | $this->assertFalse($res); | |
84 | ||
85 | $records = $this->logHandler->getRecords(); | |
6785f4aa | 86 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); |
b1d05721 JB |
87 | $this->assertEquals('ERROR', $records[0]['level_name']); |
88 | } | |
89 | ||
90 | public function testImportUserNotDefined() | |
91 | { | |
92 | $wallabagV1Import = $this->getWallabagV1Import(true); | |
93 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | |
94 | ||
95 | $res = $wallabagV1Import->import(); | |
96 | ||
97 | $this->assertFalse($res); | |
98 | ||
99 | $records = $this->logHandler->getRecords(); | |
6785f4aa | 100 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); |
b1d05721 JB |
101 | $this->assertEquals('ERROR', $records[0]['level_name']); |
102 | } | |
103 | } |