diff options
Diffstat (limited to 'tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php')
-rw-r--r-- | tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php | 138 |
1 files changed, 79 insertions, 59 deletions
diff --git a/tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php b/tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php index e262a808..274dc326 100644 --- a/tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php | |||
@@ -2,14 +2,14 @@ | |||
2 | 2 | ||
3 | namespace Tests\Wallabag\ImportBundle\Import; | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
4 | 4 | ||
5 | use Wallabag\ImportBundle\Import\InstapaperImport; | 5 | use M6Web\Component\RedisMock\RedisMockFactory; |
6 | use Wallabag\UserBundle\Entity\User; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
8 | use Wallabag\ImportBundle\Redis\Producer; | ||
9 | use Monolog\Logger; | ||
10 | use Monolog\Handler\TestHandler; | 6 | use Monolog\Handler\TestHandler; |
7 | use Monolog\Logger; | ||
11 | use Simpleue\Queue\RedisQueue; | 8 | use Simpleue\Queue\RedisQueue; |
12 | use M6Web\Component\RedisMock\RedisMockFactory; | 9 | use Wallabag\CoreBundle\Entity\Entry; |
10 | use Wallabag\ImportBundle\Import\InstapaperImport; | ||
11 | use Wallabag\ImportBundle\Redis\Producer; | ||
12 | use Wallabag\UserBundle\Entity\User; | ||
13 | 13 | ||
14 | class InstapaperImportTest extends \PHPUnit_Framework_TestCase | 14 | class InstapaperImportTest extends \PHPUnit_Framework_TestCase |
15 | { | 15 | { |
@@ -17,59 +17,28 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
17 | protected $em; | 17 | protected $em; |
18 | protected $logHandler; | 18 | protected $logHandler; |
19 | protected $contentProxy; | 19 | protected $contentProxy; |
20 | 20 | protected $tagsAssigner; | |
21 | private function getInstapaperImport($unsetUser = false, $dispatched = 0) | 21 | protected $uow; |
22 | { | ||
23 | $this->user = new User(); | ||
24 | |||
25 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | ||
26 | ->disableOriginalConstructor() | ||
27 | ->getMock(); | ||
28 | |||
29 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | ||
30 | ->disableOriginalConstructor() | ||
31 | ->getMock(); | ||
32 | |||
33 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') | ||
34 | ->disableOriginalConstructor() | ||
35 | ->getMock(); | ||
36 | |||
37 | $dispatcher | ||
38 | ->expects($this->exactly($dispatched)) | ||
39 | ->method('dispatch'); | ||
40 | |||
41 | $import = new InstapaperImport($this->em, $this->contentProxy, $dispatcher); | ||
42 | |||
43 | $this->logHandler = new TestHandler(); | ||
44 | $logger = new Logger('test', [$this->logHandler]); | ||
45 | $import->setLogger($logger); | ||
46 | |||
47 | if (false === $unsetUser) { | ||
48 | $import->setUser($this->user); | ||
49 | } | ||
50 | |||
51 | return $import; | ||
52 | } | ||
53 | 22 | ||
54 | public function testInit() | 23 | public function testInit() |
55 | { | 24 | { |
56 | $instapaperImport = $this->getInstapaperImport(); | 25 | $instapaperImport = $this->getInstapaperImport(); |
57 | 26 | ||
58 | $this->assertEquals('Instapaper', $instapaperImport->getName()); | 27 | $this->assertSame('Instapaper', $instapaperImport->getName()); |
59 | $this->assertNotEmpty($instapaperImport->getUrl()); | 28 | $this->assertNotEmpty($instapaperImport->getUrl()); |
60 | $this->assertEquals('import.instapaper.description', $instapaperImport->getDescription()); | 29 | $this->assertSame('import.instapaper.description', $instapaperImport->getDescription()); |
61 | } | 30 | } |
62 | 31 | ||
63 | public function testImport() | 32 | public function testImport() |
64 | { | 33 | { |
65 | $instapaperImport = $this->getInstapaperImport(false, 3); | 34 | $instapaperImport = $this->getInstapaperImport(false, 4); |
66 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | 35 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
67 | 36 | ||
68 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | 37 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') |
69 | ->disableOriginalConstructor() | 38 | ->disableOriginalConstructor() |
70 | ->getMock(); | 39 | ->getMock(); |
71 | 40 | ||
72 | $entryRepo->expects($this->exactly(3)) | 41 | $entryRepo->expects($this->exactly(4)) |
73 | ->method('findByUrlAndUserId') | 42 | ->method('findByUrlAndUserId') |
74 | ->willReturn(false); | 43 | ->willReturn(false); |
75 | 44 | ||
@@ -83,28 +52,28 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
83 | ->getMock(); | 52 | ->getMock(); |
84 | 53 | ||
85 | $this->contentProxy | 54 | $this->contentProxy |
86 | ->expects($this->exactly(3)) | 55 | ->expects($this->exactly(4)) |
87 | ->method('updateEntry') | 56 | ->method('updateEntry') |
88 | ->willReturn($entry); | 57 | ->willReturn($entry); |
89 | 58 | ||
90 | $res = $instapaperImport->import(); | 59 | $res = $instapaperImport->import(); |
91 | 60 | ||
92 | $this->assertTrue($res); | 61 | $this->assertTrue($res); |
93 | $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $instapaperImport->getSummary()); | 62 | $this->assertSame(['skipped' => 0, 'imported' => 4, 'queued' => 0], $instapaperImport->getSummary()); |
94 | } | 63 | } |
95 | 64 | ||
96 | public function testImportAndMarkAllAsRead() | 65 | public function testImportAndMarkAllAsRead() |
97 | { | 66 | { |
98 | $instapaperImport = $this->getInstapaperImport(false, 1); | 67 | $instapaperImport = $this->getInstapaperImport(false, 1); |
99 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | 68 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
100 | 69 | ||
101 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | 70 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') |
102 | ->disableOriginalConstructor() | 71 | ->disableOriginalConstructor() |
103 | ->getMock(); | 72 | ->getMock(); |
104 | 73 | ||
105 | $entryRepo->expects($this->exactly(3)) | 74 | $entryRepo->expects($this->exactly(4)) |
106 | ->method('findByUrlAndUserId') | 75 | ->method('findByUrlAndUserId') |
107 | ->will($this->onConsecutiveCalls(false, true, true)); | 76 | ->will($this->onConsecutiveCalls(false, true, true, true)); |
108 | 77 | ||
109 | $this->em | 78 | $this->em |
110 | ->expects($this->any()) | 79 | ->expects($this->any()) |
@@ -128,13 +97,13 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
128 | 97 | ||
129 | $this->assertTrue($res); | 98 | $this->assertTrue($res); |
130 | 99 | ||
131 | $this->assertEquals(['skipped' => 2, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary()); | 100 | $this->assertSame(['skipped' => 3, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary()); |
132 | } | 101 | } |
133 | 102 | ||
134 | public function testImportWithRabbit() | 103 | public function testImportWithRabbit() |
135 | { | 104 | { |
136 | $instapaperImport = $this->getInstapaperImport(); | 105 | $instapaperImport = $this->getInstapaperImport(); |
137 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | 106 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
138 | 107 | ||
139 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | 108 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') |
140 | ->disableOriginalConstructor() | 109 | ->disableOriginalConstructor() |
@@ -160,7 +129,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
160 | ->getMock(); | 129 | ->getMock(); |
161 | 130 | ||
162 | $producer | 131 | $producer |
163 | ->expects($this->exactly(3)) | 132 | ->expects($this->exactly(4)) |
164 | ->method('publish'); | 133 | ->method('publish'); |
165 | 134 | ||
166 | $instapaperImport->setProducer($producer); | 135 | $instapaperImport->setProducer($producer); |
@@ -168,13 +137,13 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
168 | $res = $instapaperImport->setMarkAsRead(true)->import(); | 137 | $res = $instapaperImport->setMarkAsRead(true)->import(); |
169 | 138 | ||
170 | $this->assertTrue($res); | 139 | $this->assertTrue($res); |
171 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 3], $instapaperImport->getSummary()); | 140 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary()); |
172 | } | 141 | } |
173 | 142 | ||
174 | public function testImportWithRedis() | 143 | public function testImportWithRedis() |
175 | { | 144 | { |
176 | $instapaperImport = $this->getInstapaperImport(); | 145 | $instapaperImport = $this->getInstapaperImport(); |
177 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | 146 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
178 | 147 | ||
179 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | 148 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') |
180 | ->disableOriginalConstructor() | 149 | ->disableOriginalConstructor() |
@@ -206,7 +175,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
206 | $res = $instapaperImport->setMarkAsRead(true)->import(); | 175 | $res = $instapaperImport->setMarkAsRead(true)->import(); |
207 | 176 | ||
208 | $this->assertTrue($res); | 177 | $this->assertTrue($res); |
209 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 3], $instapaperImport->getSummary()); | 178 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary()); |
210 | 179 | ||
211 | $this->assertNotEmpty($redisMock->lpop('instapaper')); | 180 | $this->assertNotEmpty($redisMock->lpop('instapaper')); |
212 | } | 181 | } |
@@ -214,7 +183,7 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
214 | public function testImportBadFile() | 183 | public function testImportBadFile() |
215 | { | 184 | { |
216 | $instapaperImport = $this->getInstapaperImport(); | 185 | $instapaperImport = $this->getInstapaperImport(); |
217 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | 186 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.jsonx'); |
218 | 187 | ||
219 | $res = $instapaperImport->import(); | 188 | $res = $instapaperImport->import(); |
220 | 189 | ||
@@ -222,13 +191,13 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
222 | 191 | ||
223 | $records = $this->logHandler->getRecords(); | 192 | $records = $this->logHandler->getRecords(); |
224 | $this->assertContains('InstapaperImport: unable to read file', $records[0]['message']); | 193 | $this->assertContains('InstapaperImport: unable to read file', $records[0]['message']); |
225 | $this->assertEquals('ERROR', $records[0]['level_name']); | 194 | $this->assertSame('ERROR', $records[0]['level_name']); |
226 | } | 195 | } |
227 | 196 | ||
228 | public function testImportUserNotDefined() | 197 | public function testImportUserNotDefined() |
229 | { | 198 | { |
230 | $instapaperImport = $this->getInstapaperImport(true); | 199 | $instapaperImport = $this->getInstapaperImport(true); |
231 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | 200 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
232 | 201 | ||
233 | $res = $instapaperImport->import(); | 202 | $res = $instapaperImport->import(); |
234 | 203 | ||
@@ -236,6 +205,57 @@ class InstapaperImportTest extends \PHPUnit_Framework_TestCase | |||
236 | 205 | ||
237 | $records = $this->logHandler->getRecords(); | 206 | $records = $this->logHandler->getRecords(); |
238 | $this->assertContains('InstapaperImport: user is not defined', $records[0]['message']); | 207 | $this->assertContains('InstapaperImport: user is not defined', $records[0]['message']); |
239 | $this->assertEquals('ERROR', $records[0]['level_name']); | 208 | $this->assertSame('ERROR', $records[0]['level_name']); |
209 | } | ||
210 | |||
211 | private function getInstapaperImport($unsetUser = false, $dispatched = 0) | ||
212 | { | ||
213 | $this->user = new User(); | ||
214 | |||
215 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | ||
216 | ->disableOriginalConstructor() | ||
217 | ->getMock(); | ||
218 | |||
219 | $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork') | ||
220 | ->disableOriginalConstructor() | ||
221 | ->getMock(); | ||
222 | |||
223 | $this->em | ||
224 | ->expects($this->any()) | ||
225 | ->method('getUnitOfWork') | ||
226 | ->willReturn($this->uow); | ||
227 | |||
228 | $this->uow | ||
229 | ->expects($this->any()) | ||
230 | ->method('getScheduledEntityInsertions') | ||
231 | ->willReturn([]); | ||
232 | |||
233 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | ||
234 | ->disableOriginalConstructor() | ||
235 | ->getMock(); | ||
236 | |||
237 | $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner') | ||
238 | ->disableOriginalConstructor() | ||
239 | ->getMock(); | ||
240 | |||
241 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') | ||
242 | ->disableOriginalConstructor() | ||
243 | ->getMock(); | ||
244 | |||
245 | $dispatcher | ||
246 | ->expects($this->exactly($dispatched)) | ||
247 | ->method('dispatch'); | ||
248 | |||
249 | $import = new InstapaperImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher); | ||
250 | |||
251 | $this->logHandler = new TestHandler(); | ||
252 | $logger = new Logger('test', [$this->logHandler]); | ||
253 | $import->setLogger($logger); | ||
254 | |||
255 | if (false === $unsetUser) { | ||
256 | $import->setUser($this->user); | ||
257 | } | ||
258 | |||
259 | return $import; | ||
240 | } | 260 | } |
241 | } | 261 | } |