Skip to content

Commit 69c1a71

Browse files
committed
CNS with MQ Service - Integration & Web Api builds are failing
1 parent 581b7ef commit 69c1a71

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

lib/internal/Magento/Framework/Amqp/Connection/Factory.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
namespace Magento\Framework\Amqp\Connection;
99

1010
use Magento\Framework\App\ObjectManager;
11+
use PhpAmqpLib\Connection\AMQPConnectionFactory;
12+
use PhpAmqpLib\Connection\AMQPConnectionConfig;
1113
use PhpAmqpLib\Connection\AbstractConnection;
12-
use PhpAmqpLib\Connection\AMQPSSLConnection;
13-
use PhpAmqpLib\Connection\AMQPStreamConnection;
1414

1515
/**
1616
* Create connection based on options.
@@ -25,20 +25,50 @@ class Factory
2525
*/
2626
public function create(FactoryOptions $options): AbstractConnection
2727
{
28-
$connectionType = $options->isSslEnabled() ? AMQPSSLConnection::class : AMQPStreamConnection::class;
29-
$parameters = [
30-
'host' => $options->getHost(),
31-
'port' => $options->getPort(),
32-
'user' => $options->getUsername(),
33-
'password' => $options->getPassword(),
34-
'vhost' => $options->getVirtualHost() !== null ? $options->getVirtualHost() : '/',
35-
];
28+
$config = ObjectManager::getInstance()->create(AMQPConnectionConfig::class);
3629

30+
// Set host, port, user, password, and vhost from options
31+
$config->setHost($options->getHost());
32+
$config->setPort((int)$options->getPort());
33+
$config->setUser($options->getUsername());
34+
$config->setPassword($options->getPassword());
35+
$config->setVhost($options->getVirtualHost() !== null ? $options->getVirtualHost() : '/');
36+
37+
// Set SSL options if SSL is enabled
3738
if ($options->isSslEnabled()) {
38-
$parameters['ssl_options'] = $options->getSslOptions() !== null
39-
? $options->getSslOptions()
40-
: ['verify_peer' => true];
39+
$config->setIsSecure(true);
40+
$sslOptions = $options->getSslOptions();
41+
if ($sslOptions) {
42+
if (isset($sslOptions['cafile'])) {
43+
$config->setSslCaCert($sslOptions['cafile']);
44+
}
45+
if (isset($sslOptions['local_cert'])) {
46+
$config->setSslCert($sslOptions['local_cert']);
47+
}
48+
if (isset($sslOptions['local_pk'])) {
49+
$config->setSslKey($sslOptions['local_pk']);
50+
}
51+
if (isset($sslOptions['verify_peer'])) {
52+
$config->setSslVerify($sslOptions['verify_peer']);
53+
}
54+
if (isset($sslOptions['verify_peer_name'])) {
55+
$config->setSslVerifyName($sslOptions['verify_peer_name']);
56+
}
57+
if (isset($sslOptions['passphrase'])) {
58+
$config->setSslPassPhrase($sslOptions['passphrase']);
59+
}
60+
if (isset($sslOptions['ciphers'])) {
61+
$config->setSslCiphers($sslOptions['ciphers']);
62+
}
63+
} else {
64+
// Default SSL verification option
65+
$config->setSslVerify(true);
66+
}
67+
} else {
68+
$config->setIsSecure(false);
4169
}
42-
return ObjectManager::getInstance()->create($connectionType, $parameters);
70+
71+
// Use the connection factory to create the connection
72+
return AMQPConnectionFactory::create($config);
4373
}
4474
}

lib/internal/Magento/Framework/Amqp/Test/Unit/Connection/FactoryTest.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
use Magento\Framework\Amqp\Connection\FactoryOptions;
1212
use Magento\Framework\ObjectManagerInterface;
1313
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14-
use PhpAmqpLib\Connection\AMQPSSLConnection;
15-
use PhpAmqpLib\Connection\AMQPStreamConnection;
14+
use PhpAmqpLib\Connection\AMQPConnectionConfig;
1615
use PHPUnit\Framework\MockObject\MockObject;
1716
use PHPUnit\Framework\TestCase;
1817

@@ -75,9 +74,9 @@ protected function setUp(): void
7574
* @return void
7675
* @dataProvider connectionDataProvider
7776
*/
78-
public function testSSLConnection($sslEnabled, $connectionClass)
77+
public function testSSLConnection(bool $sslEnabled, string $connectionClass)
7978
{
80-
$this->optionsMock->expects($this->exactly(2))
79+
$this->optionsMock->expects($this->once())
8180
->method('isSslEnabled')
8281
->willReturn($sslEnabled);
8382
$this->optionsMock->expects($this->once())
@@ -99,10 +98,18 @@ public function testSSLConnection($sslEnabled, $connectionClass)
9998
->method('getSslOptions')
10099
->willReturn(null);
101100

101+
$connection = $this->objectManager->getObject($connectionClass, [
102+
'127.0.0.1',
103+
'5672',
104+
'guest',
105+
'guest',
106+
'/']
107+
);
108+
102109
$this->objectManagerInterface->expects($this->any())
103110
->method('create')
104111
->with($connectionClass)
105-
->willReturn($this->createMock($connectionClass));
112+
->willReturn($connection);
106113

107114
\Magento\Framework\App\ObjectManager::setInstance($this->objectManagerInterface);
108115

@@ -118,12 +125,12 @@ public static function connectionDataProvider()
118125
{
119126
return [
120127
[
121-
'sslEnabled' => true,
122-
'connectionClass' => AMQPSSLConnection::class,
128+
'ssl_enabled' => true,
129+
'connection_class' => AMQPConnectionConfig::class,
123130
],
124131
[
125-
'sslEnabled' => false,
126-
'connectionClass' => AMQPStreamConnection::class,
132+
'ssl_enabled' => false,
133+
'connection_class' => AMQPConnectionConfig::class,
127134
],
128135
];
129136
}

0 commit comments

Comments
 (0)