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