]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Tests\Wallabag\ImportBundle\Import; | |
4 | ||
5 | use Wallabag\ImportBundle\Import\WallabagV2Import; | |
6 | use Wallabag\UserBundle\Entity\User; | |
7 | use Wallabag\CoreBundle\Entity\Entry; | |
8 | use Monolog\Logger; | |
9 | use Monolog\Handler\TestHandler; | |
10 | ||
11 | class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |
12 | { | |
13 | protected $user; | |
14 | protected $em; | |
15 | protected $logHandler; | |
16 | protected $contentProxy; | |
17 | ||
18 | private function getWallabagV2Import($unsetUser = false) | |
19 | { | |
20 | $this->user = new User(); | |
21 | ||
22 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
23 | ->disableOriginalConstructor() | |
24 | ->getMock(); | |
25 | ||
26 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | |
27 | ->disableOriginalConstructor() | |
28 | ->getMock(); | |
29 | ||
30 | $wallabag = new WallabagV2Import($this->em, $this->contentProxy); | |
31 | ||
32 | $this->logHandler = new TestHandler(); | |
33 | $logger = new Logger('test', [$this->logHandler]); | |
34 | $wallabag->setLogger($logger); | |
35 | ||
36 | if (false === $unsetUser) { | |
37 | $wallabag->setUser($this->user); | |
38 | } | |
39 | ||
40 | return $wallabag; | |
41 | } | |
42 | ||
43 | public function testInit() | |
44 | { | |
45 | $wallabagV2Import = $this->getWallabagV2Import(); | |
46 | ||
47 | $this->assertEquals('wallabag v2', $wallabagV2Import->getName()); | |
48 | $this->assertNotEmpty($wallabagV2Import->getUrl()); | |
49 | $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription()); | |
50 | } | |
51 | ||
52 | public function testImport() | |
53 | { | |
54 | $wallabagV2Import = $this->getWallabagV2Import(); | |
55 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
56 | ||
57 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
58 | ->disableOriginalConstructor() | |
59 | ->getMock(); | |
60 | ||
61 | $entryRepo->expects($this->exactly(24)) | |
62 | ->method('findByUrlAndUserId') | |
63 | ->will($this->onConsecutiveCalls(false, true, false)); | |
64 | ||
65 | $this->em | |
66 | ->expects($this->any()) | |
67 | ->method('getRepository') | |
68 | ->willReturn($entryRepo); | |
69 | ||
70 | $this->contentProxy | |
71 | ->expects($this->exactly(2)) | |
72 | ->method('updateEntry') | |
73 | ->willReturn(new Entry($this->user)); | |
74 | ||
75 | $res = $wallabagV2Import->import(); | |
76 | ||
77 | $this->assertTrue($res); | |
78 | $this->assertEquals(['skipped' => 22, 'imported' => 2], $wallabagV2Import->getSummary()); | |
79 | } | |
80 | ||
81 | public function testImportAndMarkAllAsRead() | |
82 | { | |
83 | $wallabagV2Import = $this->getWallabagV2Import(); | |
84 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-read.json'); | |
85 | ||
86 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
87 | ->disableOriginalConstructor() | |
88 | ->getMock(); | |
89 | ||
90 | $entryRepo->expects($this->exactly(2)) | |
91 | ->method('findByUrlAndUserId') | |
92 | ->will($this->onConsecutiveCalls(false, false)); | |
93 | ||
94 | $this->em | |
95 | ->expects($this->any()) | |
96 | ->method('getRepository') | |
97 | ->willReturn($entryRepo); | |
98 | ||
99 | $this->contentProxy | |
100 | ->expects($this->exactly(2)) | |
101 | ->method('updateEntry') | |
102 | ->willReturn(new Entry($this->user)); | |
103 | ||
104 | // check that every entry persisted are archived | |
105 | $this->em | |
106 | ->expects($this->any()) | |
107 | ->method('persist') | |
108 | ->with($this->callback(function ($persistedEntry) { | |
109 | return $persistedEntry->isArchived(); | |
110 | })); | |
111 | ||
112 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | |
113 | ||
114 | $this->assertTrue($res); | |
115 | ||
116 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary()); | |
117 | } | |
118 | ||
119 | public function testImportBadFile() | |
120 | { | |
121 | $wallabagV1Import = $this->getWallabagV2Import(); | |
122 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.jsonx'); | |
123 | ||
124 | $res = $wallabagV1Import->import(); | |
125 | ||
126 | $this->assertFalse($res); | |
127 | ||
128 | $records = $this->logHandler->getRecords(); | |
129 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); | |
130 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
131 | } | |
132 | ||
133 | public function testImportUserNotDefined() | |
134 | { | |
135 | $wallabagV1Import = $this->getWallabagV2Import(true); | |
136 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
137 | ||
138 | $res = $wallabagV1Import->import(); | |
139 | ||
140 | $this->assertFalse($res); | |
141 | ||
142 | $records = $this->logHandler->getRecords(); | |
143 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); | |
144 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
145 | } | |
146 | ||
147 | public function testImportEmptyFile() | |
148 | { | |
149 | $wallabagV2Import = $this->getWallabagV2Import(); | |
150 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-empty.json'); | |
151 | ||
152 | $res = $wallabagV2Import->import(); | |
153 | ||
154 | $this->assertFalse($res); | |
155 | $this->assertEquals(['skipped' => 0, 'imported' => 0], $wallabagV2Import->getSummary()); | |
156 | } | |
157 | ||
158 | public function testImportWithExceptionFromGraby() | |
159 | { | |
160 | $wallabagV2Import = $this->getWallabagV2Import(); | |
161 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
162 | ||
163 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
164 | ->disableOriginalConstructor() | |
165 | ->getMock(); | |
166 | ||
167 | $entryRepo->expects($this->exactly(24)) | |
168 | ->method('findByUrlAndUserId') | |
169 | ->will($this->onConsecutiveCalls(false, true, false)); | |
170 | ||
171 | $this->em | |
172 | ->expects($this->any()) | |
173 | ->method('getRepository') | |
174 | ->willReturn($entryRepo); | |
175 | ||
176 | $this->contentProxy | |
177 | ->expects($this->exactly(2)) | |
178 | ->method('updateEntry') | |
179 | ->will($this->throwException(new \Exception())); | |
180 | ||
181 | $res = $wallabagV2Import->import(); | |
182 | ||
183 | $this->assertTrue($res); | |
184 | $this->assertEquals(['skipped' => 24, 'imported' => 0], $wallabagV2Import->getSummary()); | |
185 | } | |
186 | } |