]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ImportBundle/Consumer/AMQPEntryConsumerTest.php
Fixed typo in "first_steps"
[github/wallabag/wallabag.git] / tests / Wallabag / ImportBundle / Consumer / AMQPEntryConsumerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ImportBundle\Consumer\AMQP;
4
5 use Wallabag\ImportBundle\Consumer\AMQPEntryConsumer;
6 use PhpAmqpLib\Message\AMQPMessage;
7 use Wallabag\UserBundle\Entity\User;
8 use Wallabag\CoreBundle\Entity\Entry;
9
10 class AMQPEntryConsumerTest extends \PHPUnit_Framework_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 $consumer = new AMQPEntryConsumer(
116 $em,
117 $userRepository,
118 $import
119 );
120
121 $message = new AMQPMessage($body);
122
123 $consumer->execute($message);
124 }
125
126 public function testMessageWithBadUser()
127 {
128 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
129 ->disableOriginalConstructor()
130 ->getMock();
131
132 $em
133 ->expects($this->never())
134 ->method('flush');
135
136 $em
137 ->expects($this->never())
138 ->method('clear');
139
140 $body = '{ "userId": 123 }';
141
142 $user = new User();
143 $entry = new Entry($user);
144
145 $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
146 ->disableOriginalConstructor()
147 ->getMock();
148
149 $userRepository
150 ->expects($this->once())
151 ->method('find')
152 // userId from the body json above
153 ->with(123)
154 ->willReturn(null);
155
156 $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport')
157 ->disableOriginalConstructor()
158 ->getMock();
159
160 $consumer = new AMQPEntryConsumer(
161 $em,
162 $userRepository,
163 $import
164 );
165
166 $message = new AMQPMessage($body);
167
168 $consumer->execute($message);
169 }
170
171 public function testMessageWithEntryProcessed()
172 {
173 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
174 ->disableOriginalConstructor()
175 ->getMock();
176
177 $em
178 ->expects($this->never())
179 ->method('flush');
180
181 $em
182 ->expects($this->never())
183 ->method('clear');
184
185 $body = '{ "userId": 123 }';
186
187 $user = new User();
188
189 $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
190 ->disableOriginalConstructor()
191 ->getMock();
192
193 $userRepository
194 ->expects($this->once())
195 ->method('find')
196 // userId from the body json above
197 ->with(123)
198 ->willReturn($user);
199
200 $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport')
201 ->disableOriginalConstructor()
202 ->getMock();
203
204 $import
205 ->expects($this->once())
206 ->method('setUser')
207 ->with($user);
208
209 $import
210 ->expects($this->once())
211 ->method('parseEntry')
212 ->with(json_decode($body, true))
213 ->willReturn(null);
214
215 $consumer = new AMQPEntryConsumer(
216 $em,
217 $userRepository,
218 $import
219 );
220
221 $message = new AMQPMessage($body);
222
223 $consumer->execute($message);
224 }
225 }