1616use Magento \TestFramework \ObjectManager ;
1717use Magento \TestFramework \ObjectManager \Config ;
1818
19+ /**
20+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+ */
1922class 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