16
16
use Magento \TestFramework \ObjectManager ;
17
17
use Magento \TestFramework \ObjectManager \Config ;
18
18
19
+ /**
20
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
+ */
19
22
class ObjectManagerTest extends \PHPUnit \Framework \TestCase
20
23
{
21
24
/**
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
23
28
*/
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 ;
34
30
35
31
/**
36
- * @var string Instance that should be destroyed by clearing cache.
32
+ * @inheritdoc
37
33
*/
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
+ }
39
47
40
48
/**
41
49
* 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
44
52
*/
45
53
public function testInstancePersistingAfterClearCache ()
46
54
{
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 );
49
58
}
50
-
51
- $ config = $ this ->getObjectManagerConfigMock ();
52
- $ factory = $ this ->getObjectManagerFactoryMock ();
53
-
54
- $ objectManager = new ObjectManager ($ factory , $ config , $ sharedInstances );
59
+ $ objectManager = $ this ->createObjectManager ($ sharedInstances );
55
60
$ objectManager ->clearCache ();
56
61
57
62
$ this ->assertSame (
@@ -64,7 +69,7 @@ public function testInstancePersistingAfterClearCache()
64
69
$ objectManager ->get (\Magento \Framework \App \ObjectManager::class),
65
70
"Object manager instance should be the same after cache clearing. "
66
71
);
67
- foreach (self :: $ persistedInstances as $ className ) {
72
+ foreach ($ this -> persistedInstances as $ className ) {
68
73
$ this ->assertSame (
69
74
$ sharedInstances [$ className ],
70
75
$ objectManager ->get ($ className ),
@@ -80,16 +85,14 @@ public function testInstancePersistingAfterClearCache()
80
85
*/
81
86
public function testInstanceDestroyingAfterClearCache ()
82
87
{
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 );
88
91
$ objectManager ->clearCache ();
89
92
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. '
93
96
);
94
97
}
95
98
@@ -100,10 +103,7 @@ public function testInstanceDestroyingAfterClearCache()
100
103
*/
101
104
public function testInstanceRecreatingAfterClearCache ()
102
105
{
103
- $ config = $ this ->getObjectManagerConfigMock ();
104
- $ factory = $ this ->getObjectManagerFactoryMock ();
105
-
106
- $ objectManager = new ObjectManager ($ factory , $ config );
106
+ $ objectManager = $ this ->createObjectManager ();
107
107
$ instance = $ objectManager ->get (DataObject::class);
108
108
109
109
$ this ->assertSame ($ instance , $ objectManager ->get (DataObject::class));
@@ -122,10 +122,8 @@ public function testInstanceRecreatingAfterClearCache()
122
122
*/
123
123
public function testIsEmptyMappedTableNamesAfterClearCache ()
124
124
{
125
- $ config = $ this ->getObjectManagerConfigMock ();
126
- $ factory = $ this ->getObjectManagerFactoryMock ();
127
-
128
- $ objectManager = new ObjectManager ($ factory , $ config );
125
+ $ objectManager = $ this ->createObjectManager ();
126
+ $ objectManager ->setPersistedInstances ($ this ->persistedInstances );
129
127
130
128
$ resourceConnection = $ this ->getResourceConnection ();
131
129
$ resourceConnection ->setMappedTableName ('tableName ' , 'mappedTableName ' );
@@ -141,70 +139,16 @@ public function testIsEmptyMappedTableNamesAfterClearCache()
141
139
);
142
140
}
143
141
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
-
192
142
/**
193
143
* Returns ResourceConnection.
194
144
*
195
145
* @return ResourceConnection
196
146
*/
197
- private function getResourceConnection ()
147
+ private function getResourceConnection (): ResourceConnection
198
148
{
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);
208
152
$ resourceConnection = new ResourceConnection (
209
153
$ configInterface ,
210
154
$ connectionFactory ,
@@ -213,4 +157,32 @@ private function getResourceConnection()
213
157
214
158
return $ resourceConnection ;
215
159
}
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
+ }
216
188
}
0 commit comments