Skip to content

Commit d060ba9

Browse files
committed
Merge remote-tracking branch 'tango/MC-35996' into PR-10-14
2 parents 99b758b + e2ebea3 commit d060ba9

File tree

3 files changed

+70
-78
lines changed

3 files changed

+70
-78
lines changed

app/code/Magento/Analytics/ReportXml/ConnectionFactory.php

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,65 @@
66

77
namespace Magento\Analytics\ReportXml;
88

9-
use Magento\Framework\App\ResourceConnection;
10-
use Magento\Framework\ObjectManagerInterface;
9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\ResourceConnection\ConfigInterface as ResourceConfigInterface;
11+
use Magento\Framework\Config\ConfigOptionsListConstants;
1112
use Magento\Framework\DB\Adapter\AdapterInterface;
13+
use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
1214

1315
/**
1416
* Creates connection instance for export according to existing one
17+
*
1518
* This connection does not use buffered statement, also this connection is not persistent
1619
*/
1720
class ConnectionFactory
1821
{
1922
/**
20-
* @var ResourceConnection
23+
* @var ResourceConfigInterface
2124
*/
22-
private $resourceConnection;
25+
private $resourceConfig;
2326

2427
/**
25-
* @var ObjectManagerInterface
28+
* @var DeploymentConfig
2629
*/
27-
private $objectManager;
30+
private $deploymentConfig;
2831

2932
/**
30-
* @param ResourceConnection $resourceConnection
31-
* @param ObjectManagerInterface $objectManager
33+
* @var ConnectionFactoryInterface
34+
*/
35+
private $connectionFactory;
36+
37+
/**
38+
* @param ResourceConfigInterface $resourceConfig
39+
* @param DeploymentConfig $deploymentConfig
40+
* @param ConnectionFactoryInterface $connectionFactory
3241
*/
3342
public function __construct(
34-
ResourceConnection $resourceConnection,
35-
ObjectManagerInterface $objectManager
43+
ResourceConfigInterface $resourceConfig,
44+
DeploymentConfig $deploymentConfig,
45+
ConnectionFactoryInterface $connectionFactory
3646
) {
37-
$this->resourceConnection = $resourceConnection;
38-
$this->objectManager = $objectManager;
47+
$this->resourceConfig = $resourceConfig;
48+
$this->deploymentConfig = $deploymentConfig;
49+
$this->connectionFactory = $connectionFactory;
3950
}
4051

4152
/**
4253
* Creates one-time connection for export
4354
*
44-
* @param string $connectionName
55+
* @param string $resourceName
4556
* @return AdapterInterface
4657
*/
47-
public function getConnection($connectionName)
58+
public function getConnection($resourceName)
4859
{
49-
$connection = $this->resourceConnection->getConnection($connectionName);
50-
$connectionClassName = get_class($connection);
51-
$configData = $connection->getConfig();
60+
$connectionName = $this->resourceConfig->getConnectionName($resourceName);
61+
$configData = $this->deploymentConfig->get(
62+
ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS . '/' . $connectionName
63+
);
5264
$configData['use_buffered_query'] = false;
5365
unset($configData['persistent']);
54-
return $this->objectManager->create(
55-
$connectionClassName,
56-
[
57-
'config' => $configData
58-
]
59-
);
66+
$connection = $this->connectionFactory->create($configData);
67+
68+
return $connection;
6069
}
6170
}

app/code/Magento/Analytics/Test/Unit/ReportXml/ConnectionFactoryTest.php

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,29 @@
88
namespace Magento\Analytics\Test\Unit\ReportXml;
99

1010
use Magento\Analytics\ReportXml\ConnectionFactory;
11-
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\App\DeploymentConfig;
12+
use Magento\Framework\App\ResourceConnection\ConfigInterface as ResourceConfigInterface;
1213
use Magento\Framework\DB\Adapter\AdapterInterface;
13-
use Magento\Framework\DB\Adapter\Pdo\Mysql as MysqlPdoAdapter;
14-
use Magento\Framework\ObjectManagerInterface;
15-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
14+
use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
1615
use PHPUnit\Framework\MockObject\MockObject;
1716
use PHPUnit\Framework\TestCase;
1817

1918
class ConnectionFactoryTest extends TestCase
2019
{
2120
/**
22-
* @var ResourceConnection|MockObject
21+
* @var ResourceConfigInterface|MockObject
2322
*/
24-
private $resourceConnectionMock;
23+
private $resourceConfigMock;
2524

2625
/**
27-
* @var ObjectManagerInterface|MockObject
26+
* @var DeploymentConfig|MockObject
2827
*/
29-
private $objectManagerMock;
28+
private $deploymentConfigMock;
3029

3130
/**
32-
* @var ConnectionFactory|MockObject
31+
* @var ConnectionFactoryInterface|MockObject
3332
*/
34-
private $connectionNewMock;
35-
36-
/**
37-
* @var AdapterInterface|MockObject
38-
*/
39-
private $connectionMock;
40-
41-
/**
42-
* @var ObjectManagerHelper
43-
*/
44-
private $objectManagerHelper;
33+
private $connectionFactoryMock;
4534

4635
/**
4736
* @var ConnectionFactory
@@ -53,47 +42,36 @@ class ConnectionFactoryTest extends TestCase
5342
*/
5443
protected function setUp(): void
5544
{
56-
$this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
57-
58-
$this->objectManagerMock = $this->getMockForAbstractClass(ObjectManagerInterface::class);
59-
60-
$this->connectionMock = $this->createMock(MysqlPdoAdapter::class);
61-
62-
$this->connectionNewMock = $this->createMock(MysqlPdoAdapter::class);
63-
64-
$this->objectManagerHelper = new ObjectManagerHelper($this);
65-
66-
$this->connectionFactory = $this->objectManagerHelper->getObject(
67-
ConnectionFactory::class,
68-
[
69-
'resourceConnection' => $this->resourceConnectionMock,
70-
'objectManager' => $this->objectManagerMock,
71-
]
45+
$this->resourceConfigMock = $this->createMock(ResourceConfigInterface::class);
46+
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
47+
$this->connectionFactoryMock = $this->createMock(ConnectionFactoryInterface::class);
48+
49+
$this->connectionFactory = new ConnectionFactory(
50+
$this->resourceConfigMock,
51+
$this->deploymentConfigMock,
52+
$this->connectionFactoryMock
7253
);
7354
}
7455

7556
public function testGetConnection()
7657
{
77-
$connectionName = 'read';
78-
79-
$this->resourceConnectionMock
80-
->expects($this->once())
81-
->method('getConnection')
82-
->with($connectionName)
83-
->willReturn($this->connectionMock);
84-
85-
$this->connectionMock
86-
->expects($this->once())
87-
->method('getConfig')
88-
->with()
89-
->willReturn(['persistent' => 1]);
90-
91-
$this->objectManagerMock
92-
->expects($this->once())
58+
$resourceName = 'default';
59+
60+
$this->resourceConfigMock->expects($this->once())
61+
->method('getConnectionName')
62+
->with($resourceName)
63+
->willReturn('default');
64+
$this->deploymentConfigMock->expects($this->once())
65+
->method('get')
66+
->with('db/connection/default')
67+
->willReturn(['host' => 'localhost', 'port' => 3306, 'persistent' => true]);
68+
$connectionMock = $this->createMock(AdapterInterface::class);
69+
$this->connectionFactoryMock->expects($this->once())
9370
->method('create')
94-
->with(get_class($this->connectionMock), ['config' => ['use_buffered_query' => false]])
95-
->willReturn($this->connectionNewMock);
71+
->with(['host' => 'localhost', 'port' => 3306, 'use_buffered_query' => false])
72+
->willReturn($connectionMock);
9673

97-
$this->assertSame($this->connectionNewMock, $this->connectionFactory->getConnection($connectionName));
74+
$connection = $this->connectionFactory->getConnection($resourceName);
75+
$this->assertSame($connectionMock, $connection);
9876
}
9977
}

app/code/Magento/Analytics/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,9 @@
266266
</argument>
267267
</arguments>
268268
</type>
269+
<type name="Magento\Analytics\ReportXml\ConnectionFactory">
270+
<arguments>
271+
<argument name="connectionFactory" xsi:type="object">Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactory</argument>
272+
</arguments>
273+
</type>
269274
</config>

0 commit comments

Comments
 (0)