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