99
1010use Exception ;
1111use Magento \FunctionalTestingFramework \DataGenerator \Handlers \PersistedObjectHandler ;
12+ use Magento \FunctionalTestingFramework \DataGenerator \Parsers \DataProfileSchemaParser ;
1213use Magento \FunctionalTestingFramework \DataGenerator \Persist \CurlHandler ;
14+ use Magento \FunctionalTestingFramework \Exceptions \TestFrameworkException ;
1315use Magento \FunctionalTestingFramework \Exceptions \TestReferenceException ;
16+ use Magento \FunctionalTestingFramework \ObjectManager ;
17+ use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
18+ use Magento \FunctionalTestingFramework \DataGenerator \Handlers \DataObjectHandler ;
1419use ReflectionProperty ;
1520use tests \unit \Util \MagentoTestCase ;
1621use tests \unit \Util \ObjectHandlerUtil ;
@@ -24,7 +29,7 @@ class PersistedObjectHandlerTest extends MagentoTestCase
2429 /**
2530 * @inheritDoc
2631 */
27- public function setUp (): void
32+ protected function setUp (): void
2833 {
2934 TestLoggingUtil::getInstance ()->setMockLoggingUtil ();
3035 }
@@ -90,9 +95,7 @@ public function testCreateSimpleEntity(): void
9095 }
9196 " ;
9297
93- // Mock Classes
94- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutput );
95- $ this ->mockCurlHandler ($ jsonResponse );
98+ $ this ->mockCurlHandler ($ jsonResponse , $ parserOutput );
9699 $ handler = PersistedObjectHandler::getInstance ();
97100
98101 // Call method
@@ -140,8 +143,7 @@ public function testDeleteSimpleEntity(): void
140143 " ;
141144
142145 // Mock Classes
143- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutput );
144- $ this ->mockCurlHandler ($ jsonResponse );
146+ $ this ->mockCurlHandler ($ jsonResponse , $ parserOutput );
145147 $ handler = PersistedObjectHandler::getInstance ();
146148
147149 // Call method
@@ -194,8 +196,7 @@ public function testGetSimpleEntity(): void
194196 " ;
195197
196198 // Mock Classes
197- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutput );
198- $ this ->mockCurlHandler ($ jsonResponse );
199+ $ this ->mockCurlHandler ($ jsonResponse , $ parserOutput );
199200 $ handler = PersistedObjectHandler::getInstance ();
200201
201202 // Call method
@@ -334,7 +335,7 @@ public function testRetrieveEntityAcrossScopes(): void
334335 ]
335336 ]
336337 ];
337- $ jsonReponseOne = "
338+ $ jsonResponseOne = "
338339 {
339340 \"" . strtolower ($ dataKeyOne ) . "\" : \"{$ dataValueOne }\"
340341 }
@@ -353,22 +354,21 @@ public function testRetrieveEntityAcrossScopes(): void
353354 // Mock Classes and Create Entities
354355 $ handler = PersistedObjectHandler::getInstance ();
355356
356- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutputOne );
357- $ this ->mockCurlHandler ($ jsonReponseOne );
357+ $ this ->mockCurlHandler ($ jsonResponseOne , $ parserOutputOne );
358358 $ handler ->createEntity (
359359 $ entityStepKeyOne ,
360360 PersistedObjectHandler::TEST_SCOPE ,
361361 $ entityNameOne
362362 );
363363
364- $ this ->mockCurlHandler ($ jsonReponseTwo );
364+ $ this ->mockCurlHandler ($ jsonReponseTwo, $ parserOutputOne );
365365 $ handler ->createEntity (
366366 $ entityStepKeyTwo ,
367367 PersistedObjectHandler::HOOK_SCOPE ,
368368 $ entityNameTwo
369369 );
370370
371- $ this ->mockCurlHandler ($ jsonReponseThree );
371+ $ this ->mockCurlHandler ($ jsonReponseThree, $ parserOutputOne );
372372 $ handler ->createEntity (
373373 $ entityStepKeyThree ,
374374 PersistedObjectHandler::SUITE_SCOPE ,
@@ -432,17 +432,15 @@ public function testRetrieveEntityValidField(
432432 ]
433433 ]
434434 ];
435- $ jsonReponseOne = "
435+ $ jsonResponseOne = "
436436 {
437437 \"" . strtolower ($ key ) . "\" : \"{$ value }\"
438438 }
439439 " ;
440440
441441 // Mock Classes and Create Entities
442442 $ handler = PersistedObjectHandler::getInstance ();
443-
444- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutputOne );
445- $ this ->mockCurlHandler ($ jsonReponseOne );
443+ $ this ->mockCurlHandler ($ jsonResponseOne , $ parserOutputOne );
446444 $ handler ->createEntity ($ stepKey , $ scope , $ name );
447445
448446 // Call method
@@ -463,7 +461,7 @@ public function testRetrieveEntityValidField(
463461 * @dataProvider entityDataProvider
464462 *
465463 * @return void
466- * @throws TestReferenceException
464+ * @throws TestReferenceException|TestFrameworkException
467465 */
468466 public function testRetrieveEntityInValidField (
469467 string $ name ,
@@ -490,16 +488,15 @@ public function testRetrieveEntityInValidField(
490488 ]
491489 ]
492490 ];
493- $ jsonReponseOne = "
491+ $ jsonResponseOne = "
494492 {
495493 \"" . strtolower ($ key ) . "\" : \"{$ value }\"
496494 }
497495 " ;
498496
499497 // Mock Classes and Create Entities
500498 $ handler = PersistedObjectHandler::getInstance ();
501- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutputOne );
502- $ this ->mockCurlHandler ($ jsonReponseOne );
499+ $ this ->mockCurlHandler ($ jsonResponseOne , $ parserOutputOne );
503500 $ handler ->createEntity ($ stepKey , $ scope , $ name );
504501
505502 // Call method
@@ -514,7 +511,7 @@ public function testRetrieveEntityInValidField(
514511 }
515512
516513 /**
517- * Data provider for testRetrieveEntityField
514+ * Data provider for testRetrieveEntityField.
518515 *
519516 * @return array
520517 */
@@ -531,45 +528,79 @@ public static function entityDataProvider(): array
531528 * Create mock curl handler.
532529 *
533530 * @param string $response
534- * @throws Exception
531+ * @param array $parserOutput
532+ *
533+ * @return void
535534 */
536- public function mockCurlHandler (string $ response ): void
535+ public function mockCurlHandler (string $ response, array $ parserOutput ): void
537536 {
538- $ mockCurlHandler = $ this ->createMock (CurlHandler::class);
539- $ mockCurlHandler ->expects ($ this ->any ())
537+ $ dataObjectHandler = new ReflectionProperty (DataObjectHandler::class, 'INSTANCE ' );
538+ $ dataObjectHandler ->setAccessible (true );
539+ $ dataObjectHandler ->setValue (null );
540+
541+ $ mockDataProfileSchemaParser = $ this ->createMock (DataProfileSchemaParser::class);
542+ $ mockDataProfileSchemaParser
543+ ->method ('readDataProfiles ' )
544+ ->willReturn ($ parserOutput );
545+
546+ $ curlHandler = $ this ->createMock (CurlHandler::class);
547+ $ curlHandler
540548 ->method ('executeRequest ' )
541549 ->willReturn ($ response );
542- $ mockCurlHandler -> expects ( $ this -> once ())
550+ $ curlHandler
543551 ->method ('getRequestDataArray ' )
544552 ->willReturn ([]);
545- $ mockCurlHandler -> expects ( $ this -> once ())
553+ $ curlHandler
546554 ->method ('isContentTypeJson ' )
547555 ->willReturn (true );
548556
549- $ property = new ReflectionProperty (CurlHandler::class, "INSTANCE " );
550- $ property ->setAccessible (true );
551- $ property ->setValue ($ mockCurlHandler );
557+ $ objectManagerInstance = ObjectManagerFactory::getObjectManager ();
558+ $ objectManagerMockInstance = $ this ->createMock (ObjectManager::class);
559+ $ objectManagerMockInstance ->expects ($ this ->any ())
560+ ->method ('create ' )
561+ ->will (
562+ $ this ->returnCallback (
563+ function (
564+ string $ class ,
565+ array $ arguments = []
566+ ) use ($ curlHandler , $ objectManagerInstance , $ mockDataProfileSchemaParser )
567+ {
568+ if ($ class === CurlHandler::class) {
569+ return $ curlHandler ;
570+ }
571+
572+ if ($ class === DataProfileSchemaParser::class) {
573+ return $ mockDataProfileSchemaParser ;
574+ }
575+
576+ return $ objectManagerInstance ->create ($ class , $ arguments );
577+ }
578+ )
579+ );
580+
581+ $ objectManagerProperty = new ReflectionProperty (ObjectManager::class, 'instance ' );
582+ $ objectManagerProperty ->setAccessible (true );
583+ $ objectManagerProperty ->setValue ($ objectManagerMockInstance );
552584 }
553585
554586 /**
555- * @inheritDoc
587+ * After class functionality.
588+ *
589+ * @return void
556590 */
557- public function tearDown (): void
591+ public static function tearDownAfterClass (): void
558592 {
593+ parent ::tearDownAfterClass ();
594+
559595 // Clear out Singleton between tests
560- $ property = new ReflectionProperty (PersistedObjectHandler::class, ' INSTANCE ' );
596+ $ property = new ReflectionProperty (PersistedObjectHandler::class, " INSTANCE " );
561597 $ property ->setAccessible (true );
562598 $ property ->setValue (null );
563599
564- parent ::tearDown (); // TODO: Change the autogenerated stub
565- }
600+ $ property = new ReflectionProperty (ObjectManager::class, 'instance ' );
601+ $ property ->setAccessible (true );
602+ $ property ->setValue (null );
566603
567- /**
568- * @inheritDoc
569- */
570- public static function tearDownAfterClass (): void
571- {
572604 TestLoggingUtil::getInstance ()->clearMockLoggingUtil ();
573- parent ::tearDownAfterClass ();
574605 }
575606}
0 commit comments