1+ <?php
2+
3+ use Illuminate \Container \Container ;
4+ use PhpAmqpLib \Channel \AMQPChannel ;
5+ use PhpAmqpLib \Connection \AMQPStreamConnection ;
6+ use PhpAmqpLib \Message \AMQPMessage ;
7+ use PHPUnit \Framework \TestCase ;
8+ use VladimirYuldashev \LaravelQueueRabbitMQ \Queue \RabbitMQQueue ;
9+
10+ /**
11+ * @property \Mockery\MockInterface connection
12+ * @property \Mockery\MockInterface channel
13+ * @property array config
14+ * @property RabbitMQQueue queue
15+ */
16+ class RabbitMQQueueTest extends TestCase
17+ {
18+
19+ public function setUp ()
20+ {
21+ parent ::setUp ();
22+
23+ $ this ->connection = Mockery::mock (AMQPStreamConnection::class);
24+ $ this ->channel = Mockery::mock (AMQPChannel::class);
25+
26+ $ this ->connection ->shouldReceive ('channel ' )->andReturn ($ this ->channel );
27+
28+ $ this ->config = [
29+ 'queue ' => str_random (),
30+ 'queue_params ' => [
31+ 'passive ' => false ,
32+ 'durable ' => true ,
33+ 'exclusive ' => false ,
34+ 'auto_delete ' => false ,
35+ ],
36+ 'exchange_params ' => [
37+ 'name ' => 'exchange_name ' ,
38+ 'type ' => 'direct ' ,
39+ 'passive ' => false ,
40+ 'durable ' => true ,
41+ 'auto_delete ' => false ,
42+ ],
43+ 'exchange_declare ' => true ,
44+ 'queue_declare_bind ' => true ,
45+ ];
46+
47+ $ this ->queue = new RabbitMQQueue ($ this ->connection , $ this ->config );
48+ }
49+
50+ public function test_size ()
51+ {
52+ $ this ->queue ->size ();
53+ }
54+
55+ public function test_push ()
56+ {
57+ $ job = new TestJob ();
58+ $ data = [];
59+
60+ $ this ->channel ->shouldReceive ('exchange_declare ' )->with (
61+ $ this ->config ['exchange_params ' ]['name ' ],
62+ $ this ->config ['exchange_params ' ]['type ' ],
63+ $ this ->config ['exchange_params ' ]['passive ' ],
64+ $ this ->config ['exchange_params ' ]['durable ' ],
65+ $ this ->config ['exchange_params ' ]['auto_delete ' ]
66+ )->once ();
67+
68+ $ this ->channel ->shouldReceive ('queue_declare ' )->with (
69+ $ this ->config ['queue ' ],
70+ $ this ->config ['queue_params ' ]['passive ' ],
71+ $ this ->config ['queue_params ' ]['durable ' ],
72+ $ this ->config ['queue_params ' ]['exclusive ' ],
73+ $ this ->config ['queue_params ' ]['auto_delete ' ]
74+ )->once ();
75+
76+ $ this ->channel ->shouldReceive ('queue_bind ' )->with (
77+ $ this ->config ['queue ' ],
78+ $ this ->config ['exchange_params ' ]['name ' ],
79+ $ this ->config ['queue ' ]
80+ )->once ();
81+
82+ $ this ->channel ->shouldReceive ('basic_publish ' )->once ();
83+
84+ $ correlationId = $ this ->queue ->push ($ job , $ data );
85+
86+ $ this ->assertEquals (13 , strlen ($ correlationId ));
87+ }
88+
89+ public function test_later ()
90+ {
91+ $ job = new TestJob ();
92+ $ data = [];
93+ $ delay = mt_rand (10 , 60 );
94+
95+ $ this ->channel ->shouldReceive ('exchange_declare ' )->with (
96+ $ this ->config ['exchange_params ' ]['name ' ],
97+ $ this ->config ['exchange_params ' ]['type ' ],
98+ $ this ->config ['exchange_params ' ]['passive ' ],
99+ $ this ->config ['exchange_params ' ]['durable ' ],
100+ $ this ->config ['exchange_params ' ]['auto_delete ' ]
101+ )->once ();
102+
103+ $ this ->channel ->shouldReceive ('queue_declare ' )->once ();
104+
105+ // main queue
106+ $ this ->channel ->shouldReceive ('queue_bind ' )->with (
107+ $ this ->config ['queue ' ],
108+ $ this ->config ['exchange_params ' ]['name ' ],
109+ $ this ->config ['queue ' ]
110+ )->once ();
111+
112+ // delayed queue
113+ $ this ->channel ->shouldReceive ('queue_bind ' )->with (
114+ $ this ->config ['queue ' ] . '_deferred_ ' . $ delay ,
115+ $ this ->config ['exchange_params ' ]['name ' ],
116+ $ this ->config ['queue ' ] . '_deferred_ ' . $ delay
117+ )->once ();
118+
119+ $ this ->channel ->shouldReceive ('basic_publish ' )->once ();
120+
121+ $ correlationId = $ this ->queue ->later ($ delay , $ job , $ data );
122+
123+ $ this ->assertEquals (13 , strlen ($ correlationId ));
124+ }
125+
126+ public function test_pop ()
127+ {
128+ $ message = Mockery::mock (AMQPMessage::class);
129+
130+ $ this ->channel ->shouldReceive ('exchange_declare ' )->with (
131+ $ this ->config ['exchange_params ' ]['name ' ],
132+ $ this ->config ['exchange_params ' ]['type ' ],
133+ $ this ->config ['exchange_params ' ]['passive ' ],
134+ $ this ->config ['exchange_params ' ]['durable ' ],
135+ $ this ->config ['exchange_params ' ]['auto_delete ' ]
136+ )->once ();
137+
138+ $ this ->channel ->shouldReceive ('queue_declare ' )->with (
139+ $ this ->config ['queue ' ],
140+ $ this ->config ['queue_params ' ]['passive ' ],
141+ $ this ->config ['queue_params ' ]['durable ' ],
142+ $ this ->config ['queue_params ' ]['exclusive ' ],
143+ $ this ->config ['queue_params ' ]['auto_delete ' ]
144+ )->once ();
145+
146+ $ this ->channel ->shouldReceive ('queue_bind ' )->with (
147+ $ this ->config ['queue ' ],
148+ $ this ->config ['exchange_params ' ]['name ' ],
149+ $ this ->config ['queue ' ]
150+ )->once ();
151+
152+ $ this ->channel ->shouldReceive ('basic_get ' )->with ($ this ->config ['queue ' ])->andReturn ($ message )->once ();
153+
154+ $ this ->queue ->setContainer (Mockery::mock (Container::class));
155+ $ this ->queue ->pop ();
156+ }
157+
158+ public function test_setAttempts () {
159+ $ count = mt_rand ();
160+
161+ $ this ->queue ->setAttempts ($ count );
162+ }
163+
164+ public function test_setCorrelationId () {
165+ $ id = str_random ();
166+
167+ $ this ->queue ->setCorrelationId ($ id );
168+ }
169+
170+ }
0 commit comments