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