|
3 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 |
| - |
7 |
| -/** |
8 |
| - * Test class for \Magento\Framework\ObjectManager\Test |
9 |
| - */ |
10 | 6 | namespace Magento\Test;
|
11 | 7 |
|
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 | + |
15 | 19 | class ObjectManagerTest extends \PHPUnit_Framework_TestCase
|
16 | 20 | {
|
17 | 21 | /**
|
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. |
19 | 42 | *
|
20 |
| - * @var array |
| 43 | + * @covers \Magento\TestFramework\ObjectManager::clearCache() |
21 | 44 | */
|
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 | + } |
23 | 75 |
|
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() |
25 | 102 | {
|
26 |
| - $resource = new \stdClass(); |
| 103 | + $config = $this->getObjectManagerConfigMock(); |
| 104 | + $factory = $this->getObjectManagerFactoryMock(); |
27 | 105 |
|
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) |
29 | 150 | ->disableOriginalConstructor()
|
30 |
| - ->setMethods(['getPreference', 'clean']) |
31 | 151 | ->getMock();
|
32 |
| - |
33 |
| - $configMock->expects($this->atLeastOnce()) |
34 |
| - ->method('getPreference') |
35 |
| - ->will($this->returnCallback( |
| 152 | + $configMock->method('getPreference') |
| 153 | + ->willReturnCallback( |
36 | 154 | function ($className) {
|
37 | 155 | return $className;
|
38 | 156 | }
|
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(); |
58 | 174 | }
|
59 |
| - ) |
| 175 | + } |
60 | 176 | );
|
61 | 177 |
|
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) |
63 | 206 | ->disableOriginalConstructor()
|
64 | 207 | ->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 |
94 | 212 | );
|
95 | 213 |
|
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; |
104 | 215 | }
|
105 | 216 | }
|
0 commit comments