1010use Exception ;
1111use Magento \FunctionalTestingFramework \DataGenerator \Handlers \DataObjectHandler ;
1212use Magento \FunctionalTestingFramework \DataGenerator \Objects \EntityDataObject ;
13+ use Magento \FunctionalTestingFramework \DataGenerator \Parsers \DataProfileSchemaParser ;
1314use Magento \FunctionalTestingFramework \Exceptions \TestFrameworkException ;
15+ use Magento \FunctionalTestingFramework \ObjectManager ;
16+ use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
17+ use ReflectionProperty ;
1418use tests \unit \Util \MagentoTestCase ;
15- use tests \unit \Util \ObjectHandlerUtil ;
1619use tests \unit \Util \TestLoggingUtil ;
1720
1821/**
@@ -151,7 +154,7 @@ protected function setUp(): void
151154 */
152155 public function testGetAllObjects (): void
153156 {
154- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT );
157+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT );
155158
156159 // Call the method under test
157160 $ actual = DataObjectHandler::getInstance ()->getAllObjects ();
@@ -170,10 +173,10 @@ public function testGetAllObjects(): void
170173 */
171174 public function testDeprecatedDataObject (): void
172175 {
173- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_DEPRECATED );
176+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_DEPRECATED );
174177
175178 // Call the method under test
176- $ actual = DataObjectHandler::getInstance ()->getAllObjects ();
179+ DataObjectHandler::getInstance ()->getAllObjects ();
177180
178181 //validate deprecation warning
179182 TestLoggingUtil::getInstance ()->validateMockLogStatement (
@@ -191,7 +194,7 @@ public function testDeprecatedDataObject(): void
191194 */
192195 public function testGetObject (): void
193196 {
194- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT );
197+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT );
195198
196199 // Call the method under test
197200 $ actual = DataObjectHandler::getInstance ()->getObject ('EntityOne ' );
@@ -209,7 +212,7 @@ public function testGetObject(): void
209212 */
210213 public function testGetObjectNull (): void
211214 {
212- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT );
215+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT );
213216
214217 $ actual = DataObjectHandler::getInstance ()->getObject ('h953u789h0g73t521 ' ); // doesnt exist
215218 $ this ->assertNull ($ actual );
@@ -223,7 +226,7 @@ public function testGetObjectNull(): void
223226 */
224227 public function testGetAllObjectsWithDataExtends (): void
225228 {
226- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND );
229+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND );
227230
228231 // Call the method under test
229232 $ actual = DataObjectHandler::getInstance ()->getAllObjects ();
@@ -250,7 +253,7 @@ public function testGetAllObjectsWithDataExtends(): void
250253 */
251254 public function testGetObjectWithDataExtends (): void
252255 {
253- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND );
256+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND );
254257
255258 // Call the method under test
256259 $ actual = DataObjectHandler::getInstance ()->getObject ('EntityTwo ' );
@@ -276,7 +279,7 @@ public function testGetObjectWithDataExtends(): void
276279 */
277280 public function testGetAllObjectsWithDataExtendsItself (): void
278281 {
279- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND_INVALID );
282+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND_INVALID );
280283
281284 $ this ->expectException (TestFrameworkException::class);
282285 $ this ->expectExceptionMessage (
@@ -296,7 +299,7 @@ public function testGetAllObjectsWithDataExtendsItself(): void
296299 */
297300 public function testGetObjectWithDataExtendsItself (): void
298301 {
299- ObjectHandlerUtil:: mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND_INVALID );
302+ $ this -> mockDataObjectHandlerWithData (self ::PARSER_OUTPUT_WITH_EXTEND_INVALID );
300303
301304 $ this ->expectException (TestFrameworkException::class);
302305 $ this ->expectExceptionMessage (
@@ -310,11 +313,66 @@ public function testGetObjectWithDataExtendsItself(): void
310313 );
311314 }
312315
316+ /**
317+ * Create mock data object handler with data.
318+ *
319+ * @param array $mockData
320+ *
321+ * @return void
322+ */
323+ private function mockDataObjectHandlerWithData (array $ mockData ): void
324+ {
325+ $ dataObjectHandlerProperty = new ReflectionProperty (DataObjectHandler::class, "INSTANCE " );
326+ $ dataObjectHandlerProperty ->setAccessible (true );
327+ $ dataObjectHandlerProperty ->setValue (null );
328+
329+ $ mockDataProfileSchemaParser = $ this ->createMock (DataProfileSchemaParser::class);
330+ $ mockDataProfileSchemaParser
331+ ->method ('readDataProfiles ' )
332+ ->willReturn ($ mockData );
333+
334+ $ objectManager = ObjectManagerFactory::getObjectManager ();
335+ $ mockObjectManagerInstance = $ this ->createMock (ObjectManager::class);
336+ $ mockObjectManagerInstance
337+ ->method ('create ' )
338+ ->will (
339+ $ this ->returnCallback (
340+ function (
341+ string $ class ,
342+ array $ arguments = []
343+ ) use (
344+ $ objectManager ,
345+ $ mockDataProfileSchemaParser
346+ ) {
347+ if ($ class === DataProfileSchemaParser::class) {
348+ return $ mockDataProfileSchemaParser ;
349+ }
350+
351+ return $ objectManager ->create ($ class , $ arguments );
352+ }
353+ )
354+ );
355+
356+ $ property = new ReflectionProperty (ObjectManager::class, 'instance ' );
357+ $ property ->setAccessible (true );
358+ $ property ->setValue ($ mockObjectManagerInstance );
359+ }
360+
313361 /**
314362 * @inheritDoc
315363 */
316364 public static function tearDownAfterClass (): void
317365 {
366+ parent ::tearDownAfterClass ();
367+
368+ $ dataObjectHandlerProperty = new ReflectionProperty (DataObjectHandler::class, "INSTANCE " );
369+ $ dataObjectHandlerProperty ->setAccessible (true );
370+ $ dataObjectHandlerProperty ->setValue (null );
371+
372+ $ objectManagerProperty = new ReflectionProperty (ObjectManager::class, 'instance ' );
373+ $ objectManagerProperty ->setAccessible (true );
374+ $ objectManagerProperty ->setValue (null );
375+
318376 TestLoggingUtil::getInstance ()->clearMockLoggingUtil ();
319377 }
320378}
0 commit comments