11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2012 Adobe
4+ * All Rights Reserved .
55 */
66namespace Magento \Framework \TestFramework \Unit \Helper ;
77
@@ -93,20 +93,22 @@ protected function _processSpecialCases($className, $arguments)
9393 */
9494 protected function _getResourceModelMock ()
9595 {
96- $ resourceMock = $ this ->_testObject ->getMockBuilder (\Magento \Framework \Module \ModuleResource::class)
97- ->disableOriginalConstructor ()
98- ->disableOriginalClone ()
99- ->disableArgumentCloning ()
100- ->disallowMockingUnknownTypes ()
101- ->onlyMethods (['getIdFieldName ' , '__sleep ' , '__wakeup ' ])
102- ->getMock ();
96+ $ reflection = new \ReflectionClass ($ this ->_testObject );
97+ $ method = $ reflection ->getMethod ('createPartialMock ' );
98+ $ method ->setAccessible (true );
99+ $ resourceMock = $ method ->invoke (
100+ $ this ->_testObject ,
101+ \Magento \Framework \Module \ModuleResource::class,
102+ ['getIdFieldName ' , '__sleep ' , '__wakeup ' ]
103+ );
104+ $ reflection = new \ReflectionClass ($ this ->_testObject );
105+ $ anyMethod = $ reflection ->getMethod ('any ' );
106+ $ anyMethod ->setAccessible (true );
103107 $ resourceMock ->expects (
104- $ this -> _testObject -> any ( )
108+ $ anyMethod -> invoke ( $ this -> _testObject )
105109 )->method (
106110 'getIdFieldName '
107- )->will (
108- $ this ->_testObject ->returnValue ('id ' )
109- );
111+ )->willReturn ('id ' );
110112
111113 return $ resourceMock ;
112114 }
@@ -119,17 +121,21 @@ protected function _getResourceModelMock()
119121 */
120122 protected function _getTranslatorMock ($ className )
121123 {
122- $ translator = $ this ->_testObject ->getMockBuilder ($ className )->disableOriginalConstructor ()->getMock ();
124+ $ reflection = new \ReflectionClass ($ this ->_testObject );
125+ $ method = $ reflection ->getMethod ('createMock ' );
126+ $ method ->setAccessible (true );
127+ $ translator = $ method ->invoke ($ this ->_testObject , $ className );
123128 $ translateCallback = function ($ arguments ) {
124129 return is_array ($ arguments ) ? vsprintf (array_shift ($ arguments ), $ arguments ) : '' ;
125130 };
131+ $ reflection = new \ReflectionClass ($ this ->_testObject );
132+ $ anyMethod = $ reflection ->getMethod ('any ' );
133+ $ anyMethod ->setAccessible (true );
126134 $ translator ->expects (
127- $ this -> _testObject -> any ( )
135+ $ anyMethod -> invoke ( $ this -> _testObject )
128136 )->method (
129137 'translate '
130- )->will (
131- $ this ->_testObject ->returnCallback ($ translateCallback )
132- );
138+ )->willReturnCallback ($ translateCallback );
133139 return $ translator ;
134140 }
135141
@@ -141,13 +147,11 @@ protected function _getTranslatorMock($className)
141147 */
142148 protected function _getMockWithoutConstructorCall ($ className )
143149 {
144- $ mock = $ this ->_testObject ->getMockBuilder ($ className )
145- ->disableOriginalConstructor ()
146- ->disableOriginalClone ()
147- ->disableArgumentCloning ()
148- ->disallowMockingUnknownTypes ()
149- ->getMock ();
150- return $ mock ;
150+ // Use reflection to call protected createMock method
151+ $ reflection = new \ReflectionClass ($ this ->_testObject );
152+ $ method = $ reflection ->getMethod ('createMock ' );
153+ $ method ->setAccessible (true );
154+ return $ method ->invoke ($ this ->_testObject , $ className );
151155 }
152156
153157 /**
@@ -191,24 +195,24 @@ public function getObject($className, array $arguments = [])
191195 protected function getBuilder ($ className , array $ arguments )
192196 {
193197 if (!isset ($ arguments ['objectFactory ' ])) {
194- $ objectFactory = $ this ->_testObject ->getMockBuilder (\Magento \Framework \Api \ObjectFactory::class)
198+ $ reflection = new \ReflectionClass ($ this ->_testObject );
199+ $ method = $ reflection ->getMethod ('getMockBuilder ' );
200+ $ method ->setAccessible (true );
201+ $ mockBuilder = $ method ->invoke ($ this ->_testObject , \Magento \Framework \Api \ObjectFactory::class);
202+
203+ // Use onlyMethods() with methods that actually exist in ObjectFactory
204+ $ objectFactory = $ mockBuilder ->onlyMethods (['create ' , 'get ' ])
195205 ->disableOriginalConstructor ()
196- ->disableOriginalClone ()
197- ->disableArgumentCloning ()
198- ->disallowMockingUnknownTypes ()
199- ->addMethods (['populateWithArray ' , 'populate ' ])
200- ->onlyMethods (['create ' ])
201206 ->getMock ();
202207
203- $ objectFactory ->expects ($ this ->_testObject ->any ())
204- ->method ('populateWithArray ' )
205- ->will ($ this ->_testObject ->returnSelf ());
206- $ objectFactory ->expects ($ this ->_testObject ->any ())
207- ->method ('populate ' )
208- ->will ($ this ->_testObject ->returnSelf ());
209- $ objectFactory ->expects ($ this ->_testObject ->any ())
208+ $ reflection = new \ReflectionClass ($ this ->_testObject );
209+ $ anyMethod = $ reflection ->getMethod ('any ' );
210+ $ anyMethod ->setAccessible (true );
211+
212+ // Only configure methods that actually exist in ObjectFactory
213+ $ objectFactory ->expects ($ anyMethod ->invoke ($ this ->_testObject ))
210214 ->method ('create ' )
211- ->will ( $ this -> _testObject -> returnCallback (
215+ ->willReturnCallback (
212216 function ($ className , $ arguments ) {
213217 $ reflectionClass = new \ReflectionClass ($ className );
214218 $ constructorMethod = $ reflectionClass ->getConstructor ();
@@ -232,7 +236,15 @@ function ($className, $arguments) {
232236 }
233237 return new $ className (...array_values ($ args ));
234238 }
235- ));
239+ );
240+
241+ $ objectFactory ->expects ($ anyMethod ->invoke ($ this ->_testObject ))
242+ ->method ('get ' )
243+ ->willReturnCallback (
244+ function ($ className ) {
245+ return $ this ->_getMockWithoutConstructorCall ($ className );
246+ }
247+ );
236248
237249 $ arguments ['objectFactory ' ] = $ objectFactory ;
238250 }
@@ -281,12 +293,10 @@ public function getConstructArguments($className, array $arguments = [])
281293 if ($ firstPosition !== false ) {
282294 $ parameterString = substr ($ parameterString , $ firstPosition + 11 );
283295 $ parameterString = substr ($ parameterString , 0 , strpos ($ parameterString , ' ' ));
284- $ object = $ this ->_testObject ->getMockBuilder ($ parameterString )
285- ->disableOriginalConstructor ()
286- ->disableOriginalClone ()
287- ->disableArgumentCloning ()
288- ->disallowMockingUnknownTypes ()
289- ->getMock ();
296+ $ reflection = new \ReflectionClass ($ this ->_testObject );
297+ $ method = $ reflection ->getMethod ('createMock ' );
298+ $ method ->setAccessible (true );
299+ $ object = $ method ->invoke ($ this ->_testObject , $ parameterString );
290300 }
291301 }
292302
@@ -310,20 +320,19 @@ public function getCollectionMock($className, array $data)
310320 $ className . ' does not instance of \Magento\Framework\Data\Collection '
311321 );
312322 }
313- $ mock = $ this ->_testObject ->getMockBuilder ($ className )
314- ->disableOriginalConstructor ()
315- ->disableOriginalClone ()
316- ->disableArgumentCloning ()
317- ->disallowMockingUnknownTypes ()
318- ->getMock ();
323+ $ reflection = new \ReflectionClass ($ this ->_testObject );
324+ $ method = $ reflection ->getMethod ('createMock ' );
325+ $ method ->setAccessible (true );
326+ $ mock = $ method ->invoke ($ this ->_testObject , $ className );
319327 $ iterator = new \ArrayIterator ($ data );
328+ $ reflection = new \ReflectionClass ($ this ->_testObject );
329+ $ anyMethod = $ reflection ->getMethod ('any ' );
330+ $ anyMethod ->setAccessible (true );
320331 $ mock ->expects (
321- $ this -> _testObject -> any ( )
332+ $ anyMethod -> invoke ( $ this -> _testObject )
322333 )->method (
323334 'getIterator '
324- )->will (
325- $ this ->_testObject ->returnValue ($ iterator )
326- );
335+ )->willReturn ($ iterator );
327336 return $ mock ;
328337 }
329338
@@ -370,16 +379,41 @@ public function setBackwardCompatibleProperty($object, $propertyName, $propertyV
370379 */
371380 public function prepareObjectManager (array $ map = [])
372381 {
373- $ objectManagerMock = $ this ->_testObject ->getMockBuilder (ObjectManagerInterface::class)
374- ->addMethods (['getInstance ' ])
375- ->onlyMethods (['get ' ])
376- ->getMockForAbstractClass ();
382+ $ reflection = new \ReflectionClass ($ this ->_testObject );
383+ $ method = $ reflection ->getMethod ('createMock ' );
384+ $ method ->setAccessible (true );
385+ $ objectManagerMock = $ method ->invoke (
386+ $ this ->_testObject ,
387+ ObjectManagerInterface::class
388+ );
377389
378- $ objectManagerMock ->method ('getInstance ' )->willReturnSelf ();
379390 $ objectManagerMock ->method ('get ' )->willReturnMap ($ map );
380391
381392 $ reflectionProperty = new \ReflectionProperty (AppObjectManager::class, '_instance ' );
382393 $ reflectionProperty ->setAccessible (true );
383394 $ reflectionProperty ->setValue ($ objectManagerMock , $ objectManagerMock );
384395 }
396+
397+ /**
398+ * Create a partial mock with reflection.
399+ *
400+ * @param string $className
401+ * @param array $methods
402+ * @return MockObject
403+ */
404+ public function createPartialMockWithReflection (string $ className , array $ methods ): MockObject
405+ {
406+ $ reflection = new \ReflectionClass ($ this ->_testObject );
407+ $ getMockBuilderMethod = $ reflection ->getMethod ('getMockBuilder ' );
408+ $ getMockBuilderMethod ->setAccessible (true );
409+ $ mockBuilder = $ getMockBuilderMethod ->invoke ($ this ->_testObject , $ className );
410+
411+ $ builderReflection = new \ReflectionClass ($ mockBuilder );
412+ $ methodsProperty = $ builderReflection ->getProperty ('methods ' );
413+ $ methodsProperty ->setAccessible (true );
414+ $ methodsProperty ->setValue ($ mockBuilder , $ methods );
415+
416+ $ mockBuilder ->disableOriginalConstructor ();
417+ return $ mockBuilder ->getMock ();
418+ }
385419}
0 commit comments