|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RabbitMqModule\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Test\PHPUnit\Controller\AbstractConsoleControllerTestCase; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ConsumerControllerTest |
|
9
|
|
|
* @package RabbitMqModule\Controller |
|
10
|
|
|
*/ |
|
11
|
|
|
class ConsumerControllerTest extends AbstractConsoleControllerTestCase |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
protected function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
$config = include __DIR__.'/../../TestConfiguration.php.dist'; |
|
17
|
|
|
$this->setApplicationConfig($config); |
|
18
|
|
|
parent::setUp(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testDispatchWithTestConsumer() |
|
22
|
|
|
{ |
|
23
|
|
|
$consumer = $this->getMockBuilder('RabbitMqModule\Consumer') |
|
24
|
|
|
->setMethods(['consume']) |
|
25
|
|
|
->disableOriginalConstructor() |
|
26
|
|
|
->getMock(); |
|
27
|
|
|
$consumer |
|
28
|
|
|
->expects(static::once()) |
|
29
|
|
|
->method('consume'); |
|
30
|
|
|
|
|
31
|
|
|
$serviceManager = $this->getApplicationServiceLocator(); |
|
32
|
|
|
$serviceManager->setAllowOverride(true); |
|
33
|
|
|
$serviceManager->setService('rabbitmq_module.consumer.foo', $consumer); |
|
34
|
|
|
|
|
35
|
|
|
ob_start(); |
|
36
|
|
|
$this->dispatch('rabbitmq-module consumer foo'); |
|
37
|
|
|
ob_end_clean(); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertResponseStatusCode(0); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testDispatchWithInvalidTestConsumer() |
|
43
|
|
|
{ |
|
44
|
|
|
ob_start(); |
|
45
|
|
|
$this->dispatch('rabbitmq-module consumer foo'); |
|
46
|
|
|
$output = ob_get_clean(); |
|
47
|
|
|
|
|
48
|
|
|
static::assertRegExp('/No consumer with name "foo" found/', $output); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertResponseStatusCode(1); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testStopConsumerController() |
|
54
|
|
|
{ |
|
55
|
|
|
$consumer = $this->getMockBuilder('RabbitMqModule\Consumer') |
|
56
|
|
|
->setMethods(['forceStopConsumer', 'stopConsuming']) |
|
57
|
|
|
->disableOriginalConstructor() |
|
58
|
|
|
->getMock(); |
|
59
|
|
|
|
|
60
|
|
|
$consumer->expects(static::once()) |
|
61
|
|
|
->method('forceStopConsumer'); |
|
62
|
|
|
|
|
63
|
|
|
$consumer->expects(static::once()) |
|
64
|
|
|
->method('stopConsuming'); |
|
65
|
|
|
|
|
66
|
|
|
$container = $this->getMockBuilder('Zend\ServiceManager\ServiceLocatorInterface') |
|
67
|
|
|
->getMock(); |
|
68
|
|
|
|
|
69
|
|
|
$stub = $this->getMockBuilder('RabbitMqModule\\Controller\\ConsumerController') |
|
70
|
|
|
->setConstructorArgs([$container]) |
|
71
|
|
|
->setMethods(['callExit']) |
|
72
|
|
|
->getMock(); |
|
73
|
|
|
|
|
74
|
|
|
$stub->expects(static::once()) |
|
75
|
|
|
->method('callExit'); |
|
76
|
|
|
|
|
77
|
|
|
/** @var \RabbitMqModule\Consumer $consumer */ |
|
78
|
|
|
/** @var ConsumerController $controller */ |
|
79
|
|
|
$controller = $stub; |
|
80
|
|
|
$controller->setConsumer($consumer); |
|
81
|
|
|
|
|
82
|
|
|
$controller->stopConsumer(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testDispatchWithoutSignals() |
|
86
|
|
|
{ |
|
87
|
|
|
$consumer = $this->getMockBuilder('RabbitMqModule\Consumer') |
|
88
|
|
|
->setMethods(['consume']) |
|
89
|
|
|
->disableOriginalConstructor() |
|
90
|
|
|
->getMock(); |
|
91
|
|
|
|
|
92
|
|
|
$consumer |
|
93
|
|
|
->expects(static::once()) |
|
94
|
|
|
->method('consume'); |
|
95
|
|
|
|
|
96
|
|
|
$serviceManager = $this->getApplicationServiceLocator(); |
|
97
|
|
|
$serviceManager->setAllowOverride(true); |
|
98
|
|
|
$serviceManager->setService('rabbitmq_module.consumer.foo', $consumer); |
|
99
|
|
|
|
|
100
|
|
|
ob_start(); |
|
101
|
|
|
$this->dispatch('rabbitmq-module consumer foo --without-signals'); |
|
102
|
|
|
ob_end_clean(); |
|
103
|
|
|
|
|
104
|
|
|
static::assertTrue(defined('AMQP_WITHOUT_SIGNALS')); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertResponseStatusCode(0); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function testListConsumersWithNoConsumers() |
|
110
|
|
|
{ |
|
111
|
|
|
ob_start(); |
|
112
|
|
|
$this->dispatch('rabbitmq-module list consumers'); |
|
113
|
|
|
ob_end_clean(); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertConsoleOutputContains('No consumers defined!'); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertResponseStatusCode(0); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testListConsumersWithNoConfigKey() |
|
121
|
|
|
{ |
|
122
|
|
|
$serviceManager = $this->getApplicationServiceLocator(); |
|
123
|
|
|
$serviceManager->setAllowOverride(true); |
|
124
|
|
|
/** @var array $configuration */ |
|
125
|
|
|
$configuration = $serviceManager->get('Configuration'); |
|
126
|
|
|
unset($configuration['rabbitmq_module']); |
|
127
|
|
|
$serviceManager->setService('Configuration', $configuration); |
|
128
|
|
|
|
|
129
|
|
|
ob_start(); |
|
130
|
|
|
$this->dispatch('rabbitmq-module list consumers'); |
|
131
|
|
|
ob_end_clean(); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertConsoleOutputContains('No \'rabbitmq_module.consumer\' configuration key found!'); |
|
134
|
|
|
|
|
135
|
|
|
$this->assertResponseStatusCode(0); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testListConsumers() |
|
139
|
|
|
{ |
|
140
|
|
|
$serviceManager = $this->getApplicationServiceLocator(); |
|
141
|
|
|
$serviceManager->setAllowOverride(true); |
|
142
|
|
|
/** @var array $configuration */ |
|
143
|
|
|
$configuration = $serviceManager->get('Configuration'); |
|
144
|
|
|
$configuration['rabbitmq_module']['consumer'] = [ |
|
145
|
|
|
'consumer_key1' => [], |
|
146
|
|
|
'consumer_key2' => ['description' => 'foo description'] |
|
147
|
|
|
]; |
|
148
|
|
|
$serviceManager->setService('Configuration', $configuration); |
|
149
|
|
|
|
|
150
|
|
|
ob_start(); |
|
151
|
|
|
$this->dispatch('rabbitmq-module list consumers'); |
|
152
|
|
|
$content = ob_get_contents(); |
|
153
|
|
|
ob_end_clean(); |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
static::assertTrue(false !== strpos($content, 'consumer_key1')); |
|
157
|
|
|
static::assertTrue(false !== strpos($content, 'consumer_key2')); |
|
158
|
|
|
static::assertTrue(false !== strpos($content, 'foo description')); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertResponseStatusCode(0); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|