Skip to content

Commit 99b6f8c

Browse files
committed
MAGETWO-65657: Block "Recently Viewed Products" fatals on category scheduled updates preview
- Add clearing mapped table names list in resource connection.
1 parent a44be9d commit 99b6f8c

File tree

2 files changed

+202
-87
lines changed

2 files changed

+202
-87
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
/**
3-
* Test object manager
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
8-
96
namespace Magento\TestFramework;
107

8+
/**
9+
* ObjectManager for integration test framework.
10+
*/
1111
class ObjectManager extends \Magento\Framework\App\ObjectManager
1212
{
1313
/**
@@ -35,7 +35,7 @@ class ObjectManager extends \Magento\Framework\App\ObjectManager
3535
];
3636

3737
/**
38-
* Clear InstanceManager cache
38+
* Clear InstanceManager cache.
3939
*
4040
* @return \Magento\TestFramework\ObjectManager
4141
*/
@@ -72,35 +72,39 @@ public function clearCache()
7272
private function clearMappedTableNames()
7373
{
7474
$resourceConnection = $this->get(\Magento\Framework\App\ResourceConnection::class);
75-
$reflection = new \ReflectionClass($resourceConnection);
76-
$dataProperty = $reflection->getProperty('mappedTableNames');
77-
$dataProperty->setAccessible(true);
78-
$dataProperty->setValue($resourceConnection, null);
75+
if ($resourceConnection) {
76+
$reflection = new \ReflectionClass($resourceConnection);
77+
$dataProperty = $reflection->getProperty('mappedTableNames');
78+
$dataProperty->setAccessible(true);
79+
$dataProperty->setValue($resourceConnection, null);
80+
}
7981
}
8082

8183
/**
82-
* Add shared instance
84+
* Add shared instance.
8385
*
8486
* @param mixed $instance
8587
* @param string $className
88+
* @return void
8689
*/
8790
public function addSharedInstance($instance, $className)
8891
{
8992
$this->_sharedInstances[$className] = $instance;
9093
}
9194

9295
/**
93-
* Remove shared instance
96+
* Remove shared instance.
9497
*
9598
* @param string $className
99+
* @return void
96100
*/
97101
public function removeSharedInstance($className)
98102
{
99103
unset($this->_sharedInstances[$className]);
100104
}
101105

102106
/**
103-
* Set objectManager
107+
* Set objectManager.
104108
*
105109
* @param \Magento\Framework\ObjectManagerInterface $objectManager
106110
* @return \Magento\Framework\ObjectManagerInterface

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

Lines changed: 187 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,214 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
7-
/**
8-
* Test class for \Magento\Framework\ObjectManager\Test
9-
*/
106
namespace Magento\Test;
117

12-
/**
13-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14-
*/
8+
use Magento\Framework\App\CacheInterface;
9+
use Magento\Framework\App\DeploymentConfig;
10+
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\App\ResourceConnection\ConfigInterface;
12+
use Magento\Framework\DataObject;
13+
use Magento\Framework\Model\ResourceModel\Type\Db\ConnectionFactoryInterface;
14+
use Magento\Framework\ObjectManager\FactoryInterface;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\TestFramework\ObjectManager;
17+
use Magento\TestFramework\ObjectManager\Config;
18+
1519
class ObjectManagerTest extends \PHPUnit_Framework_TestCase
1620
{
1721
/**
18-
* Expected instance manager parametrized cache after clear
22+
* @var array Instances that shouldn't be destroyed by clearing cache.
23+
*/
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+
];
34+
35+
/**
36+
* @var string Instance that should be destroyed by clearing cache.
37+
*/
38+
private static $notPersistedInstance = CacheInterface::class;
39+
40+
/**
41+
* Tests that the scope of persisted instances doesn't clear after Object Manager cache clearing.
1942
*
20-
* @var array
43+
* @covers \Magento\TestFramework\ObjectManager::clearCache()
2144
*/
22-
protected $_instanceCache = ['hashShort' => [], 'hashLong' => []];
45+
public function testInstancePersistingAfterClearCache()
46+
{
47+
foreach (self::$persistedInstances as $className) {
48+
$sharedInstances[$className] = $this->createInstanceMock($className);
49+
}
50+
51+
$config = $this->getObjectManagerConfigMock();
52+
$factory = $this->getObjectManagerFactoryMock();
53+
54+
$objectManager = new ObjectManager($factory, $config, $sharedInstances);
55+
$objectManager->clearCache();
56+
57+
$this->assertSame(
58+
$objectManager,
59+
$objectManager->get(ObjectManagerInterface::class),
60+
"Object manager instance should be the same after cache clearing."
61+
);
62+
$this->assertSame(
63+
$objectManager,
64+
$objectManager->get(\Magento\Framework\App\ObjectManager::class),
65+
"Object manager instance should be the same after cache clearing."
66+
);
67+
foreach (self::$persistedInstances as $className) {
68+
$this->assertSame(
69+
$sharedInstances[$className],
70+
$objectManager->get($className),
71+
"Instance of {$className} should be the same after cache clearing."
72+
);
73+
}
74+
}
2375

24-
public function testClearCache()
76+
/**
77+
* Tests that instance is destroyed after Object Manager cache clearing.
78+
*
79+
* @covers \Magento\TestFramework\ObjectManager::clearCache()
80+
*/
81+
public function testInstanceDestroyingAfterClearCache()
82+
{
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+
$objectManager->clearCache();
89+
90+
$this->assertNull(
91+
$objectManager->get(self::$notPersistedInstance),
92+
'Instance of ' . self::$notPersistedInstance . ' should be destroyed after cache clearing.'
93+
);
94+
}
95+
96+
/**
97+
* Tests that instance is recreated after Object Manager cache clearing.
98+
*
99+
* @covers \Magento\TestFramework\ObjectManager::clearCache()
100+
*/
101+
public function testInstanceRecreatingAfterClearCache()
25102
{
26-
$resource = new \stdClass();
103+
$config = $this->getObjectManagerConfigMock();
104+
$factory = $this->getObjectManagerFactoryMock();
27105

28-
$configMock = $this->getMockBuilder(\Magento\TestFramework\ObjectManager\Config::class)
106+
$objectManager = new ObjectManager($factory, $config);
107+
$instance = $objectManager->get(DataObject::class);
108+
109+
$this->assertSame($instance, $objectManager->get(DataObject::class));
110+
$objectManager->clearCache();
111+
$this->assertNotSame(
112+
$instance,
113+
$objectManager->get(DataObject::class),
114+
'Instance ' . DataObject::class . ' should be recreated after cache clearing.'
115+
);
116+
}
117+
118+
/**
119+
* Tests that mapped table names list is empty after Object Manager cache clearing.
120+
*
121+
* @covers \Magento\TestFramework\ObjectManager::clearCache()
122+
*/
123+
public function testIsEmptyMappedTableNamesAfterClearCache()
124+
{
125+
$config = $this->getObjectManagerConfigMock();
126+
$factory = $this->getObjectManagerFactoryMock();
127+
128+
$objectManager = new ObjectManager($factory, $config);
129+
130+
$resourceConnection = $this->getResourceConnection();
131+
$resourceConnection->setMappedTableName('tableName', 'mappedTableName');
132+
$objectManager->addSharedInstance(
133+
$resourceConnection,
134+
ResourceConnection::class
135+
);
136+
$objectManager->clearCache();
137+
138+
$this->assertFalse(
139+
$objectManager->get(ResourceConnection::class)->getMappedTableName('tableName'),
140+
'Mapped table names list is not empty after Object Manager cache clearing.'
141+
);
142+
}
143+
144+
/**
145+
* @return Config|\PHPUnit_Framework_MockObject_MockObject
146+
*/
147+
private function getObjectManagerConfigMock()
148+
{
149+
$configMock = $this->getMockBuilder(Config::class)
29150
->disableOriginalConstructor()
30-
->setMethods(['getPreference', 'clean'])
31151
->getMock();
32-
33-
$configMock->expects($this->atLeastOnce())
34-
->method('getPreference')
35-
->will($this->returnCallback(
152+
$configMock->method('getPreference')
153+
->willReturnCallback(
36154
function ($className) {
37155
return $className;
38156
}
39-
));
40-
41-
$cache = $this->getMock(\Magento\Framework\App\CacheInterface::class);
42-
$configLoader = $this->getMock(\Magento\Framework\App\ObjectManager\ConfigLoader::class, [], [], '', false);
43-
$configCache = $this->getMock(\Magento\Framework\App\ObjectManager\ConfigCache::class, [], [], '', false);
44-
$primaryLoaderMock = $this->getMock(
45-
\Magento\Framework\App\ObjectManager\ConfigLoader\Primary::class,
46-
[],
47-
[],
48-
'',
49-
false
50-
);
51-
$factory = $this->getMock(\Magento\Framework\ObjectManager\FactoryInterface::class);
52-
$factory->expects($this->exactly(2))->method('create')->will(
53-
$this->returnCallback(
54-
function ($className) {
55-
if ($className === \Magento\Framework\DataObject::class) {
56-
return $this->getMock(\Magento\Framework\DataObject::class, [], [], '', false);
57-
}
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();
58174
}
59-
)
175+
}
60176
);
61177

62-
$connectionMock = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
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+
192+
/**
193+
* Returns ResourceConnection.
194+
*
195+
* @return ResourceConnection
196+
*/
197+
private function getResourceConnection()
198+
{
199+
$configInterface = $this->getMockForAbstractClass(
200+
ConfigInterface::class
201+
);
202+
$connectionFactory = $this->getMockForAbstractClass(
203+
ConnectionFactoryInterface::class
204+
);
205+
$deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
63206
->disableOriginalConstructor()
64207
->getMock();
65-
$sharedInstances = [
66-
\Magento\Framework\App\Cache\Type\Config::class => $cache,
67-
\Magento\Framework\App\ObjectManager\ConfigLoader::class => $configLoader,
68-
\Magento\Framework\App\ObjectManager\ConfigCache::class => $configCache,
69-
\Magento\Framework\Config\ReaderInterface::class =>
70-
$this->getMock(
71-
\Magento\Framework\Config\ReaderInterface::class
72-
),
73-
\Magento\Framework\Config\ScopeInterface::class =>
74-
$this->getMock(\Magento\Framework\Config\ScopeInterface::class),
75-
\Magento\Framework\Config\CacheInterface::class =>
76-
$this->getMock(\Magento\Framework\Config\CacheInterface::class),
77-
\Magento\Framework\Cache\FrontendInterface::class =>
78-
$this->getMock(\Magento\Framework\Cache\FrontendInterface::class),
79-
\Magento\Framework\App\ResourceConnection::class => $connectionMock,
80-
\Magento\Framework\App\ResourceConnection\Config::class =>
81-
$this->getMock(
82-
\Magento\Framework\App\ResourceConnection\Config::class,
83-
[],
84-
[],
85-
'',
86-
false
87-
)
88-
];
89-
$model = new \Magento\TestFramework\ObjectManager(
90-
$factory,
91-
$configMock,
92-
$sharedInstances,
93-
$primaryLoaderMock
208+
$resourceConnection = new ResourceConnection(
209+
$configInterface,
210+
$connectionFactory,
211+
$deploymentConfig
94212
);
95213

96-
$model->addSharedInstance($resource, \Magento\Framework\App\ResourceConnection::class);
97-
$instance1 = $model->get(\Magento\Framework\DataObject::class);
98-
99-
$this->assertSame($instance1, $model->get(\Magento\Framework\DataObject::class));
100-
$this->assertSame($model, $model->clearCache());
101-
$this->assertSame($model, $model->get(\Magento\Framework\ObjectManagerInterface::class));
102-
$this->assertSame($resource, $model->get(\Magento\Framework\App\ResourceConnection::class));
103-
$this->assertNotSame($instance1, $model->get(\Magento\Framework\DataObject::class));
214+
return $resourceConnection;
104215
}
105216
}

0 commit comments

Comments
 (0)