8
8
use Enqueue \Psr \PsrProcessor ;
9
9
use Enqueue \Psr \PsrQueue ;
10
10
use Enqueue \Symfony \Consumption \ContainerAwareConsumeMessagesCommand ;
11
+ use Enqueue \Tests \Symfony \Consumption \Mock \QueueSubscriberProcessor ;
11
12
use Symfony \Component \Console \Tester \CommandTester ;
12
13
use Symfony \Component \DependencyInjection \Container ;
13
14
use PHPUnit \Framework \TestCase ;
@@ -32,10 +33,11 @@ public function testShouldHaveExpectedOptions()
32
33
33
34
$ options = $ command ->getDefinition ()->getOptions ();
34
35
35
- $ this ->assertCount (3 , $ options );
36
+ $ this ->assertCount (4 , $ options );
36
37
$ this ->assertArrayHasKey ('memory-limit ' , $ options );
37
38
$ this ->assertArrayHasKey ('message-limit ' , $ options );
38
39
$ this ->assertArrayHasKey ('time-limit ' , $ options );
40
+ $ this ->assertArrayHasKey ('queue ' , $ options );
39
41
}
40
42
41
43
public function testShouldHaveExpectedAttributes ()
@@ -44,41 +46,63 @@ public function testShouldHaveExpectedAttributes()
44
46
45
47
$ arguments = $ command ->getDefinition ()->getArguments ();
46
48
47
- $ this ->assertCount (2 , $ arguments );
49
+ $ this ->assertCount (1 , $ arguments );
48
50
$ this ->assertArrayHasKey ('processor-service ' , $ arguments );
49
- $ this ->assertArrayHasKey ('queue ' , $ arguments );
50
51
}
51
52
52
53
public function testShouldThrowExceptionIfProcessorInstanceHasWrongClass ()
53
54
{
54
- $ this ->setExpectedException (\LogicException::class, 'Invalid message processor service given. ' .
55
- ' It must be an instance of Enqueue\Psr\PsrProcessor but stdClass ' );
56
-
57
55
$ container = new Container ();
58
56
$ container ->set ('processor-service ' , new \stdClass ());
59
57
60
58
$ command = new ContainerAwareConsumeMessagesCommand ($ this ->createQueueConsumerMock ());
61
59
$ command ->setContainer ($ container );
62
60
63
61
$ tester = new CommandTester ($ command );
62
+
63
+ $ this ->expectException (\LogicException::class);
64
+ $ this ->expectExceptionMessage ('Invalid message processor service given. It must be an instance of Enqueue\Psr\PsrProcessor but stdClass ' );
64
65
$ tester ->execute ([
65
- 'queue ' => 'queue-name ' ,
66
66
'processor-service ' => 'processor-service ' ,
67
+ '--queue ' => ['queue-name ' ],
67
68
]);
68
69
}
69
70
70
- public function testShouldExecuteConsumption ()
71
+ public function testThrowIfNeitherQueueOptionNorProcessorImplementsQueueSubscriberInterface ()
71
72
{
72
73
$ processor = $ this ->createProcessor ();
73
74
74
- $ queue = $ this ->createQueueMock ();
75
+ $ consumer = $ this ->createQueueConsumerMock ();
76
+ $ consumer
77
+ ->expects ($ this ->never ())
78
+ ->method ('bind ' )
79
+ ;
80
+ $ consumer
81
+ ->expects ($ this ->never ())
82
+ ->method ('consume ' )
83
+ ;
84
+
85
+ $ container = new Container ();
86
+ $ container ->set ('processor-service ' , $ processor );
87
+
88
+ $ command = new ContainerAwareConsumeMessagesCommand ($ consumer );
89
+ $ command ->setContainer ($ container );
90
+
91
+ $ tester = new CommandTester ($ command );
92
+
93
+
94
+ $ this ->expectException (\LogicException::class);
95
+ $ this ->expectExceptionMessage ('The queues are not provided. The processor must implement "Enqueue\Consumption\QueueSubscriberInterface" interface and it must return not empty array of queues or queues set using --queue option. ' );
96
+ $ tester ->execute ([
97
+ 'processor-service ' => 'processor-service ' ,
98
+ ]);
99
+ }
100
+
101
+ public function testShouldExecuteConsumptionWithExplisitlySetQueueViaQueueOption ()
102
+ {
103
+ $ processor = $ this ->createProcessor ();
75
104
76
105
$ context = $ this ->createContextMock ();
77
- $ context
78
- ->expects ($ this ->once ())
79
- ->method ('createQueue ' )
80
- ->willReturn ($ queue )
81
- ;
82
106
$ context
83
107
->expects ($ this ->once ())
84
108
->method ('close ' )
@@ -88,15 +112,15 @@ public function testShouldExecuteConsumption()
88
112
$ consumer
89
113
->expects ($ this ->once ())
90
114
->method ('bind ' )
91
- ->with ($ this -> identicalTo ( $ queue) , $ this ->identicalTo ($ processor ))
115
+ ->with (' queue-name ' , $ this ->identicalTo ($ processor ))
92
116
;
93
117
$ consumer
94
118
->expects ($ this ->once ())
95
119
->method ('consume ' )
96
120
->with ($ this ->isInstanceOf (ChainExtension::class))
97
121
;
98
122
$ consumer
99
- ->expects ($ this ->exactly (2 ))
123
+ ->expects ($ this ->exactly (1 ))
100
124
->method ('getPsrContext ' )
101
125
->will ($ this ->returnValue ($ context ))
102
126
;
@@ -109,8 +133,52 @@ public function testShouldExecuteConsumption()
109
133
110
134
$ tester = new CommandTester ($ command );
111
135
$ tester ->execute ([
112
- 'queue ' => 'queue-name ' ,
113
136
'processor-service ' => 'processor-service ' ,
137
+ '--queue ' => ['queue-name ' ],
138
+ ]);
139
+ }
140
+
141
+ public function testShouldExecuteConsumptionWhenProcessorImplementsQueueSubscriberInterface ()
142
+ {
143
+ $ processor = new QueueSubscriberProcessor ();
144
+
145
+ $ context = $ this ->createContextMock ();
146
+ $ context
147
+ ->expects ($ this ->once ())
148
+ ->method ('close ' )
149
+ ;
150
+
151
+ $ consumer = $ this ->createQueueConsumerMock ();
152
+ $ consumer
153
+ ->expects ($ this ->at (0 ))
154
+ ->method ('bind ' )
155
+ ->with ('fooSubscribedQueues ' , $ this ->identicalTo ($ processor ))
156
+ ;
157
+ $ consumer
158
+ ->expects ($ this ->at (1 ))
159
+ ->method ('bind ' )
160
+ ->with ('barSubscribedQueues ' , $ this ->identicalTo ($ processor ))
161
+ ;
162
+ $ consumer
163
+ ->expects ($ this ->at (2 ))
164
+ ->method ('consume ' )
165
+ ->with ($ this ->isInstanceOf (ChainExtension::class))
166
+ ;
167
+ $ consumer
168
+ ->expects ($ this ->at (3 ))
169
+ ->method ('getPsrContext ' )
170
+ ->will ($ this ->returnValue ($ context ))
171
+ ;
172
+
173
+ $ container = new Container ();
174
+ $ container ->set ('processor-service ' , $ processor );
175
+
176
+ $ command = new ContainerAwareConsumeMessagesCommand ($ consumer );
177
+ $ command ->setContainer ($ container );
178
+
179
+ $ tester = new CommandTester ($ command );
180
+ $ tester ->execute ([
181
+ 'processor-service ' => 'processor-service '
114
182
]);
115
183
}
116
184
0 commit comments