3 namespace Tests\Wallabag\ImportBundle\Consumer\AMQP
;
5 use PhpAmqpLib\Message\AMQPMessage
;
6 use PHPUnit\Framework\TestCase
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\ImportBundle\Consumer\AMQPEntryConsumer
;
9 use Wallabag\UserBundle\Entity\User
;
11 class AMQPEntryConsumerTest
extends TestCase
13 public function testMessageOk()
15 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
16 ->disableOriginalConstructor()
20 ->expects($this->once())
24 ->expects($this->exactly(2))
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",
35 "time_added": "1473020899",
36 "time_updated": "1473020899",
38 "time_favorited": "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 ...",
50 "item_id": "1402935436",
54 "item_id": "1402935436",
60 "item_id": "1402935436",
61 "author_id": "2484273",
62 "name": "Adam Rosenberg",
63 "url": "http://mashable.com/author/adam-rosenberg/"
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",
74 "item_id": "1402935436",
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",
79 "credit": "Image: Steve Eichner/NameFace/Sipa USA",
88 $entry = new Entry($user);
90 $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
91 ->disableOriginalConstructor()
95 ->expects($this->once())
97 // userId from the body json above
101 $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport')
102 ->disableOriginalConstructor()
106 ->expects($this->once())
111 ->expects($this->once())
112 ->method('parseEntry')
113 ->with(json_decode($body, true))
114 ->willReturn($entry);
116 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
117 ->disableOriginalConstructor()
121 ->expects($this->once())
122 ->method('dispatch');
124 $consumer = new AMQPEntryConsumer(
131 $message = new AMQPMessage($body);
133 $consumer->execute($message);
136 public function testMessageWithBadUser()
138 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
139 ->disableOriginalConstructor()
143 ->expects($this->never())
147 ->expects($this->never())
150 $body = '{ "userId": 123 }';
153 $entry = new Entry($user);
155 $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
156 ->disableOriginalConstructor()
160 ->expects($this->once())
162 // userId from the body json above
166 $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport')
167 ->disableOriginalConstructor()
170 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
171 ->disableOriginalConstructor()
175 ->expects($this->never())
176 ->method('dispatch');
178 $consumer = new AMQPEntryConsumer(
185 $message = new AMQPMessage($body);
187 $res = $consumer->execute($message);
189 $this->assertTrue($res);
192 public function testMessageWithEntryProcessed()
194 $em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
195 ->disableOriginalConstructor()
199 ->expects($this->never())
203 ->expects($this->never())
206 $body = '{ "userId": 123 }';
210 $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
211 ->disableOriginalConstructor()
215 ->expects($this->once())
217 // userId from the body json above
221 $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport')
222 ->disableOriginalConstructor()
226 ->expects($this->once())
231 ->expects($this->once())
232 ->method('parseEntry')
233 ->with(json_decode($body, true))
236 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
237 ->disableOriginalConstructor()
241 ->expects($this->never())
242 ->method('dispatch');
244 $consumer = new AMQPEntryConsumer(
251 $message = new AMQPMessage($body);
253 $consumer->execute($message);