Skip to content

Commit 20e621d

Browse files
Merge pull request #86 from magento-thunder/MAGECLOUD-1246-2002
MAGECLOUD-1246: Cover different RabbitMQ relationship names
2 parents 697fad2 + 728aded commit 20e621d

File tree

2 files changed

+120
-43
lines changed
  • src
    • Process/Deploy/InstallUpdate/ConfigUpdate
    • Test/Unit/Process/Deploy/InstallUpdate/ConfigUpdate

2 files changed

+120
-43
lines changed

src/Process/Deploy/InstallUpdate/ConfigUpdate/Amqp.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ class Amqp implements ProcessInterface
3333
*/
3434
private $configReader;
3535

36+
/**
37+
* Possible names for amqp relationship
38+
*
39+
* @var array
40+
*/
41+
private $possibleRelationshipNames = ['rabbitmq', 'mq', 'amqp'];
42+
3643
/**
3744
* @param Environment $environment
3845
* @param ConfigReader $configReader
@@ -56,7 +63,7 @@ public function __construct(
5663
*/
5764
public function execute()
5865
{
59-
$mqConfig = $this->environment->getRelationship('mq');
66+
$mqConfig = $this->getAmqpConfig();
6067
$config = $this->configReader->read();
6168

6269
if (count($mqConfig)) {
@@ -66,7 +73,7 @@ public function execute()
6673
$config['queue']['amqp']['port'] = $amqpConfig['port'];
6774
$config['queue']['amqp']['user'] = $amqpConfig['username'];
6875
$config['queue']['amqp']['password'] = $amqpConfig['password'];
69-
$config['queue']['amqp']['virtualhost'] = '/';
76+
$config['queue']['amqp']['virtualhost'] = isset($amqpConfig['vhost']) ? $amqpConfig['vhost'] : '/';
7077
$config['queue']['amqp']['ssl'] = '';
7178
} else {
7279
$config = $this->removeAmqpConfig($config);
@@ -95,4 +102,22 @@ private function removeAmqpConfig(array $config)
95102

96103
return $config;
97104
}
105+
106+
/**
107+
* Finds if configuration exists for one of possible amqp relationship names and return first match,
108+
* amqp relationship can have different name on different environment.
109+
*
110+
* @return array
111+
*/
112+
private function getAmqpConfig(): array
113+
{
114+
foreach ($this->possibleRelationshipNames as $relationshipName) {
115+
$mqConfig = $this->environment->getRelationship($relationshipName);
116+
if (!empty($mqConfig)) {
117+
return $mqConfig;
118+
}
119+
}
120+
121+
return [];
122+
}
98123
}

src/Test/Unit/Process/Deploy/InstallUpdate/ConfigUpdate/AmqpTest.php

Lines changed: 93 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ protected function setUp()
6767
public function testExecuteWithoutAmqp()
6868
{
6969
$config = ['some config'];
70-
$this->environmentMock->expects($this->once())
70+
$this->environmentMock->expects($this->any())
7171
->method('getRelationship')
72-
->with('mq')
72+
->with($this->anything())
7373
->willReturn([]);
7474
$this->configReaderMock->expects($this->once())
7575
->method('read')
@@ -88,32 +88,18 @@ public function testExecuteWithoutAmqp()
8888
* @return void
8989
* @dataProvider executeAddUpdateDataProvider
9090
*/
91-
public function testExecuteAddUpdate(array $config)
91+
public function testExecuteAddUpdate(array $config, array $amqpConfig, array $resultConfig)
9292
{
93-
$amqpConfig = [[
94-
'host' => 'localhost',
95-
'port' => '777',
96-
'username' => 'login',
97-
'password' => 'pswd'
98-
]];
99-
$resultConfig = [
100-
'some config',
101-
'queue' => [
102-
'amqp' => [
103-
'host' => 'localhost',
104-
'port' => '777',
105-
'user' => 'login',
106-
'password' => 'pswd',
107-
'virtualhost' => '/',
108-
'ssl' => '',
109-
]
110-
],
111-
];
112-
113-
$this->environmentMock->expects($this->once())
93+
$this->environmentMock->expects($this->exactly(2))
11494
->method('getRelationship')
115-
->with('mq')
116-
->willReturn($amqpConfig);
95+
->withConsecutive(
96+
['rabbitmq'],
97+
['mq']
98+
)
99+
->willReturnOnConsecutiveCalls(
100+
[],
101+
$amqpConfig
102+
);
117103
$this->configReaderMock->expects($this->once())
118104
->method('read')
119105
->willReturn($config);
@@ -133,20 +119,86 @@ public function testExecuteAddUpdate(array $config)
133119
public function executeAddUpdateDataProvider(): array
134120
{
135121
return [
136-
[['some config']],
137-
[[
138-
'some config',
139-
'queue' => [
140-
'amqp' => [
141-
'host' => 'some-host',
142-
'port' => '888',
143-
'user' => 'mylogin',
144-
'password' => 'mysqwwd',
145-
'virtualhost' => 'virtualhost',
146-
'ssl' => '1',
147-
]
122+
[
123+
['some config'],
124+
[[
125+
'host' => 'localhost',
126+
'port' => '777',
127+
'username' => 'login',
128+
'password' => 'pswd',
129+
'vhost' => 'virtualhost'
130+
]],
131+
[
132+
'some config',
133+
'queue' => [
134+
'amqp' => [
135+
'host' => 'localhost',
136+
'port' => '777',
137+
'user' => 'login',
138+
'password' => 'pswd',
139+
'virtualhost' => 'virtualhost',
140+
'ssl' => '',
141+
]
142+
],
143+
]
144+
],
145+
[
146+
['some config'],
147+
[[
148+
'host' => 'localhost',
149+
'port' => '777',
150+
'username' => 'login',
151+
'password' => 'pswd',
152+
]],
153+
[
154+
'some config',
155+
'queue' => [
156+
'amqp' => [
157+
'host' => 'localhost',
158+
'port' => '777',
159+
'user' => 'login',
160+
'password' => 'pswd',
161+
'virtualhost' => '/',
162+
'ssl' => '',
163+
]
164+
],
165+
]
166+
],
167+
[
168+
[
169+
'some config',
170+
'queue' => [
171+
'amqp' => [
172+
'host' => 'some-host',
173+
'port' => '888',
174+
'user' => 'mylogin',
175+
'password' => 'mysqwwd',
176+
'virtualhost' => 'virtualhost',
177+
'ssl' => '1',
178+
]
179+
],
148180
],
149-
]]
181+
[[
182+
'host' => 'localhost',
183+
'port' => '777',
184+
'username' => 'login',
185+
'password' => 'pswd',
186+
'vhost' => 'virtualhost'
187+
]],
188+
[
189+
'some config',
190+
'queue' => [
191+
'amqp' => [
192+
'host' => 'localhost',
193+
'port' => '777',
194+
'user' => 'login',
195+
'password' => 'pswd',
196+
'virtualhost' => 'virtualhost',
197+
'ssl' => '',
198+
]
199+
],
200+
]
201+
]
150202
];
151203
}
152204

@@ -158,9 +210,9 @@ public function executeAddUpdateDataProvider(): array
158210
*/
159211
public function testExecuteRemoveAmqp(array $config, array $expectedConfig)
160212
{
161-
$this->environmentMock->expects($this->once())
213+
$this->environmentMock->expects($this->any())
162214
->method('getRelationship')
163-
->with('mq')
215+
->with($this->anything())
164216
->willReturn([]);
165217
$this->configReaderMock->expects($this->once())
166218
->method('read')

0 commit comments

Comments
 (0)