diff options
Diffstat (limited to 'tests/Wallabag/ImportBundle/Import')
7 files changed, 1338 insertions, 40 deletions
diff --git a/tests/Wallabag/ImportBundle/Import/ChromeImportTest.php b/tests/Wallabag/ImportBundle/Import/ChromeImportTest.php new file mode 100644 index 00000000..1e52615c --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ChromeImportTest.php | |||
@@ -0,0 +1,233 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\ChromeImport; | ||
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; | ||
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
13 | |||
14 | class ChromeImportTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | protected $user; | ||
17 | protected $em; | ||
18 | protected $logHandler; | ||
19 | protected $contentProxy; | ||
20 | |||
21 | private function getChromeImport($unsetUser = false) | ||
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 | $wallabag = new ChromeImport($this->em, $this->contentProxy); | ||
34 | |||
35 | $this->logHandler = new TestHandler(); | ||
36 | $logger = new Logger('test', [$this->logHandler]); | ||
37 | $wallabag->setLogger($logger); | ||
38 | |||
39 | if (false === $unsetUser) { | ||
40 | $wallabag->setUser($this->user); | ||
41 | } | ||
42 | |||
43 | return $wallabag; | ||
44 | } | ||
45 | |||
46 | public function testInit() | ||
47 | { | ||
48 | $chromeImport = $this->getChromeImport(); | ||
49 | |||
50 | $this->assertEquals('Chrome', $chromeImport->getName()); | ||
51 | $this->assertNotEmpty($chromeImport->getUrl()); | ||
52 | $this->assertEquals('import.chrome.description', $chromeImport->getDescription()); | ||
53 | } | ||
54 | |||
55 | public function testImport() | ||
56 | { | ||
57 | $chromeImport = $this->getChromeImport(); | ||
58 | $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks'); | ||
59 | |||
60 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
61 | ->disableOriginalConstructor() | ||
62 | ->getMock(); | ||
63 | |||
64 | $entryRepo->expects($this->exactly(1)) | ||
65 | ->method('findByUrlAndUserId') | ||
66 | ->willReturn(false); | ||
67 | |||
68 | $this->em | ||
69 | ->expects($this->any()) | ||
70 | ->method('getRepository') | ||
71 | ->willReturn($entryRepo); | ||
72 | |||
73 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
74 | ->disableOriginalConstructor() | ||
75 | ->getMock(); | ||
76 | |||
77 | $this->contentProxy | ||
78 | ->expects($this->exactly(1)) | ||
79 | ->method('updateEntry') | ||
80 | ->willReturn($entry); | ||
81 | |||
82 | $res = $chromeImport->import(); | ||
83 | |||
84 | $this->assertTrue($res); | ||
85 | $this->assertEquals(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary()); | ||
86 | } | ||
87 | |||
88 | public function testImportAndMarkAllAsRead() | ||
89 | { | ||
90 | $chromeImport = $this->getChromeImport(); | ||
91 | $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks'); | ||
92 | |||
93 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
94 | ->disableOriginalConstructor() | ||
95 | ->getMock(); | ||
96 | |||
97 | $entryRepo->expects($this->exactly(1)) | ||
98 | ->method('findByUrlAndUserId') | ||
99 | ->will($this->onConsecutiveCalls(false, true)); | ||
100 | |||
101 | $this->em | ||
102 | ->expects($this->any()) | ||
103 | ->method('getRepository') | ||
104 | ->willReturn($entryRepo); | ||
105 | |||
106 | $this->contentProxy | ||
107 | ->expects($this->exactly(1)) | ||
108 | ->method('updateEntry') | ||
109 | ->willReturn(new Entry($this->user)); | ||
110 | |||
111 | // check that every entry persisted are archived | ||
112 | $this->em | ||
113 | ->expects($this->any()) | ||
114 | ->method('persist') | ||
115 | ->with($this->callback(function ($persistedEntry) { | ||
116 | return $persistedEntry->isArchived(); | ||
117 | })); | ||
118 | |||
119 | $res = $chromeImport->setMarkAsRead(true)->import(); | ||
120 | |||
121 | $this->assertTrue($res); | ||
122 | |||
123 | $this->assertEquals(['skipped' => 0, 'imported' => 1, 'queued' => 0], $chromeImport->getSummary()); | ||
124 | } | ||
125 | |||
126 | public function testImportWithRabbit() | ||
127 | { | ||
128 | $chromeImport = $this->getChromeImport(); | ||
129 | $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks'); | ||
130 | |||
131 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
132 | ->disableOriginalConstructor() | ||
133 | ->getMock(); | ||
134 | |||
135 | $entryRepo->expects($this->never()) | ||
136 | ->method('findByUrlAndUserId'); | ||
137 | |||
138 | $this->em | ||
139 | ->expects($this->never()) | ||
140 | ->method('getRepository'); | ||
141 | |||
142 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
143 | ->disableOriginalConstructor() | ||
144 | ->getMock(); | ||
145 | |||
146 | $this->contentProxy | ||
147 | ->expects($this->never()) | ||
148 | ->method('updateEntry'); | ||
149 | |||
150 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
151 | ->disableOriginalConstructor() | ||
152 | ->getMock(); | ||
153 | |||
154 | $producer | ||
155 | ->expects($this->exactly(1)) | ||
156 | ->method('publish'); | ||
157 | |||
158 | $chromeImport->setProducer($producer); | ||
159 | |||
160 | $res = $chromeImport->setMarkAsRead(true)->import(); | ||
161 | |||
162 | $this->assertTrue($res); | ||
163 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary()); | ||
164 | } | ||
165 | |||
166 | public function testImportWithRedis() | ||
167 | { | ||
168 | $chromeImport = $this->getChromeImport(); | ||
169 | $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks'); | ||
170 | |||
171 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
172 | ->disableOriginalConstructor() | ||
173 | ->getMock(); | ||
174 | |||
175 | $entryRepo->expects($this->never()) | ||
176 | ->method('findByUrlAndUserId'); | ||
177 | |||
178 | $this->em | ||
179 | ->expects($this->never()) | ||
180 | ->method('getRepository'); | ||
181 | |||
182 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
183 | ->disableOriginalConstructor() | ||
184 | ->getMock(); | ||
185 | |||
186 | $this->contentProxy | ||
187 | ->expects($this->never()) | ||
188 | ->method('updateEntry'); | ||
189 | |||
190 | $factory = new RedisMockFactory(); | ||
191 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
192 | |||
193 | $queue = new RedisQueue($redisMock, 'chrome'); | ||
194 | $producer = new Producer($queue); | ||
195 | |||
196 | $chromeImport->setProducer($producer); | ||
197 | |||
198 | $res = $chromeImport->setMarkAsRead(true)->import(); | ||
199 | |||
200 | $this->assertTrue($res); | ||
201 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $chromeImport->getSummary()); | ||
202 | |||
203 | $this->assertNotEmpty($redisMock->lpop('chrome')); | ||
204 | } | ||
205 | |||
206 | public function testImportBadFile() | ||
207 | { | ||
208 | $chromeImport = $this->getChromeImport(); | ||
209 | $chromeImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | ||
210 | |||
211 | $res = $chromeImport->import(); | ||
212 | |||
213 | $this->assertFalse($res); | ||
214 | |||
215 | $records = $this->logHandler->getRecords(); | ||
216 | $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']); | ||
217 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
218 | } | ||
219 | |||
220 | public function testImportUserNotDefined() | ||
221 | { | ||
222 | $chromeImport = $this->getChromeImport(true); | ||
223 | $chromeImport->setFilepath(__DIR__.'/../fixtures/chrome-bookmarks'); | ||
224 | |||
225 | $res = $chromeImport->import(); | ||
226 | |||
227 | $this->assertFalse($res); | ||
228 | |||
229 | $records = $this->logHandler->getRecords(); | ||
230 | $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']); | ||
231 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
232 | } | ||
233 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php b/tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php new file mode 100644 index 00000000..007dda6a --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/FirefoxImportTest.php | |||
@@ -0,0 +1,233 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\FirefoxImport; | ||
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; | ||
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
13 | |||
14 | class FirefoxImportTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | protected $user; | ||
17 | protected $em; | ||
18 | protected $logHandler; | ||
19 | protected $contentProxy; | ||
20 | |||
21 | private function getFirefoxImport($unsetUser = false) | ||
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 | $wallabag = new FirefoxImport($this->em, $this->contentProxy); | ||
34 | |||
35 | $this->logHandler = new TestHandler(); | ||
36 | $logger = new Logger('test', [$this->logHandler]); | ||
37 | $wallabag->setLogger($logger); | ||
38 | |||
39 | if (false === $unsetUser) { | ||
40 | $wallabag->setUser($this->user); | ||
41 | } | ||
42 | |||
43 | return $wallabag; | ||
44 | } | ||
45 | |||
46 | public function testInit() | ||
47 | { | ||
48 | $firefoxImport = $this->getFirefoxImport(); | ||
49 | |||
50 | $this->assertEquals('Firefox', $firefoxImport->getName()); | ||
51 | $this->assertNotEmpty($firefoxImport->getUrl()); | ||
52 | $this->assertEquals('import.firefox.description', $firefoxImport->getDescription()); | ||
53 | } | ||
54 | |||
55 | public function testImport() | ||
56 | { | ||
57 | $firefoxImport = $this->getFirefoxImport(); | ||
58 | $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json'); | ||
59 | |||
60 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
61 | ->disableOriginalConstructor() | ||
62 | ->getMock(); | ||
63 | |||
64 | $entryRepo->expects($this->exactly(2)) | ||
65 | ->method('findByUrlAndUserId') | ||
66 | ->willReturn(false); | ||
67 | |||
68 | $this->em | ||
69 | ->expects($this->any()) | ||
70 | ->method('getRepository') | ||
71 | ->willReturn($entryRepo); | ||
72 | |||
73 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
74 | ->disableOriginalConstructor() | ||
75 | ->getMock(); | ||
76 | |||
77 | $this->contentProxy | ||
78 | ->expects($this->exactly(2)) | ||
79 | ->method('updateEntry') | ||
80 | ->willReturn($entry); | ||
81 | |||
82 | $res = $firefoxImport->import(); | ||
83 | |||
84 | $this->assertTrue($res); | ||
85 | $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $firefoxImport->getSummary()); | ||
86 | } | ||
87 | |||
88 | public function testImportAndMarkAllAsRead() | ||
89 | { | ||
90 | $firefoxImport = $this->getFirefoxImport(); | ||
91 | $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json'); | ||
92 | |||
93 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
94 | ->disableOriginalConstructor() | ||
95 | ->getMock(); | ||
96 | |||
97 | $entryRepo->expects($this->exactly(2)) | ||
98 | ->method('findByUrlAndUserId') | ||
99 | ->will($this->onConsecutiveCalls(false, true)); | ||
100 | |||
101 | $this->em | ||
102 | ->expects($this->any()) | ||
103 | ->method('getRepository') | ||
104 | ->willReturn($entryRepo); | ||
105 | |||
106 | $this->contentProxy | ||
107 | ->expects($this->exactly(1)) | ||
108 | ->method('updateEntry') | ||
109 | ->willReturn(new Entry($this->user)); | ||
110 | |||
111 | // check that every entry persisted are archived | ||
112 | $this->em | ||
113 | ->expects($this->any()) | ||
114 | ->method('persist') | ||
115 | ->with($this->callback(function ($persistedEntry) { | ||
116 | return $persistedEntry->isArchived(); | ||
117 | })); | ||
118 | |||
119 | $res = $firefoxImport->setMarkAsRead(true)->import(); | ||
120 | |||
121 | $this->assertTrue($res); | ||
122 | |||
123 | $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $firefoxImport->getSummary()); | ||
124 | } | ||
125 | |||
126 | public function testImportWithRabbit() | ||
127 | { | ||
128 | $firefoxImport = $this->getFirefoxImport(); | ||
129 | $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json'); | ||
130 | |||
131 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
132 | ->disableOriginalConstructor() | ||
133 | ->getMock(); | ||
134 | |||
135 | $entryRepo->expects($this->never()) | ||
136 | ->method('findByUrlAndUserId'); | ||
137 | |||
138 | $this->em | ||
139 | ->expects($this->never()) | ||
140 | ->method('getRepository'); | ||
141 | |||
142 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
143 | ->disableOriginalConstructor() | ||
144 | ->getMock(); | ||
145 | |||
146 | $this->contentProxy | ||
147 | ->expects($this->never()) | ||
148 | ->method('updateEntry'); | ||
149 | |||
150 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
151 | ->disableOriginalConstructor() | ||
152 | ->getMock(); | ||
153 | |||
154 | $producer | ||
155 | ->expects($this->exactly(1)) | ||
156 | ->method('publish'); | ||
157 | |||
158 | $firefoxImport->setProducer($producer); | ||
159 | |||
160 | $res = $firefoxImport->setMarkAsRead(true)->import(); | ||
161 | |||
162 | $this->assertTrue($res); | ||
163 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary()); | ||
164 | } | ||
165 | |||
166 | public function testImportWithRedis() | ||
167 | { | ||
168 | $firefoxImport = $this->getFirefoxImport(); | ||
169 | $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json'); | ||
170 | |||
171 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
172 | ->disableOriginalConstructor() | ||
173 | ->getMock(); | ||
174 | |||
175 | $entryRepo->expects($this->never()) | ||
176 | ->method('findByUrlAndUserId'); | ||
177 | |||
178 | $this->em | ||
179 | ->expects($this->never()) | ||
180 | ->method('getRepository'); | ||
181 | |||
182 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
183 | ->disableOriginalConstructor() | ||
184 | ->getMock(); | ||
185 | |||
186 | $this->contentProxy | ||
187 | ->expects($this->never()) | ||
188 | ->method('updateEntry'); | ||
189 | |||
190 | $factory = new RedisMockFactory(); | ||
191 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
192 | |||
193 | $queue = new RedisQueue($redisMock, 'firefox'); | ||
194 | $producer = new Producer($queue); | ||
195 | |||
196 | $firefoxImport->setProducer($producer); | ||
197 | |||
198 | $res = $firefoxImport->setMarkAsRead(true)->import(); | ||
199 | |||
200 | $this->assertTrue($res); | ||
201 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $firefoxImport->getSummary()); | ||
202 | |||
203 | $this->assertNotEmpty($redisMock->lpop('firefox')); | ||
204 | } | ||
205 | |||
206 | public function testImportBadFile() | ||
207 | { | ||
208 | $firefoxImport = $this->getFirefoxImport(); | ||
209 | $firefoxImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | ||
210 | |||
211 | $res = $firefoxImport->import(); | ||
212 | |||
213 | $this->assertFalse($res); | ||
214 | |||
215 | $records = $this->logHandler->getRecords(); | ||
216 | $this->assertContains('Wallabag Browser Import: unable to read file', $records[0]['message']); | ||
217 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
218 | } | ||
219 | |||
220 | public function testImportUserNotDefined() | ||
221 | { | ||
222 | $firefoxImport = $this->getFirefoxImport(true); | ||
223 | $firefoxImport->setFilepath(__DIR__.'/../fixtures/firefox-bookmarks.json'); | ||
224 | |||
225 | $res = $firefoxImport->import(); | ||
226 | |||
227 | $this->assertFalse($res); | ||
228 | |||
229 | $records = $this->logHandler->getRecords(); | ||
230 | $this->assertContains('Wallabag Browser Import: user is not defined', $records[0]['message']); | ||
231 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
232 | } | ||
233 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php b/tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php new file mode 100644 index 00000000..75900bd7 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php | |||
@@ -0,0 +1,233 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\InstapaperImport; | ||
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; | ||
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
13 | |||
14 | class InstapaperImportTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | protected $user; | ||
17 | protected $em; | ||
18 | protected $logHandler; | ||
19 | protected $contentProxy; | ||
20 | |||
21 | private function getInstapaperImport($unsetUser = false) | ||
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 | $import = new InstapaperImport($this->em, $this->contentProxy); | ||
34 | |||
35 | $this->logHandler = new TestHandler(); | ||
36 | $logger = new Logger('test', [$this->logHandler]); | ||
37 | $import->setLogger($logger); | ||
38 | |||
39 | if (false === $unsetUser) { | ||
40 | $import->setUser($this->user); | ||
41 | } | ||
42 | |||
43 | return $import; | ||
44 | } | ||
45 | |||
46 | public function testInit() | ||
47 | { | ||
48 | $instapaperImport = $this->getInstapaperImport(); | ||
49 | |||
50 | $this->assertEquals('Instapaper', $instapaperImport->getName()); | ||
51 | $this->assertNotEmpty($instapaperImport->getUrl()); | ||
52 | $this->assertEquals('import.instapaper.description', $instapaperImport->getDescription()); | ||
53 | } | ||
54 | |||
55 | public function testImport() | ||
56 | { | ||
57 | $instapaperImport = $this->getInstapaperImport(); | ||
58 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | ||
59 | |||
60 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
61 | ->disableOriginalConstructor() | ||
62 | ->getMock(); | ||
63 | |||
64 | $entryRepo->expects($this->exactly(3)) | ||
65 | ->method('findByUrlAndUserId') | ||
66 | ->willReturn(false); | ||
67 | |||
68 | $this->em | ||
69 | ->expects($this->any()) | ||
70 | ->method('getRepository') | ||
71 | ->willReturn($entryRepo); | ||
72 | |||
73 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
74 | ->disableOriginalConstructor() | ||
75 | ->getMock(); | ||
76 | |||
77 | $this->contentProxy | ||
78 | ->expects($this->exactly(3)) | ||
79 | ->method('updateEntry') | ||
80 | ->willReturn($entry); | ||
81 | |||
82 | $res = $instapaperImport->import(); | ||
83 | |||
84 | $this->assertTrue($res); | ||
85 | $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $instapaperImport->getSummary()); | ||
86 | } | ||
87 | |||
88 | public function testImportAndMarkAllAsRead() | ||
89 | { | ||
90 | $instapaperImport = $this->getInstapaperImport(); | ||
91 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | ||
92 | |||
93 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
94 | ->disableOriginalConstructor() | ||
95 | ->getMock(); | ||
96 | |||
97 | $entryRepo->expects($this->exactly(3)) | ||
98 | ->method('findByUrlAndUserId') | ||
99 | ->will($this->onConsecutiveCalls(false, true, true)); | ||
100 | |||
101 | $this->em | ||
102 | ->expects($this->any()) | ||
103 | ->method('getRepository') | ||
104 | ->willReturn($entryRepo); | ||
105 | |||
106 | $this->contentProxy | ||
107 | ->expects($this->once()) | ||
108 | ->method('updateEntry') | ||
109 | ->willReturn(new Entry($this->user)); | ||
110 | |||
111 | // check that every entry persisted are archived | ||
112 | $this->em | ||
113 | ->expects($this->once()) | ||
114 | ->method('persist') | ||
115 | ->with($this->callback(function ($persistedEntry) { | ||
116 | return $persistedEntry->isArchived(); | ||
117 | })); | ||
118 | |||
119 | $res = $instapaperImport->setMarkAsRead(true)->import(); | ||
120 | |||
121 | $this->assertTrue($res); | ||
122 | |||
123 | $this->assertEquals(['skipped' => 2, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary()); | ||
124 | } | ||
125 | |||
126 | public function testImportWithRabbit() | ||
127 | { | ||
128 | $instapaperImport = $this->getInstapaperImport(); | ||
129 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | ||
130 | |||
131 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
132 | ->disableOriginalConstructor() | ||
133 | ->getMock(); | ||
134 | |||
135 | $entryRepo->expects($this->never()) | ||
136 | ->method('findByUrlAndUserId'); | ||
137 | |||
138 | $this->em | ||
139 | ->expects($this->never()) | ||
140 | ->method('getRepository'); | ||
141 | |||
142 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
143 | ->disableOriginalConstructor() | ||
144 | ->getMock(); | ||
145 | |||
146 | $this->contentProxy | ||
147 | ->expects($this->never()) | ||
148 | ->method('updateEntry'); | ||
149 | |||
150 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
151 | ->disableOriginalConstructor() | ||
152 | ->getMock(); | ||
153 | |||
154 | $producer | ||
155 | ->expects($this->exactly(3)) | ||
156 | ->method('publish'); | ||
157 | |||
158 | $instapaperImport->setProducer($producer); | ||
159 | |||
160 | $res = $instapaperImport->setMarkAsRead(true)->import(); | ||
161 | |||
162 | $this->assertTrue($res); | ||
163 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 3], $instapaperImport->getSummary()); | ||
164 | } | ||
165 | |||
166 | public function testImportWithRedis() | ||
167 | { | ||
168 | $instapaperImport = $this->getInstapaperImport(); | ||
169 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | ||
170 | |||
171 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
172 | ->disableOriginalConstructor() | ||
173 | ->getMock(); | ||
174 | |||
175 | $entryRepo->expects($this->never()) | ||
176 | ->method('findByUrlAndUserId'); | ||
177 | |||
178 | $this->em | ||
179 | ->expects($this->never()) | ||
180 | ->method('getRepository'); | ||
181 | |||
182 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
183 | ->disableOriginalConstructor() | ||
184 | ->getMock(); | ||
185 | |||
186 | $this->contentProxy | ||
187 | ->expects($this->never()) | ||
188 | ->method('updateEntry'); | ||
189 | |||
190 | $factory = new RedisMockFactory(); | ||
191 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
192 | |||
193 | $queue = new RedisQueue($redisMock, 'instapaper'); | ||
194 | $producer = new Producer($queue); | ||
195 | |||
196 | $instapaperImport->setProducer($producer); | ||
197 | |||
198 | $res = $instapaperImport->setMarkAsRead(true)->import(); | ||
199 | |||
200 | $this->assertTrue($res); | ||
201 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 3], $instapaperImport->getSummary()); | ||
202 | |||
203 | $this->assertNotEmpty($redisMock->lpop('instapaper')); | ||
204 | } | ||
205 | |||
206 | public function testImportBadFile() | ||
207 | { | ||
208 | $instapaperImport = $this->getInstapaperImport(); | ||
209 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | ||
210 | |||
211 | $res = $instapaperImport->import(); | ||
212 | |||
213 | $this->assertFalse($res); | ||
214 | |||
215 | $records = $this->logHandler->getRecords(); | ||
216 | $this->assertContains('InstapaperImport: unable to read file', $records[0]['message']); | ||
217 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
218 | } | ||
219 | |||
220 | public function testImportUserNotDefined() | ||
221 | { | ||
222 | $instapaperImport = $this->getInstapaperImport(true); | ||
223 | $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv'); | ||
224 | |||
225 | $res = $instapaperImport->import(); | ||
226 | |||
227 | $this->assertFalse($res); | ||
228 | |||
229 | $records = $this->logHandler->getRecords(); | ||
230 | $this->assertContains('InstapaperImport: user is not defined', $records[0]['message']); | ||
231 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
232 | } | ||
233 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php index 8534e1c8..9ec7935c 100644 --- a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php | |||
@@ -4,21 +4,17 @@ namespace Tests\Wallabag\ImportBundle\Import; | |||
4 | 4 | ||
5 | use Wallabag\UserBundle\Entity\User; | 5 | use Wallabag\UserBundle\Entity\User; |
6 | use Wallabag\CoreBundle\Entity\Entry; | 6 | use Wallabag\CoreBundle\Entity\Entry; |
7 | use Wallabag\CoreBundle\Entity\Config; | ||
7 | use Wallabag\ImportBundle\Import\PocketImport; | 8 | use Wallabag\ImportBundle\Import\PocketImport; |
8 | use GuzzleHttp\Client; | 9 | use GuzzleHttp\Client; |
9 | use GuzzleHttp\Subscriber\Mock; | 10 | use GuzzleHttp\Subscriber\Mock; |
10 | use GuzzleHttp\Message\Response; | 11 | use GuzzleHttp\Message\Response; |
11 | use GuzzleHttp\Stream\Stream; | 12 | use GuzzleHttp\Stream\Stream; |
13 | use Wallabag\ImportBundle\Redis\Producer; | ||
12 | use Monolog\Logger; | 14 | use Monolog\Logger; |
13 | use Monolog\Handler\TestHandler; | 15 | use Monolog\Handler\TestHandler; |
14 | 16 | use Simpleue\Queue\RedisQueue; | |
15 | class PocketImportMock extends PocketImport | 17 | use M6Web\Component\RedisMock\RedisMockFactory; |
16 | { | ||
17 | public function getAccessToken() | ||
18 | { | ||
19 | return $this->accessToken; | ||
20 | } | ||
21 | } | ||
22 | 18 | ||
23 | class PocketImportTest extends \PHPUnit_Framework_TestCase | 19 | class PocketImportTest extends \PHPUnit_Framework_TestCase |
24 | { | 20 | { |
@@ -32,45 +28,38 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
32 | { | 28 | { |
33 | $this->user = new User(); | 29 | $this->user = new User(); |
34 | 30 | ||
35 | $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') | 31 | $config = new Config($this->user); |
36 | ->disableOriginalConstructor() | 32 | $config->setPocketConsumerKey('xxx'); |
37 | ->getMock(); | ||
38 | 33 | ||
39 | $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') | 34 | $this->user->setConfig($config); |
40 | ->disableOriginalConstructor() | ||
41 | ->getMock(); | ||
42 | 35 | ||
43 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | 36 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
44 | ->disableOriginalConstructor() | 37 | ->disableOriginalConstructor() |
45 | ->getMock(); | 38 | ->getMock(); |
46 | 39 | ||
47 | $token->expects($this->once()) | ||
48 | ->method('getUser') | ||
49 | ->willReturn($this->user); | ||
50 | |||
51 | $this->tokenStorage->expects($this->once()) | ||
52 | ->method('getToken') | ||
53 | ->willReturn($token); | ||
54 | |||
55 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | 40 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
56 | ->disableOriginalConstructor() | 41 | ->disableOriginalConstructor() |
57 | ->getMock(); | 42 | ->getMock(); |
58 | 43 | ||
59 | $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config') | 44 | $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork') |
60 | ->disableOriginalConstructor() | 45 | ->disableOriginalConstructor() |
61 | ->getMock(); | 46 | ->getMock(); |
62 | 47 | ||
63 | $config->expects($this->any()) | 48 | $this->em |
64 | ->method('get') | 49 | ->expects($this->any()) |
65 | ->with('pocket_consumer_key') | 50 | ->method('getUnitOfWork') |
66 | ->willReturn($consumerKey); | 51 | ->willReturn($this->uow); |
52 | |||
53 | $this->uow | ||
54 | ->expects($this->any()) | ||
55 | ->method('getScheduledEntityInsertions') | ||
56 | ->willReturn([]); | ||
67 | 57 | ||
68 | $pocket = new PocketImportMock( | 58 | $pocket = new PocketImport( |
69 | $this->tokenStorage, | ||
70 | $this->em, | 59 | $this->em, |
71 | $this->contentProxy, | 60 | $this->contentProxy |
72 | $config | ||
73 | ); | 61 | ); |
62 | $pocket->setUser($this->user); | ||
74 | 63 | ||
75 | $this->logHandler = new TestHandler(); | 64 | $this->logHandler = new TestHandler(); |
76 | $logger = new Logger('test', [$this->logHandler]); | 65 | $logger = new Logger('test', [$this->logHandler]); |
@@ -189,10 +178,16 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
189 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 178 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
190 | "favorite": "1", | 179 | "favorite": "1", |
191 | "status": "1", | 180 | "status": "1", |
181 | "time_added": "1473020899", | ||
182 | "time_updated": "1473020899", | ||
183 | "time_read": "0", | ||
184 | "time_favorited": "0", | ||
185 | "sort_id": 0, | ||
192 | "resolved_title": "The Massive Ryder Cup Preview", | 186 | "resolved_title": "The Massive Ryder Cup Preview", |
193 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 187 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", |
194 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 188 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
195 | "is_article": "1", | 189 | "is_article": "1", |
190 | "is_index": "0", | ||
196 | "has_video": "1", | 191 | "has_video": "1", |
197 | "has_image": "1", | 192 | "has_image": "1", |
198 | "word_count": "3197", | 193 | "word_count": "3197", |
@@ -236,10 +231,16 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
236 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 231 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
237 | "favorite": "1", | 232 | "favorite": "1", |
238 | "status": "1", | 233 | "status": "1", |
234 | "time_added": "1473020899", | ||
235 | "time_updated": "1473020899", | ||
236 | "time_read": "0", | ||
237 | "time_favorited": "0", | ||
238 | "sort_id": 1, | ||
239 | "resolved_title": "The Massive Ryder Cup Preview", | 239 | "resolved_title": "The Massive Ryder Cup Preview", |
240 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 240 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", |
241 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 241 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
242 | "is_article": "1", | 242 | "is_article": "1", |
243 | "is_index": "0", | ||
243 | "has_video": "0", | 244 | "has_video": "0", |
244 | "has_image": "0", | 245 | "has_image": "0", |
245 | "word_count": "3197" | 246 | "word_count": "3197" |
@@ -279,7 +280,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
279 | $res = $pocketImport->import(); | 280 | $res = $pocketImport->import(); |
280 | 281 | ||
281 | $this->assertTrue($res); | 282 | $this->assertTrue($res); |
282 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); | 283 | $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary()); |
283 | } | 284 | } |
284 | 285 | ||
285 | /** | 286 | /** |
@@ -302,6 +303,11 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
302 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 303 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
303 | "favorite": "1", | 304 | "favorite": "1", |
304 | "status": "1", | 305 | "status": "1", |
306 | "time_added": "1473020899", | ||
307 | "time_updated": "1473020899", | ||
308 | "time_read": "0", | ||
309 | "time_favorited": "0", | ||
310 | "sort_id": 0, | ||
305 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 311 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
306 | "is_article": "1", | 312 | "is_article": "1", |
307 | "has_video": "1", | 313 | "has_video": "1", |
@@ -315,6 +321,11 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
315 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 321 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
316 | "favorite": "1", | 322 | "favorite": "1", |
317 | "status": "0", | 323 | "status": "0", |
324 | "time_added": "1473020899", | ||
325 | "time_updated": "1473020899", | ||
326 | "time_read": "0", | ||
327 | "time_favorited": "0", | ||
328 | "sort_id": 1, | ||
318 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | 329 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
319 | "is_article": "1", | 330 | "is_article": "1", |
320 | "has_video": "0", | 331 | "has_video": "0", |
@@ -364,7 +375,174 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
364 | $res = $pocketImport->setMarkAsRead(true)->import(); | 375 | $res = $pocketImport->setMarkAsRead(true)->import(); |
365 | 376 | ||
366 | $this->assertTrue($res); | 377 | $this->assertTrue($res); |
367 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary()); | 378 | $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $pocketImport->getSummary()); |
379 | } | ||
380 | |||
381 | /** | ||
382 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
383 | */ | ||
384 | public function testImportWithRabbit() | ||
385 | { | ||
386 | $client = new Client(); | ||
387 | |||
388 | $body = <<<'JSON' | ||
389 | { | ||
390 | "item_id": "229279689", | ||
391 | "resolved_id": "229279689", | ||
392 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
393 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
394 | "favorite": "1", | ||
395 | "status": "1", | ||
396 | "time_added": "1473020899", | ||
397 | "time_updated": "1473020899", | ||
398 | "time_read": "0", | ||
399 | "time_favorited": "0", | ||
400 | "sort_id": 0, | ||
401 | "resolved_title": "The Massive Ryder Cup Preview", | ||
402 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
403 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | ||
404 | "is_article": "1", | ||
405 | "has_video": "0", | ||
406 | "has_image": "0", | ||
407 | "word_count": "3197" | ||
408 | } | ||
409 | JSON; | ||
410 | |||
411 | $mock = new Mock([ | ||
412 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
413 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
414 | { | ||
415 | "status": 1, | ||
416 | "list": { | ||
417 | "229279690": '.$body.' | ||
418 | } | ||
419 | } | ||
420 | ')), | ||
421 | ]); | ||
422 | |||
423 | $client->getEmitter()->attach($mock); | ||
424 | |||
425 | $pocketImport = $this->getPocketImport(); | ||
426 | |||
427 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
428 | ->disableOriginalConstructor() | ||
429 | ->getMock(); | ||
430 | |||
431 | $entryRepo->expects($this->never()) | ||
432 | ->method('findByUrlAndUserId'); | ||
433 | |||
434 | $this->em | ||
435 | ->expects($this->never()) | ||
436 | ->method('getRepository'); | ||
437 | |||
438 | $entry = new Entry($this->user); | ||
439 | |||
440 | $this->contentProxy | ||
441 | ->expects($this->never()) | ||
442 | ->method('updateEntry'); | ||
443 | |||
444 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
445 | ->disableOriginalConstructor() | ||
446 | ->getMock(); | ||
447 | |||
448 | $bodyAsArray = json_decode($body, true); | ||
449 | // because with just use `new User()` so it doesn't have an id | ||
450 | $bodyAsArray['userId'] = null; | ||
451 | |||
452 | $producer | ||
453 | ->expects($this->once()) | ||
454 | ->method('publish') | ||
455 | ->with(json_encode($bodyAsArray)); | ||
456 | |||
457 | $pocketImport->setClient($client); | ||
458 | $pocketImport->setProducer($producer); | ||
459 | $pocketImport->authorize('wunderbar_code'); | ||
460 | |||
461 | $res = $pocketImport->setMarkAsRead(true)->import(); | ||
462 | |||
463 | $this->assertTrue($res); | ||
464 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary()); | ||
465 | } | ||
466 | |||
467 | /** | ||
468 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
469 | */ | ||
470 | public function testImportWithRedis() | ||
471 | { | ||
472 | $client = new Client(); | ||
473 | |||
474 | $body = <<<'JSON' | ||
475 | { | ||
476 | "item_id": "229279689", | ||
477 | "resolved_id": "229279689", | ||
478 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
479 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
480 | "favorite": "1", | ||
481 | "status": "1", | ||
482 | "time_added": "1473020899", | ||
483 | "time_updated": "1473020899", | ||
484 | "time_read": "0", | ||
485 | "time_favorited": "0", | ||
486 | "sort_id": 0, | ||
487 | "resolved_title": "The Massive Ryder Cup Preview", | ||
488 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
489 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | ||
490 | "is_article": "1", | ||
491 | "has_video": "0", | ||
492 | "has_image": "0", | ||
493 | "word_count": "3197" | ||
494 | } | ||
495 | JSON; | ||
496 | |||
497 | $mock = new Mock([ | ||
498 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
499 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
500 | { | ||
501 | "status": 1, | ||
502 | "list": { | ||
503 | "229279690": '.$body.' | ||
504 | } | ||
505 | } | ||
506 | ')), | ||
507 | ]); | ||
508 | |||
509 | $client->getEmitter()->attach($mock); | ||
510 | |||
511 | $pocketImport = $this->getPocketImport(); | ||
512 | |||
513 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
514 | ->disableOriginalConstructor() | ||
515 | ->getMock(); | ||
516 | |||
517 | $entryRepo->expects($this->never()) | ||
518 | ->method('findByUrlAndUserId'); | ||
519 | |||
520 | $this->em | ||
521 | ->expects($this->never()) | ||
522 | ->method('getRepository'); | ||
523 | |||
524 | $entry = new Entry($this->user); | ||
525 | |||
526 | $this->contentProxy | ||
527 | ->expects($this->never()) | ||
528 | ->method('updateEntry'); | ||
529 | |||
530 | $factory = new RedisMockFactory(); | ||
531 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
532 | |||
533 | $queue = new RedisQueue($redisMock, 'pocket'); | ||
534 | $producer = new Producer($queue); | ||
535 | |||
536 | $pocketImport->setClient($client); | ||
537 | $pocketImport->setProducer($producer); | ||
538 | $pocketImport->authorize('wunderbar_code'); | ||
539 | |||
540 | $res = $pocketImport->setMarkAsRead(true)->import(); | ||
541 | |||
542 | $this->assertTrue($res); | ||
543 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary()); | ||
544 | |||
545 | $this->assertNotEmpty($redisMock->lpop('pocket')); | ||
368 | } | 546 | } |
369 | 547 | ||
370 | public function testImportBadResponse() | 548 | public function testImportBadResponse() |
@@ -402,6 +580,8 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
402 | "status": 1, | 580 | "status": 1, |
403 | "list": { | 581 | "list": { |
404 | "229279689": { | 582 | "229279689": { |
583 | "status": "1", | ||
584 | "favorite": "1", | ||
405 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" | 585 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" |
406 | } | 586 | } |
407 | } | 587 | } |
@@ -439,6 +619,6 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
439 | $res = $pocketImport->import(); | 619 | $res = $pocketImport->import(); |
440 | 620 | ||
441 | $this->assertTrue($res); | 621 | $this->assertTrue($res); |
442 | $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary()); | 622 | $this->assertEquals(['skipped' => 0, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary()); |
443 | } | 623 | } |
444 | } | 624 | } |
diff --git a/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php new file mode 100644 index 00000000..d98cd486 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php | |||
@@ -0,0 +1,233 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\ReadabilityImport; | ||
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; | ||
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
13 | |||
14 | class ReadabilityImportTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | protected $user; | ||
17 | protected $em; | ||
18 | protected $logHandler; | ||
19 | protected $contentProxy; | ||
20 | |||
21 | private function getReadabilityImport($unsetUser = false) | ||
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 | $wallabag = new ReadabilityImport($this->em, $this->contentProxy); | ||
34 | |||
35 | $this->logHandler = new TestHandler(); | ||
36 | $logger = new Logger('test', [$this->logHandler]); | ||
37 | $wallabag->setLogger($logger); | ||
38 | |||
39 | if (false === $unsetUser) { | ||
40 | $wallabag->setUser($this->user); | ||
41 | } | ||
42 | |||
43 | return $wallabag; | ||
44 | } | ||
45 | |||
46 | public function testInit() | ||
47 | { | ||
48 | $readabilityImport = $this->getReadabilityImport(); | ||
49 | |||
50 | $this->assertEquals('Readability', $readabilityImport->getName()); | ||
51 | $this->assertNotEmpty($readabilityImport->getUrl()); | ||
52 | $this->assertEquals('import.readability.description', $readabilityImport->getDescription()); | ||
53 | } | ||
54 | |||
55 | public function testImport() | ||
56 | { | ||
57 | $readabilityImport = $this->getReadabilityImport(); | ||
58 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
59 | |||
60 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
61 | ->disableOriginalConstructor() | ||
62 | ->getMock(); | ||
63 | |||
64 | $entryRepo->expects($this->exactly(24)) | ||
65 | ->method('findByUrlAndUserId') | ||
66 | ->willReturn(false); | ||
67 | |||
68 | $this->em | ||
69 | ->expects($this->any()) | ||
70 | ->method('getRepository') | ||
71 | ->willReturn($entryRepo); | ||
72 | |||
73 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
74 | ->disableOriginalConstructor() | ||
75 | ->getMock(); | ||
76 | |||
77 | $this->contentProxy | ||
78 | ->expects($this->exactly(24)) | ||
79 | ->method('updateEntry') | ||
80 | ->willReturn($entry); | ||
81 | |||
82 | $res = $readabilityImport->import(); | ||
83 | |||
84 | $this->assertTrue($res); | ||
85 | $this->assertEquals(['skipped' => 0, 'imported' => 24, 'queued' => 0], $readabilityImport->getSummary()); | ||
86 | } | ||
87 | |||
88 | public function testImportAndMarkAllAsRead() | ||
89 | { | ||
90 | $readabilityImport = $this->getReadabilityImport(); | ||
91 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability-read.json'); | ||
92 | |||
93 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
94 | ->disableOriginalConstructor() | ||
95 | ->getMock(); | ||
96 | |||
97 | $entryRepo->expects($this->exactly(2)) | ||
98 | ->method('findByUrlAndUserId') | ||
99 | ->will($this->onConsecutiveCalls(false, true)); | ||
100 | |||
101 | $this->em | ||
102 | ->expects($this->any()) | ||
103 | ->method('getRepository') | ||
104 | ->willReturn($entryRepo); | ||
105 | |||
106 | $this->contentProxy | ||
107 | ->expects($this->exactly(1)) | ||
108 | ->method('updateEntry') | ||
109 | ->willReturn(new Entry($this->user)); | ||
110 | |||
111 | // check that every entry persisted are archived | ||
112 | $this->em | ||
113 | ->expects($this->any()) | ||
114 | ->method('persist') | ||
115 | ->with($this->callback(function ($persistedEntry) { | ||
116 | return $persistedEntry->isArchived(); | ||
117 | })); | ||
118 | |||
119 | $res = $readabilityImport->setMarkAsRead(true)->import(); | ||
120 | |||
121 | $this->assertTrue($res); | ||
122 | |||
123 | $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $readabilityImport->getSummary()); | ||
124 | } | ||
125 | |||
126 | public function testImportWithRabbit() | ||
127 | { | ||
128 | $readabilityImport = $this->getReadabilityImport(); | ||
129 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
130 | |||
131 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
132 | ->disableOriginalConstructor() | ||
133 | ->getMock(); | ||
134 | |||
135 | $entryRepo->expects($this->never()) | ||
136 | ->method('findByUrlAndUserId'); | ||
137 | |||
138 | $this->em | ||
139 | ->expects($this->never()) | ||
140 | ->method('getRepository'); | ||
141 | |||
142 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
143 | ->disableOriginalConstructor() | ||
144 | ->getMock(); | ||
145 | |||
146 | $this->contentProxy | ||
147 | ->expects($this->never()) | ||
148 | ->method('updateEntry'); | ||
149 | |||
150 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
151 | ->disableOriginalConstructor() | ||
152 | ->getMock(); | ||
153 | |||
154 | $producer | ||
155 | ->expects($this->exactly(24)) | ||
156 | ->method('publish'); | ||
157 | |||
158 | $readabilityImport->setProducer($producer); | ||
159 | |||
160 | $res = $readabilityImport->setMarkAsRead(true)->import(); | ||
161 | |||
162 | $this->assertTrue($res); | ||
163 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $readabilityImport->getSummary()); | ||
164 | } | ||
165 | |||
166 | public function testImportWithRedis() | ||
167 | { | ||
168 | $readabilityImport = $this->getReadabilityImport(); | ||
169 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
170 | |||
171 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
172 | ->disableOriginalConstructor() | ||
173 | ->getMock(); | ||
174 | |||
175 | $entryRepo->expects($this->never()) | ||
176 | ->method('findByUrlAndUserId'); | ||
177 | |||
178 | $this->em | ||
179 | ->expects($this->never()) | ||
180 | ->method('getRepository'); | ||
181 | |||
182 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
183 | ->disableOriginalConstructor() | ||
184 | ->getMock(); | ||
185 | |||
186 | $this->contentProxy | ||
187 | ->expects($this->never()) | ||
188 | ->method('updateEntry'); | ||
189 | |||
190 | $factory = new RedisMockFactory(); | ||
191 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
192 | |||
193 | $queue = new RedisQueue($redisMock, 'readability'); | ||
194 | $producer = new Producer($queue); | ||
195 | |||
196 | $readabilityImport->setProducer($producer); | ||
197 | |||
198 | $res = $readabilityImport->setMarkAsRead(true)->import(); | ||
199 | |||
200 | $this->assertTrue($res); | ||
201 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $readabilityImport->getSummary()); | ||
202 | |||
203 | $this->assertNotEmpty($redisMock->lpop('readability')); | ||
204 | } | ||
205 | |||
206 | public function testImportBadFile() | ||
207 | { | ||
208 | $readabilityImport = $this->getReadabilityImport(); | ||
209 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | ||
210 | |||
211 | $res = $readabilityImport->import(); | ||
212 | |||
213 | $this->assertFalse($res); | ||
214 | |||
215 | $records = $this->logHandler->getRecords(); | ||
216 | $this->assertContains('ReadabilityImport: unable to read file', $records[0]['message']); | ||
217 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
218 | } | ||
219 | |||
220 | public function testImportUserNotDefined() | ||
221 | { | ||
222 | $readabilityImport = $this->getReadabilityImport(true); | ||
223 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
224 | |||
225 | $res = $readabilityImport->import(); | ||
226 | |||
227 | $this->assertFalse($res); | ||
228 | |||
229 | $records = $this->logHandler->getRecords(); | ||
230 | $this->assertContains('ReadabilityImport: user is not defined', $records[0]['message']); | ||
231 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
232 | } | ||
233 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php index bdc47dac..82dc4c7e 100644 --- a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php | |||
@@ -5,8 +5,11 @@ namespace Tests\Wallabag\ImportBundle\Import; | |||
5 | use Wallabag\ImportBundle\Import\WallabagV1Import; | 5 | use Wallabag\ImportBundle\Import\WallabagV1Import; |
6 | use Wallabag\UserBundle\Entity\User; | 6 | use Wallabag\UserBundle\Entity\User; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\ImportBundle\Redis\Producer; | ||
8 | use Monolog\Logger; | 9 | use Monolog\Logger; |
9 | use Monolog\Handler\TestHandler; | 10 | use Monolog\Handler\TestHandler; |
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
10 | 13 | ||
11 | class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | 14 | class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase |
12 | { | 15 | { |
@@ -23,6 +26,20 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | |||
23 | ->disableOriginalConstructor() | 26 | ->disableOriginalConstructor() |
24 | ->getMock(); | 27 | ->getMock(); |
25 | 28 | ||
29 | $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork') | ||
30 | ->disableOriginalConstructor() | ||
31 | ->getMock(); | ||
32 | |||
33 | $this->em | ||
34 | ->expects($this->any()) | ||
35 | ->method('getUnitOfWork') | ||
36 | ->willReturn($this->uow); | ||
37 | |||
38 | $this->uow | ||
39 | ->expects($this->any()) | ||
40 | ->method('getScheduledEntityInsertions') | ||
41 | ->willReturn([]); | ||
42 | |||
26 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | 43 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
27 | ->disableOriginalConstructor() | 44 | ->disableOriginalConstructor() |
28 | ->getMock(); | 45 | ->getMock(); |
@@ -79,7 +96,7 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | |||
79 | $res = $wallabagV1Import->import(); | 96 | $res = $wallabagV1Import->import(); |
80 | 97 | ||
81 | $this->assertTrue($res); | 98 | $this->assertTrue($res); |
82 | $this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary()); | 99 | $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary()); |
83 | } | 100 | } |
84 | 101 | ||
85 | public function testImportAndMarkAllAsRead() | 102 | public function testImportAndMarkAllAsRead() |
@@ -117,7 +134,87 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | |||
117 | 134 | ||
118 | $this->assertTrue($res); | 135 | $this->assertTrue($res); |
119 | 136 | ||
120 | $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary()); | 137 | $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary()); |
138 | } | ||
139 | |||
140 | public function testImportWithRabbit() | ||
141 | { | ||
142 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
143 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | ||
144 | |||
145 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
146 | ->disableOriginalConstructor() | ||
147 | ->getMock(); | ||
148 | |||
149 | $entryRepo->expects($this->never()) | ||
150 | ->method('findByUrlAndUserId'); | ||
151 | |||
152 | $this->em | ||
153 | ->expects($this->never()) | ||
154 | ->method('getRepository'); | ||
155 | |||
156 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
157 | ->disableOriginalConstructor() | ||
158 | ->getMock(); | ||
159 | |||
160 | $this->contentProxy | ||
161 | ->expects($this->never()) | ||
162 | ->method('updateEntry'); | ||
163 | |||
164 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
165 | ->disableOriginalConstructor() | ||
166 | ->getMock(); | ||
167 | |||
168 | $producer | ||
169 | ->expects($this->exactly(4)) | ||
170 | ->method('publish'); | ||
171 | |||
172 | $wallabagV1Import->setProducer($producer); | ||
173 | |||
174 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | ||
175 | |||
176 | $this->assertTrue($res); | ||
177 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary()); | ||
178 | } | ||
179 | |||
180 | public function testImportWithRedis() | ||
181 | { | ||
182 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
183 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | ||
184 | |||
185 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
186 | ->disableOriginalConstructor() | ||
187 | ->getMock(); | ||
188 | |||
189 | $entryRepo->expects($this->never()) | ||
190 | ->method('findByUrlAndUserId'); | ||
191 | |||
192 | $this->em | ||
193 | ->expects($this->never()) | ||
194 | ->method('getRepository'); | ||
195 | |||
196 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
197 | ->disableOriginalConstructor() | ||
198 | ->getMock(); | ||
199 | |||
200 | $this->contentProxy | ||
201 | ->expects($this->never()) | ||
202 | ->method('updateEntry'); | ||
203 | |||
204 | $factory = new RedisMockFactory(); | ||
205 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
206 | |||
207 | $queue = new RedisQueue($redisMock, 'wallabag_v1'); | ||
208 | $producer = new Producer($queue); | ||
209 | |||
210 | $wallabagV1Import->setProducer($producer); | ||
211 | |||
212 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | ||
213 | |||
214 | $this->assertTrue($res); | ||
215 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary()); | ||
216 | |||
217 | $this->assertNotEmpty($redisMock->lpop('wallabag_v1')); | ||
121 | } | 218 | } |
122 | 219 | ||
123 | public function testImportBadFile() | 220 | public function testImportBadFile() |
diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php index 4a45e0f0..bea89efb 100644 --- a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php | |||
@@ -5,8 +5,11 @@ namespace Tests\Wallabag\ImportBundle\Import; | |||
5 | use Wallabag\ImportBundle\Import\WallabagV2Import; | 5 | use Wallabag\ImportBundle\Import\WallabagV2Import; |
6 | use Wallabag\UserBundle\Entity\User; | 6 | use Wallabag\UserBundle\Entity\User; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\ImportBundle\Redis\Producer; | ||
8 | use Monolog\Logger; | 9 | use Monolog\Logger; |
9 | use Monolog\Handler\TestHandler; | 10 | use Monolog\Handler\TestHandler; |
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
10 | 13 | ||
11 | class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | 14 | class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase |
12 | { | 15 | { |
@@ -23,6 +26,20 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
23 | ->disableOriginalConstructor() | 26 | ->disableOriginalConstructor() |
24 | ->getMock(); | 27 | ->getMock(); |
25 | 28 | ||
29 | $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork') | ||
30 | ->disableOriginalConstructor() | ||
31 | ->getMock(); | ||
32 | |||
33 | $this->em | ||
34 | ->expects($this->any()) | ||
35 | ->method('getUnitOfWork') | ||
36 | ->willReturn($this->uow); | ||
37 | |||
38 | $this->uow | ||
39 | ->expects($this->any()) | ||
40 | ->method('getScheduledEntityInsertions') | ||
41 | ->willReturn([]); | ||
42 | |||
26 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | 43 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
27 | ->disableOriginalConstructor() | 44 | ->disableOriginalConstructor() |
28 | ->getMock(); | 45 | ->getMock(); |
@@ -75,7 +92,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
75 | $res = $wallabagV2Import->import(); | 92 | $res = $wallabagV2Import->import(); |
76 | 93 | ||
77 | $this->assertTrue($res); | 94 | $this->assertTrue($res); |
78 | $this->assertEquals(['skipped' => 22, 'imported' => 2], $wallabagV2Import->getSummary()); | 95 | $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
79 | } | 96 | } |
80 | 97 | ||
81 | public function testImportAndMarkAllAsRead() | 98 | public function testImportAndMarkAllAsRead() |
@@ -113,7 +130,79 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
113 | 130 | ||
114 | $this->assertTrue($res); | 131 | $this->assertTrue($res); |
115 | 132 | ||
116 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary()); | 133 | $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
134 | } | ||
135 | |||
136 | public function testImportWithRabbit() | ||
137 | { | ||
138 | $wallabagV2Import = $this->getWallabagV2Import(); | ||
139 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | ||
140 | |||
141 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
142 | ->disableOriginalConstructor() | ||
143 | ->getMock(); | ||
144 | |||
145 | $entryRepo->expects($this->never()) | ||
146 | ->method('findByUrlAndUserId'); | ||
147 | |||
148 | $this->em | ||
149 | ->expects($this->never()) | ||
150 | ->method('getRepository'); | ||
151 | |||
152 | $this->contentProxy | ||
153 | ->expects($this->never()) | ||
154 | ->method('updateEntry'); | ||
155 | |||
156 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
157 | ->disableOriginalConstructor() | ||
158 | ->getMock(); | ||
159 | |||
160 | $producer | ||
161 | ->expects($this->exactly(24)) | ||
162 | ->method('publish'); | ||
163 | |||
164 | $wallabagV2Import->setProducer($producer); | ||
165 | |||
166 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | ||
167 | |||
168 | $this->assertTrue($res); | ||
169 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary()); | ||
170 | } | ||
171 | |||
172 | public function testImportWithRedis() | ||
173 | { | ||
174 | $wallabagV2Import = $this->getWallabagV2Import(); | ||
175 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | ||
176 | |||
177 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
178 | ->disableOriginalConstructor() | ||
179 | ->getMock(); | ||
180 | |||
181 | $entryRepo->expects($this->never()) | ||
182 | ->method('findByUrlAndUserId'); | ||
183 | |||
184 | $this->em | ||
185 | ->expects($this->never()) | ||
186 | ->method('getRepository'); | ||
187 | |||
188 | $this->contentProxy | ||
189 | ->expects($this->never()) | ||
190 | ->method('updateEntry'); | ||
191 | |||
192 | $factory = new RedisMockFactory(); | ||
193 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
194 | |||
195 | $queue = new RedisQueue($redisMock, 'wallabag_v2'); | ||
196 | $producer = new Producer($queue); | ||
197 | |||
198 | $wallabagV2Import->setProducer($producer); | ||
199 | |||
200 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | ||
201 | |||
202 | $this->assertTrue($res); | ||
203 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary()); | ||
204 | |||
205 | $this->assertNotEmpty($redisMock->lpop('wallabag_v2')); | ||
117 | } | 206 | } |
118 | 207 | ||
119 | public function testImportBadFile() | 208 | public function testImportBadFile() |
@@ -152,7 +241,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
152 | $res = $wallabagV2Import->import(); | 241 | $res = $wallabagV2Import->import(); |
153 | 242 | ||
154 | $this->assertFalse($res); | 243 | $this->assertFalse($res); |
155 | $this->assertEquals(['skipped' => 0, 'imported' => 0], $wallabagV2Import->getSummary()); | 244 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary()); |
156 | } | 245 | } |
157 | 246 | ||
158 | public function testImportWithExceptionFromGraby() | 247 | public function testImportWithExceptionFromGraby() |
@@ -181,6 +270,6 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
181 | $res = $wallabagV2Import->import(); | 270 | $res = $wallabagV2Import->import(); |
182 | 271 | ||
183 | $this->assertTrue($res); | 272 | $this->assertTrue($res); |
184 | $this->assertEquals(['skipped' => 24, 'imported' => 0], $wallabagV2Import->getSummary()); | 273 | $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
185 | } | 274 | } |
186 | } | 275 | } |