]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ImportBundle/Consumer/RedisEntryConsumerTest.php
Enable PHPStan
[github/wallabag/wallabag.git] / tests / Wallabag / ImportBundle / Consumer / RedisEntryConsumerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ImportBundle\Consumer;
4
5 use PHPUnit\Framework\TestCase;
6 use Wallabag\CoreBundle\Entity\Entry;
7 use Wallabag\ImportBundle\Consumer\RedisEntryConsumer;
8 use Wallabag\UserBundle\Entity\User;
9
10 class RedisEntryConsumerTest extends TestCase
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 }
84 JSON;
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
115 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
116 ->disableOriginalConstructor()
117 ->getMock();
118
119 $dispatcher
120 ->expects($this->once())
121 ->method('dispatch');
122
123 $consumer = new RedisEntryConsumer(
124 $em,
125 $userRepository,
126 $import,
127 $dispatcher
128 );
129
130 $res = $consumer->manage($body);
131
132 $this->assertTrue($res);
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
169 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
170 ->disableOriginalConstructor()
171 ->getMock();
172
173 $dispatcher
174 ->expects($this->never())
175 ->method('dispatch');
176
177 $consumer = new RedisEntryConsumer(
178 $em,
179 $userRepository,
180 $import,
181 $dispatcher
182 );
183
184 $res = $consumer->manage($body);
185
186 $this->assertTrue($res);
187 }
188
189 public function testMessageWithEntryProcessed()
190 {
191 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
192 ->disableOriginalConstructor()
193 ->getMock();
194
195 $em
196 ->expects($this->never())
197 ->method('flush');
198
199 $em
200 ->expects($this->never())
201 ->method('clear');
202
203 $body = '{ "userId": 123 }';
204
205 $user = new User();
206
207 $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
208 ->disableOriginalConstructor()
209 ->getMock();
210
211 $userRepository
212 ->expects($this->once())
213 ->method('find')
214 // userId from the body json above
215 ->with(123)
216 ->willReturn($user);
217
218 $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport')
219 ->disableOriginalConstructor()
220 ->getMock();
221
222 $import
223 ->expects($this->once())
224 ->method('setUser')
225 ->with($user);
226
227 $import
228 ->expects($this->once())
229 ->method('parseEntry')
230 ->with(json_decode($body, true))
231 ->willReturn(null);
232
233 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
234 ->disableOriginalConstructor()
235 ->getMock();
236
237 $dispatcher
238 ->expects($this->never())
239 ->method('dispatch');
240
241 $consumer = new RedisEntryConsumer(
242 $em,
243 $userRepository,
244 $import,
245 $dispatcher
246 );
247
248 $res = $consumer->manage($body);
249
250 $this->assertTrue($res);
251 $this->assertFalse($consumer->isStopJob($body));
252 }
253 }