]>
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; | |
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->assertEquals('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription()); | |
43 | } | |
44 | ||
45 | public function testImport() | |
46 | { | |
47 | $wallabagV1Import = $this->getWallabagV1Import(); | |
48 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | |
49 | ||
50 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
51 | ->disableOriginalConstructor() | |
52 | ->getMock(); | |
53 | ||
54 | $entryRepo->expects($this->exactly(3)) | |
55 | ->method('existByUrlAndUserId') | |
56 | ->will($this->onConsecutiveCalls(false, true, false)); | |
57 | ||
58 | $this->em | |
59 | ->expects($this->any()) | |
60 | ->method('getRepository') | |
61 | ->willReturn($entryRepo); | |
62 | ||
63 | $res = $wallabagV1Import->import(); | |
64 | ||
65 | $this->assertTrue($res); | |
66 | $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV1Import->getSummary()); | |
67 | } | |
68 | ||
69 | public function testImportBadFile() | |
70 | { | |
71 | $wallabagV1Import = $this->getWallabagV1Import(); | |
72 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | |
73 | ||
74 | $res = $wallabagV1Import->import(); | |
75 | ||
76 | $this->assertFalse($res); | |
77 | ||
78 | $records = $this->logHandler->getRecords(); | |
79 | $this->assertContains('WallabagV1Import: unable to read file', $records[0]['message']); | |
80 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
81 | } | |
82 | ||
83 | public function testImportUserNotDefined() | |
84 | { | |
85 | $wallabagV1Import = $this->getWallabagV1Import(true); | |
86 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | |
87 | ||
88 | $res = $wallabagV1Import->import(); | |
89 | ||
90 | $this->assertFalse($res); | |
91 | ||
92 | $records = $this->logHandler->getRecords(); | |
93 | $this->assertContains('WallabagV1Import: user is not defined', $records[0]['message']); | |
94 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
95 | } | |
96 | } |