]>
Commit | Line | Data |
---|---|---|
13470c35 JB |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\ImportBundle\Consumer\AMQP; | |
4 | ||
13470c35 | 5 | use PhpAmqpLib\Message\AMQPMessage; |
bd91bd5c | 6 | use PHPUnit\Framework\TestCase; |
13470c35 | 7 | use Wallabag\CoreBundle\Entity\Entry; |
f808b016 JB |
8 | use Wallabag\ImportBundle\Consumer\AMQPEntryConsumer; |
9 | use Wallabag\UserBundle\Entity\User; | |
13470c35 | 10 | |
bd91bd5c | 11 | class AMQPEntryConsumerTest extends TestCase |
13470c35 JB |
12 | { |
13 | public function testMessageOk() | |
14 | { | |
15 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
16 | ->disableOriginalConstructor() | |
17 | ->getMock(); | |
18 | ||
19 | $em | |
20 | ->expects($this->once()) | |
21 | ->method('flush'); | |
22 | ||
23 | $em | |
24 | ->expects($this->exactly(2)) | |
25 | ->method('clear'); | |
26 | ||
27 | $body = <<<'JSON' | |
28 | { | |
29 | "item_id": "1402935436", | |
30 | "resolved_id": "1402935436", | |
31 | "given_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", | |
32 | "given_title": "Leslie Jones is back on Twitter and her comeback tweet rules", | |
33 | "favorite": "0", | |
34 | "status": "0", | |
35 | "time_added": "1473020899", | |
36 | "time_updated": "1473020899", | |
37 | "time_read": "0", | |
38 | "time_favorited": "0", | |
39 | "sort_id": 0, | |
40 | "resolved_title": "Leslie Jones is back on Twitter and her comeback tweet rules", | |
41 | "resolved_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", | |
42 | "excerpt": "Leslie Jones is back to communicating with her adoring public on Twitter after cowardly hacker-trolls drove her away, probably to compensate for their own failings. It all started with a mic drop ...", | |
43 | "is_article": "1", | |
44 | "is_index": "0", | |
45 | "has_video": "0", | |
46 | "has_image": "1", | |
47 | "word_count": "200", | |
48 | "tags": { | |
49 | "ifttt": { | |
50 | "item_id": "1402935436", | |
51 | "tag": "ifttt" | |
52 | }, | |
53 | "mashable": { | |
54 | "item_id": "1402935436", | |
55 | "tag": "mashable" | |
56 | } | |
57 | }, | |
58 | "authors": { | |
59 | "2484273": { | |
60 | "item_id": "1402935436", | |
61 | "author_id": "2484273", | |
62 | "name": "Adam Rosenberg", | |
63 | "url": "http://mashable.com/author/adam-rosenberg/" | |
64 | } | |
65 | }, | |
66 | "image": { | |
67 | "item_id": "1402935436", | |
68 | "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", | |
69 | "width": "0", | |
70 | "height": "0" | |
71 | }, | |
72 | "images": { | |
73 | "1": { | |
74 | "item_id": "1402935436", | |
75 | "image_id": "1", | |
76 | "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", | |
77 | "width": "0", | |
78 | "height": "0", | |
79 | "credit": "Image: Steve Eichner/NameFace/Sipa USA", | |
80 | "caption": "" | |
81 | } | |
82 | }, | |
83 | "userId": 1 | |
84 | } | |
85 | JSON; | |
86 | ||
87 | $user = new User(); | |
88 | $entry = new Entry($user); | |
89 | ||
90 | $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') | |
91 | ->disableOriginalConstructor() | |
92 | ->getMock(); | |
93 | ||
94 | $userRepository | |
95 | ->expects($this->once()) | |
96 | ->method('find') | |
97 | // userId from the body json above | |
98 | ->with(1) | |
99 | ->willReturn($user); | |
100 | ||
101 | $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') | |
102 | ->disableOriginalConstructor() | |
103 | ->getMock(); | |
104 | ||
105 | $import | |
106 | ->expects($this->once()) | |
107 | ->method('setUser') | |
108 | ->with($user); | |
109 | ||
110 | $import | |
111 | ->expects($this->once()) | |
112 | ->method('parseEntry') | |
113 | ->with(json_decode($body, true)) | |
114 | ->willReturn($entry); | |
115 | ||
7816eb62 JB |
116 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') |
117 | ->disableOriginalConstructor() | |
118 | ->getMock(); | |
119 | ||
120 | $dispatcher | |
121 | ->expects($this->once()) | |
122 | ->method('dispatch'); | |
123 | ||
ac87e0db | 124 | $consumer = new AMQPEntryConsumer( |
13470c35 JB |
125 | $em, |
126 | $userRepository, | |
7816eb62 JB |
127 | $import, |
128 | $dispatcher | |
13470c35 JB |
129 | ); |
130 | ||
131 | $message = new AMQPMessage($body); | |
132 | ||
133 | $consumer->execute($message); | |
134 | } | |
135 | ||
136 | public function testMessageWithBadUser() | |
137 | { | |
138 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
139 | ->disableOriginalConstructor() | |
140 | ->getMock(); | |
141 | ||
142 | $em | |
143 | ->expects($this->never()) | |
144 | ->method('flush'); | |
145 | ||
146 | $em | |
147 | ->expects($this->never()) | |
148 | ->method('clear'); | |
149 | ||
150 | $body = '{ "userId": 123 }'; | |
151 | ||
152 | $user = new User(); | |
153 | $entry = new Entry($user); | |
154 | ||
155 | $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') | |
156 | ->disableOriginalConstructor() | |
157 | ->getMock(); | |
158 | ||
159 | $userRepository | |
160 | ->expects($this->once()) | |
161 | ->method('find') | |
162 | // userId from the body json above | |
163 | ->with(123) | |
164 | ->willReturn(null); | |
165 | ||
166 | $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') | |
167 | ->disableOriginalConstructor() | |
168 | ->getMock(); | |
169 | ||
7816eb62 JB |
170 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') |
171 | ->disableOriginalConstructor() | |
172 | ->getMock(); | |
173 | ||
174 | $dispatcher | |
175 | ->expects($this->never()) | |
176 | ->method('dispatch'); | |
177 | ||
ac87e0db | 178 | $consumer = new AMQPEntryConsumer( |
13470c35 JB |
179 | $em, |
180 | $userRepository, | |
7816eb62 JB |
181 | $import, |
182 | $dispatcher | |
13470c35 JB |
183 | ); |
184 | ||
185 | $message = new AMQPMessage($body); | |
186 | ||
b45b6b67 NL |
187 | $res = $consumer->execute($message); |
188 | ||
189 | $this->assertTrue($res); | |
13470c35 JB |
190 | } |
191 | ||
192 | public function testMessageWithEntryProcessed() | |
193 | { | |
194 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
195 | ->disableOriginalConstructor() | |
196 | ->getMock(); | |
197 | ||
198 | $em | |
199 | ->expects($this->never()) | |
200 | ->method('flush'); | |
201 | ||
202 | $em | |
203 | ->expects($this->never()) | |
204 | ->method('clear'); | |
205 | ||
206 | $body = '{ "userId": 123 }'; | |
207 | ||
208 | $user = new User(); | |
209 | ||
210 | $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') | |
211 | ->disableOriginalConstructor() | |
212 | ->getMock(); | |
213 | ||
214 | $userRepository | |
215 | ->expects($this->once()) | |
216 | ->method('find') | |
217 | // userId from the body json above | |
218 | ->with(123) | |
219 | ->willReturn($user); | |
220 | ||
221 | $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') | |
222 | ->disableOriginalConstructor() | |
223 | ->getMock(); | |
224 | ||
225 | $import | |
226 | ->expects($this->once()) | |
227 | ->method('setUser') | |
228 | ->with($user); | |
229 | ||
230 | $import | |
231 | ->expects($this->once()) | |
232 | ->method('parseEntry') | |
233 | ->with(json_decode($body, true)) | |
234 | ->willReturn(null); | |
235 | ||
7816eb62 JB |
236 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') |
237 | ->disableOriginalConstructor() | |
238 | ->getMock(); | |
239 | ||
240 | $dispatcher | |
241 | ->expects($this->never()) | |
242 | ->method('dispatch'); | |
243 | ||
ac87e0db | 244 | $consumer = new AMQPEntryConsumer( |
13470c35 JB |
245 | $em, |
246 | $userRepository, | |
7816eb62 JB |
247 | $import, |
248 | $dispatcher | |
13470c35 JB |
249 | ); |
250 | ||
251 | $message = new AMQPMessage($body); | |
252 | ||
253 | $consumer->execute($message); | |
254 | } | |
255 | } |