Skip to content

Commit 7e7cc3d

Browse files
committed
Merge branch '2.4-develop' into ACP2E-347
2 parents 7588fb1 + 1f37c78 commit 7e7cc3d

File tree

8 files changed

+147
-139
lines changed

8 files changed

+147
-139
lines changed

app/code/Magento/AsynchronousOperations/Model/BulkManagement.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,18 @@ public function __construct(
104104
*/
105105
public function scheduleBulk($bulkUuid, array $operations, $description, $userId = null)
106106
{
107-
$metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
108-
$connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
109-
// save bulk summary and related operations
110-
$connection->beginTransaction();
111107
$userType = $this->userContext->getUserType();
112108
if ($userType === null) {
113109
$userType = UserContextInterface::USER_TYPE_ADMIN;
114110
}
111+
if ($userId === null && $userType === UserContextInterface::USER_TYPE_ADMIN) {
112+
$userId = $this->userContext->getUserId();
113+
}
114+
115+
$metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
116+
$connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
117+
// save bulk summary and related operations
118+
$connection->beginTransaction();
115119
try {
116120
/** @var BulkSummaryInterface $bulkSummary */
117121
$bulkSummary = $this->bulkSummaryFactory->create();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
return [
8+
\Magento\Framework\App\ResourceConnection::class,
9+
\Magento\Framework\Config\Scope::class,
10+
\Magento\Framework\ObjectManager\RelationsInterface::class,
11+
\Magento\Framework\ObjectManager\ConfigInterface::class,
12+
\Magento\Framework\Interception\DefinitionInterface::class,
13+
\Magento\Framework\ObjectManager\DefinitionInterface::class,
14+
\Magento\Framework\Session\Config::class,
15+
\Magento\Framework\ObjectManager\Config\Mapper\Dom::class,
16+
];

dev/tests/integration/etc/install-config-mysql.php.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ return [
2323
'amqp-port' => '5672',
2424
'amqp-user' => 'guest',
2525
'amqp-password' => 'guest',
26+
'consumers-wait-for-messages' => '0',
2627
];

dev/tests/integration/framework/Magento/TestFramework/ObjectManager.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@ class ObjectManager extends \Magento\Framework\App\ObjectManager
2323
/**
2424
* @var array
2525
*/
26-
protected $persistedInstances = [
27-
\Magento\Framework\App\ResourceConnection::class,
28-
\Magento\Framework\Config\Scope::class,
29-
\Magento\Framework\ObjectManager\RelationsInterface::class,
30-
\Magento\Framework\ObjectManager\ConfigInterface::class,
31-
\Magento\Framework\Interception\DefinitionInterface::class,
32-
\Magento\Framework\ObjectManager\DefinitionInterface::class,
33-
\Magento\Framework\Session\Config::class,
34-
\Magento\Framework\ObjectManager\Config\Mapper\Dom::class,
35-
];
26+
protected $persistedInstances = [];
27+
28+
/**
29+
* Set list of instances that should be persistent.
30+
*
31+
* @param array $persistedInstances
32+
*/
33+
public function setPersistedInstances(array $persistedInstances): void
34+
{
35+
$this->persistedInstances = $persistedInstances;
36+
}
3637

3738
/**
3839
* Clear InstanceManager cache.

dev/tests/integration/framework/Magento/TestFramework/ObjectManagerFactory.php

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@
2323
class ObjectManagerFactory extends \Magento\Framework\App\ObjectManagerFactory
2424
{
2525
/**
26-
* Locator class name
27-
*
2826
* @var string
2927
*/
3028
protected $_locatorClassName = ObjectManager::class;
3129

3230
/**
33-
* Config class name
34-
*
3531
* @var string
3632
*/
3733
protected $_configClassName = \Magento\TestFramework\ObjectManager\Config::class;
@@ -80,6 +76,30 @@ public function restore(ObjectManager $objectManager, $directoryList, array $arg
8076
return $objectManager;
8177
}
8278

79+
/**
80+
* Read config from provided directory
81+
*
82+
* @param string $directory
83+
* @return array
84+
* @throws LocalizedException
85+
*/
86+
private function readCustomConfig(string $directory): array
87+
{
88+
$path = __DIR__ . '/../../../etc/di/' . $directory . '/';
89+
$files = glob($path . '*.php');
90+
91+
$data = [];
92+
foreach ($files as $file) {
93+
if (!is_readable($file)) {
94+
throw new LocalizedException(__("'%1' is not readable file.", $file));
95+
}
96+
$data[] = include $file;
97+
}
98+
$data = array_merge([], ...$data);
99+
100+
return $data;
101+
}
102+
83103
/**
84104
* Load primary config
85105
*
@@ -98,23 +118,25 @@ protected function _loadPrimaryConfig(DirectoryList $directoryList, $driverPool,
98118
'default_setup' => ['type' => ConnectionAdapter::class]
99119
]
100120
);
101-
$diPreferences = [];
102-
$diPreferencesPath = __DIR__ . '/../../../etc/di/preferences/';
103-
104-
$preferenceFiles = glob($diPreferencesPath . '*.php');
105-
106-
foreach ($preferenceFiles as $file) {
107-
if (!is_readable($file)) {
108-
throw new LocalizedException(__("'%1' is not readable file.", $file));
109-
}
110-
$diPreferences = array_replace($diPreferences, include $file);
111-
}
112-
121+
$diPreferences = $this->readCustomConfig('preferences');
113122
$this->_primaryConfigData['preferences'] = array_replace(
114123
$this->_primaryConfigData['preferences'],
115124
$diPreferences
116125
);
117126
}
118127
return $this->_primaryConfigData;
119128
}
129+
130+
/**
131+
* @inheritdoc
132+
*/
133+
public function create(array $arguments)
134+
{
135+
/** @var \Magento\TestFramework\ObjectManager $objectManager */
136+
$objectManager = parent::create($arguments);
137+
$persistedInstances = $this->readCustomConfig('persistedInstances');
138+
$objectManager->setPersistedInstances($persistedInstances);
139+
140+
return $objectManager;
141+
}
120142
}

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/ObjectManagerTest.php

Lines changed: 67 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,34 @@
1616
use Magento\TestFramework\ObjectManager;
1717
use Magento\TestFramework\ObjectManager\Config;
1818

19+
/**
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
*/
1922
class ObjectManagerTest extends \PHPUnit\Framework\TestCase
2023
{
2124
/**
22-
* @var array Instances that shouldn't be destroyed by clearing cache.
25+
* Instances that shouldn't be destroyed by clearing cache.
26+
*
27+
* @var array
2328
*/
24-
private static $persistedInstances = [
25-
ResourceConnection::class,
26-
\Magento\Framework\Config\Scope::class,
27-
\Magento\Framework\ObjectManager\RelationsInterface::class,
28-
\Magento\Framework\ObjectManager\ConfigInterface::class,
29-
\Magento\Framework\Interception\DefinitionInterface::class,
30-
\Magento\Framework\ObjectManager\DefinitionInterface::class,
31-
\Magento\Framework\Session\Config::class,
32-
\Magento\Framework\ObjectManager\Config\Mapper\Dom::class
33-
];
29+
private $persistedInstances;
3430

3531
/**
36-
* @var string Instance that should be destroyed by clearing cache.
32+
* @inheritdoc
3733
*/
38-
private static $notPersistedInstance = CacheInterface::class;
34+
protected function setUp(): void
35+
{
36+
$this->persistedInstances = [
37+
ResourceConnection::class,
38+
\Magento\Framework\Config\Scope::class,
39+
\Magento\Framework\ObjectManager\RelationsInterface::class,
40+
\Magento\Framework\ObjectManager\ConfigInterface::class,
41+
\Magento\Framework\Interception\DefinitionInterface::class,
42+
\Magento\Framework\ObjectManager\DefinitionInterface::class,
43+
\Magento\Framework\Session\Config::class,
44+
\Magento\Framework\ObjectManager\Config\Mapper\Dom::class,
45+
];
46+
}
3947

4048
/**
4149
* Tests that the scope of persisted instances doesn't clear after Object Manager cache clearing.
@@ -44,14 +52,11 @@ class ObjectManagerTest extends \PHPUnit\Framework\TestCase
4452
*/
4553
public function testInstancePersistingAfterClearCache()
4654
{
47-
foreach (self::$persistedInstances as $className) {
48-
$sharedInstances[$className] = $this->createInstanceMock($className);
55+
$sharedInstances = [];
56+
foreach ($this->persistedInstances as $className) {
57+
$sharedInstances[$className] = $this->createMock($className);
4958
}
50-
51-
$config = $this->getObjectManagerConfigMock();
52-
$factory = $this->getObjectManagerFactoryMock();
53-
54-
$objectManager = new ObjectManager($factory, $config, $sharedInstances);
59+
$objectManager = $this->createObjectManager($sharedInstances);
5560
$objectManager->clearCache();
5661

5762
$this->assertSame(
@@ -64,7 +69,7 @@ public function testInstancePersistingAfterClearCache()
6469
$objectManager->get(\Magento\Framework\App\ObjectManager::class),
6570
"Object manager instance should be the same after cache clearing."
6671
);
67-
foreach (self::$persistedInstances as $className) {
72+
foreach ($this->persistedInstances as $className) {
6873
$this->assertSame(
6974
$sharedInstances[$className],
7075
$objectManager->get($className),
@@ -80,16 +85,14 @@ public function testInstancePersistingAfterClearCache()
8085
*/
8186
public function testInstanceDestroyingAfterClearCache()
8287
{
83-
$sharedInstances[self::$notPersistedInstance] = $this->createInstanceMock(self::$notPersistedInstance);
84-
$config = $this->getObjectManagerConfigMock();
85-
$factory = $this->getObjectManagerFactoryMock();
86-
87-
$objectManager = new ObjectManager($factory, $config, $sharedInstances);
88+
$notPersistedInstance = CacheInterface::class;
89+
$sharedInstances = [$notPersistedInstance => $this->createMock($notPersistedInstance)];
90+
$objectManager = $this->createObjectManager($sharedInstances);
8891
$objectManager->clearCache();
8992

90-
$this->assertNull(
91-
$objectManager->get(self::$notPersistedInstance),
92-
'Instance of ' . self::$notPersistedInstance . ' should be destroyed after cache clearing.'
93+
$this->assertNotSame(
94+
$objectManager->get($notPersistedInstance),
95+
'Instance of ' . $notPersistedInstance . ' should be destroyed after cache clearing.'
9396
);
9497
}
9598

@@ -100,10 +103,7 @@ public function testInstanceDestroyingAfterClearCache()
100103
*/
101104
public function testInstanceRecreatingAfterClearCache()
102105
{
103-
$config = $this->getObjectManagerConfigMock();
104-
$factory = $this->getObjectManagerFactoryMock();
105-
106-
$objectManager = new ObjectManager($factory, $config);
106+
$objectManager = $this->createObjectManager();
107107
$instance = $objectManager->get(DataObject::class);
108108

109109
$this->assertSame($instance, $objectManager->get(DataObject::class));
@@ -122,10 +122,8 @@ public function testInstanceRecreatingAfterClearCache()
122122
*/
123123
public function testIsEmptyMappedTableNamesAfterClearCache()
124124
{
125-
$config = $this->getObjectManagerConfigMock();
126-
$factory = $this->getObjectManagerFactoryMock();
127-
128-
$objectManager = new ObjectManager($factory, $config);
125+
$objectManager = $this->createObjectManager();
126+
$objectManager->setPersistedInstances($this->persistedInstances);
129127

130128
$resourceConnection = $this->getResourceConnection();
131129
$resourceConnection->setMappedTableName('tableName', 'mappedTableName');
@@ -141,70 +139,16 @@ public function testIsEmptyMappedTableNamesAfterClearCache()
141139
);
142140
}
143141

144-
/**
145-
* @return Config|\PHPUnit\Framework\MockObject\MockObject
146-
*/
147-
private function getObjectManagerConfigMock()
148-
{
149-
$configMock = $this->getMockBuilder(Config::class)
150-
->disableOriginalConstructor()
151-
->getMock();
152-
$configMock->method('getPreference')
153-
->willReturnCallback(
154-
function ($className) {
155-
return $className;
156-
}
157-
);
158-
159-
return $configMock;
160-
}
161-
162-
/**
163-
* @return FactoryInterface|\PHPUnit\Framework\MockObject\MockObject
164-
*/
165-
private function getObjectManagerFactoryMock()
166-
{
167-
$factory = $this->getMockForAbstractClass(FactoryInterface::class);
168-
$factory->method('create')->willReturnCallback(
169-
function ($className) {
170-
if ($className === DataObject::class) {
171-
return $this->getMockBuilder(DataObject::class)
172-
->disableOriginalConstructor()
173-
->getMock();
174-
}
175-
}
176-
);
177-
178-
return $factory;
179-
}
180-
181-
/**
182-
* Returns mock of instance.
183-
*
184-
* @param string $className
185-
* @return \PHPUnit\Framework\MockObject\MockObject
186-
*/
187-
private function createInstanceMock($className)
188-
{
189-
return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock();
190-
}
191-
192142
/**
193143
* Returns ResourceConnection.
194144
*
195145
* @return ResourceConnection
196146
*/
197-
private function getResourceConnection()
147+
private function getResourceConnection(): ResourceConnection
198148
{
199-
$configInterface = $this->getMockForAbstractClass(
200-
ConfigInterface::class
201-
);
202-
$connectionFactory = $this->getMockForAbstractClass(
203-
ConnectionFactoryInterface::class
204-
);
205-
$deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
206-
->disableOriginalConstructor()
207-
->getMock();
149+
$configInterface = $this->createMock(ConfigInterface::class);
150+
$connectionFactory = $this->createMock(ConnectionFactoryInterface::class);
151+
$deploymentConfig = $this->createMock(DeploymentConfig::class);
208152
$resourceConnection = new ResourceConnection(
209153
$configInterface,
210154
$connectionFactory,
@@ -213,4 +157,32 @@ private function getResourceConnection()
213157

214158
return $resourceConnection;
215159
}
160+
161+
/**
162+
* Create instance of object manager.
163+
*
164+
* @param array $sharedInstances
165+
* @return ObjectManager
166+
*/
167+
private function createObjectManager(array $sharedInstances = []): ObjectManager
168+
{
169+
$factory = $this->createMock(FactoryInterface::class);
170+
$factory->method('create')
171+
->willReturnCallback(
172+
function ($className) {
173+
return $this->createMock($className);
174+
}
175+
);
176+
$configMock = $this->createMock(Config::class);
177+
$configMock->method('getPreference')
178+
->willReturnCallback(
179+
function ($className) {
180+
return $className;
181+
}
182+
);
183+
$objectManager = new ObjectManager($factory, $configMock, $sharedInstances);
184+
$objectManager->setPersistedInstances($this->persistedInstances);
185+
186+
return $objectManager;
187+
}
216188
}

0 commit comments

Comments
 (0)