22
33namespace OldSound \RabbitMqBundle \Tests \RabbitMq ;
44
5+ use OldSound \RabbitMqBundle \Event \AfterProcessingMessageEvent ;
6+ use OldSound \RabbitMqBundle \Event \BeforeProcessingMessageEvent ;
57use OldSound \RabbitMqBundle \RabbitMq \Consumer ;
68use PhpAmqpLib \Message \AMQPMessage ;
79use OldSound \RabbitMqBundle \RabbitMq \ConsumerInterface ;
@@ -58,7 +60,17 @@ public function testProcessMessage($processFlag, $expectedMethod, $expectedReque
5860 ->will ($ this ->returnCallback (function ($ delivery_tag ) use ($ expectedMethod ) {
5961 \PHPUnit_Framework_Assert::assertSame ($ expectedMethod , 'basic_ack ' ); // Check if this function should be called.
6062 }));
63+ $ eventDispatcher = $ this ->getMockBuilder ('Symfony\Component\EventDispatcher\EventDispatcherInterface ' )
64+ ->getMock ();
65+ $ consumer ->setEventDispatcher ($ eventDispatcher );
6166
67+ $ eventDispatcher ->expects ($ this ->atLeastOnce ())
68+ ->method ('dispatch ' )
69+ ->withConsecutive (
70+ array (BeforeProcessingMessageEvent::NAME , new BeforeProcessingMessageEvent ($ amqpMessage )),
71+ array (AfterProcessingMessageEvent::NAME , new AfterProcessingMessageEvent ($ amqpMessage ))
72+ )
73+ ->willReturn (true );
6274 $ consumer ->processMessage ($ amqpMessage );
6375 }
6476
@@ -73,4 +85,87 @@ public function processMessageProvider()
7385 array (ConsumerInterface::MSG_REJECT , 'basic_reject ' , false ), // Reject and drop
7486 );
7587 }
88+
89+ /**
90+ * @return array
91+ */
92+ public function consumeProvider ()
93+ {
94+ $ testCases [ "All ok 4 callbacks " ] = array (
95+ array (
96+ "messages " => array (
97+ "msgCallback1 " ,
98+ "msgCallback2 " ,
99+ "msgCallback3 " ,
100+ "msgCallback4 " ,
101+ )
102+ )
103+ );
104+
105+ $ testCases [ "No callbacks " ] = array (
106+ array (
107+ "messages " => array (
108+ )
109+ )
110+ );
111+
112+ return $ testCases ;
113+ }
114+
115+ /**
116+ * @dataProvider consumeProvider
117+ *
118+ * @param $data
119+ */
120+ public function testConsume ($ data )
121+ {
122+ $ consumerCallBacks = $ data ['messages ' ];
123+
124+ // set up amqp connection
125+ $ amqpConnection = $ this ->prepareAMQPConnection ();
126+ // set up amqp channel
127+ $ amqpChannel = $ this ->prepareAMQPChannel ();
128+ $ amqpChannel ->expects ($ this ->atLeastOnce ())
129+ ->method ('getChannelId ' )
130+ ->with ()
131+ ->willReturn (true );
132+ $ amqpChannel ->expects ($ this ->once ())
133+ ->method ('basic_consume ' )
134+ ->withAnyParameters ()
135+ ->willReturn (true );
136+
137+ // set up consumer
138+ $ consumer = $ this ->getConsumer ($ amqpConnection , $ amqpChannel );
139+ // disable autosetup fabric so we do not mock more objects
140+ $ consumer ->disableAutoSetupFabric ();
141+ $ consumer ->setChannel ($ amqpChannel );
142+ $ amqpChannel ->callbacks = $ consumerCallBacks ;
143+
144+ /**
145+ * Mock ait method and use a callback to remove one element each time from callbacks
146+ * This will simulate a basic consumer consume with provided messages count
147+ */
148+ $ amqpChannel ->expects ($ this ->exactly (count ($ consumerCallBacks )))
149+ ->method ('wait ' )
150+ ->with (null , false , $ consumer ->getIdleTimeout ())
151+ ->will (
152+ $ this ->returnCallback (
153+ function () use ($ amqpChannel ) {
154+ /** remove an element on each loop like ... simulate an ACK */
155+ array_splice ($ amqpChannel ->callbacks , 0 , 1 );
156+ })
157+ );
158+
159+ // set up event dispatcher
160+ $ eventDispatcher = $ this ->getMockBuilder ('Symfony\Component\EventDispatcher\EventDispatcher ' )
161+ ->disableOriginalConstructor ()
162+ ->getMock ();
163+
164+ $ eventDispatcher ->expects ($ this ->exactly (count ($ consumerCallBacks )))
165+ ->method ('dispatch ' )
166+ ->willReturn ($ eventDispatcher );
167+
168+ $ consumer ->setEventDispatcher ($ eventDispatcher );
169+ $ consumer ->consume (1 );
170+ }
76171}
0 commit comments