]>
Commit | Line | Data |
---|---|---|
a1a10770 JB |
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 Monolog\Logger; | |
9 | use Monolog\Handler\TestHandler; | |
10 | ||
11 | class ReadabilityImportTest extends \PHPUnit_Framework_TestCase | |
12 | { | |
13 | protected $user; | |
14 | protected $em; | |
15 | protected $logHandler; | |
16 | protected $contentProxy; | |
17 | ||
18 | private function getReadabilityImport($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 ReadabilityImport($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 | $readabilityImport = $this->getReadabilityImport(); | |
46 | ||
47 | $this->assertEquals('Readability', $readabilityImport->getName()); | |
48 | $this->assertNotEmpty($readabilityImport->getUrl()); | |
49 | $this->assertEquals('import.readability.description', $readabilityImport->getDescription()); | |
50 | } | |
51 | ||
52 | public function testImport() | |
53 | { | |
54 | $readabilityImport = $this->getReadabilityImport(); | |
55 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | |
56 | ||
57 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
58 | ->disableOriginalConstructor() | |
59 | ->getMock(); | |
60 | ||
61 | $entryRepo->expects($this->exactly(2)) | |
62 | ->method('findByUrlAndUserId') | |
63 | ->will($this->onConsecutiveCalls(false, true)); | |
64 | ||
65 | $this->em | |
66 | ->expects($this->any()) | |
67 | ->method('getRepository') | |
68 | ->willReturn($entryRepo); | |
69 | ||
70 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
71 | ->disableOriginalConstructor() | |
72 | ->getMock(); | |
73 | ||
74 | $this->contentProxy | |
75 | ->expects($this->exactly(1)) | |
76 | ->method('updateEntry') | |
77 | ->willReturn($entry); | |
78 | ||
79 | $res = $readabilityImport->import(); | |
80 | ||
81 | $this->assertTrue($res); | |
82 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $readabilityImport->getSummary()); | |
83 | } | |
84 | ||
85 | public function testImportAndMarkAllAsRead() | |
86 | { | |
87 | $readabilityImport = $this->getReadabilityImport(); | |
88 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability-read.json'); | |
89 | ||
90 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
91 | ->disableOriginalConstructor() | |
92 | ->getMock(); | |
93 | ||
94 | $entryRepo->expects($this->exactly(2)) | |
95 | ->method('findByUrlAndUserId') | |
96 | ->will($this->onConsecutiveCalls(false, false)); | |
97 | ||
98 | $this->em | |
99 | ->expects($this->any()) | |
100 | ->method('getRepository') | |
101 | ->willReturn($entryRepo); | |
102 | ||
103 | $this->contentProxy | |
104 | ->expects($this->exactly(2)) | |
105 | ->method('updateEntry') | |
106 | ->willReturn(new Entry($this->user)); | |
107 | ||
108 | // check that every entry persisted are archived | |
109 | $this->em | |
110 | ->expects($this->any()) | |
111 | ->method('persist') | |
112 | ->with($this->callback(function ($persistedEntry) { | |
113 | return $persistedEntry->isArchived(); | |
114 | })); | |
115 | ||
116 | $res = $readabilityImport->setMarkAsRead(true)->import(); | |
117 | ||
118 | $this->assertTrue($res); | |
119 | ||
120 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary()); | |
13470c35 JB |
121 | } |
122 | ||
123 | public function testImportWithRabbit() | |
124 | { | |
125 | $readabilityImport = $this->getReadabilityImport(); | |
126 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | |
127 | ||
128 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
129 | ->disableOriginalConstructor() | |
130 | ->getMock(); | |
131 | ||
132 | $entryRepo->expects($this->never()) | |
133 | ->method('findByUrlAndUserId'); | |
134 | ||
135 | $this->em | |
136 | ->expects($this->never()) | |
137 | ->method('getRepository'); | |
138 | ||
139 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
140 | ->disableOriginalConstructor() | |
141 | ->getMock(); | |
142 | ||
143 | $this->contentProxy | |
144 | ->expects($this->never()) | |
145 | ->method('updateEntry'); | |
146 | ||
147 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | |
148 | ->disableOriginalConstructor() | |
149 | ->getMock(); | |
150 | ||
151 | $producer | |
152 | ->expects($this->exactly(2)) | |
153 | ->method('publish'); | |
154 | ||
155 | $readabilityImport->setRabbitmqProducer($producer); | |
156 | ||
157 | $res = $readabilityImport->setMarkAsRead(true)->import(); | |
158 | ||
159 | $this->assertTrue($res); | |
160 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary()); | |
a1a10770 JB |
161 | } |
162 | ||
163 | public function testImportBadFile() | |
164 | { | |
165 | $readabilityImport = $this->getReadabilityImport(); | |
166 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | |
167 | ||
168 | $res = $readabilityImport->import(); | |
169 | ||
170 | $this->assertFalse($res); | |
171 | ||
172 | $records = $this->logHandler->getRecords(); | |
173 | $this->assertContains('ReadabilityImport: unable to read file', $records[0]['message']); | |
174 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
175 | } | |
176 | ||
177 | public function testImportUserNotDefined() | |
178 | { | |
179 | $readabilityImport = $this->getReadabilityImport(true); | |
180 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | |
181 | ||
182 | $res = $readabilityImport->import(); | |
183 | ||
184 | $this->assertFalse($res); | |
185 | ||
186 | $records = $this->logHandler->getRecords(); | |
187 | $this->assertContains('ReadabilityImport: user is not defined', $records[0]['message']); | |
188 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
189 | } | |
190 | } |