9
9
10
10
use Exception ;
11
11
use Magento \FunctionalTestingFramework \DataGenerator \Handlers \PersistedObjectHandler ;
12
+ use Magento \FunctionalTestingFramework \DataGenerator \Parsers \DataProfileSchemaParser ;
12
13
use Magento \FunctionalTestingFramework \DataGenerator \Persist \CurlHandler ;
14
+ use Magento \FunctionalTestingFramework \Exceptions \TestFrameworkException ;
13
15
use Magento \FunctionalTestingFramework \Exceptions \TestReferenceException ;
16
+ use Magento \FunctionalTestingFramework \ObjectManager ;
17
+ use Magento \FunctionalTestingFramework \ObjectManagerFactory ;
18
+ use Magento \FunctionalTestingFramework \DataGenerator \Handlers \DataObjectHandler ;
14
19
use ReflectionProperty ;
15
20
use tests \unit \Util \MagentoTestCase ;
16
21
use tests \unit \Util \ObjectHandlerUtil ;
@@ -24,7 +29,7 @@ class PersistedObjectHandlerTest extends MagentoTestCase
24
29
/**
25
30
* @inheritDoc
26
31
*/
27
- public function setUp (): void
32
+ protected function setUp (): void
28
33
{
29
34
TestLoggingUtil::getInstance ()->setMockLoggingUtil ();
30
35
}
@@ -90,9 +95,7 @@ public function testCreateSimpleEntity(): void
90
95
}
91
96
" ;
92
97
93
- // Mock Classes
94
- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutput );
95
- $ this ->mockCurlHandler ($ jsonResponse );
98
+ $ this ->mockCurlHandler ($ jsonResponse , $ parserOutput );
96
99
$ handler = PersistedObjectHandler::getInstance ();
97
100
98
101
// Call method
@@ -140,8 +143,7 @@ public function testDeleteSimpleEntity(): void
140
143
" ;
141
144
142
145
// Mock Classes
143
- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutput );
144
- $ this ->mockCurlHandler ($ jsonResponse );
146
+ $ this ->mockCurlHandler ($ jsonResponse , $ parserOutput );
145
147
$ handler = PersistedObjectHandler::getInstance ();
146
148
147
149
// Call method
@@ -194,8 +196,7 @@ public function testGetSimpleEntity(): void
194
196
" ;
195
197
196
198
// Mock Classes
197
- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutput );
198
- $ this ->mockCurlHandler ($ jsonResponse );
199
+ $ this ->mockCurlHandler ($ jsonResponse , $ parserOutput );
199
200
$ handler = PersistedObjectHandler::getInstance ();
200
201
201
202
// Call method
@@ -334,7 +335,7 @@ public function testRetrieveEntityAcrossScopes(): void
334
335
]
335
336
]
336
337
];
337
- $ jsonReponseOne = "
338
+ $ jsonResponseOne = "
338
339
{
339
340
\"" . strtolower ($ dataKeyOne ) . "\" : \"{$ dataValueOne }\"
340
341
}
@@ -353,22 +354,21 @@ public function testRetrieveEntityAcrossScopes(): void
353
354
// Mock Classes and Create Entities
354
355
$ handler = PersistedObjectHandler::getInstance ();
355
356
356
- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutputOne );
357
- $ this ->mockCurlHandler ($ jsonReponseOne );
357
+ $ this ->mockCurlHandler ($ jsonResponseOne , $ parserOutputOne );
358
358
$ handler ->createEntity (
359
359
$ entityStepKeyOne ,
360
360
PersistedObjectHandler::TEST_SCOPE ,
361
361
$ entityNameOne
362
362
);
363
363
364
- $ this ->mockCurlHandler ($ jsonReponseTwo );
364
+ $ this ->mockCurlHandler ($ jsonReponseTwo, $ parserOutputOne );
365
365
$ handler ->createEntity (
366
366
$ entityStepKeyTwo ,
367
367
PersistedObjectHandler::HOOK_SCOPE ,
368
368
$ entityNameTwo
369
369
);
370
370
371
- $ this ->mockCurlHandler ($ jsonReponseThree );
371
+ $ this ->mockCurlHandler ($ jsonReponseThree, $ parserOutputOne );
372
372
$ handler ->createEntity (
373
373
$ entityStepKeyThree ,
374
374
PersistedObjectHandler::SUITE_SCOPE ,
@@ -432,17 +432,15 @@ public function testRetrieveEntityValidField(
432
432
]
433
433
]
434
434
];
435
- $ jsonReponseOne = "
435
+ $ jsonResponseOne = "
436
436
{
437
437
\"" . strtolower ($ key ) . "\" : \"{$ value }\"
438
438
}
439
439
" ;
440
440
441
441
// Mock Classes and Create Entities
442
442
$ handler = PersistedObjectHandler::getInstance ();
443
-
444
- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutputOne );
445
- $ this ->mockCurlHandler ($ jsonReponseOne );
443
+ $ this ->mockCurlHandler ($ jsonResponseOne , $ parserOutputOne );
446
444
$ handler ->createEntity ($ stepKey , $ scope , $ name );
447
445
448
446
// Call method
@@ -463,7 +461,7 @@ public function testRetrieveEntityValidField(
463
461
* @dataProvider entityDataProvider
464
462
*
465
463
* @return void
466
- * @throws TestReferenceException
464
+ * @throws TestReferenceException|TestFrameworkException
467
465
*/
468
466
public function testRetrieveEntityInValidField (
469
467
string $ name ,
@@ -490,16 +488,15 @@ public function testRetrieveEntityInValidField(
490
488
]
491
489
]
492
490
];
493
- $ jsonReponseOne = "
491
+ $ jsonResponseOne = "
494
492
{
495
493
\"" . strtolower ($ key ) . "\" : \"{$ value }\"
496
494
}
497
495
" ;
498
496
499
497
// Mock Classes and Create Entities
500
498
$ handler = PersistedObjectHandler::getInstance ();
501
- ObjectHandlerUtil::mockDataObjectHandlerWithData ($ parserOutputOne );
502
- $ this ->mockCurlHandler ($ jsonReponseOne );
499
+ $ this ->mockCurlHandler ($ jsonResponseOne , $ parserOutputOne );
503
500
$ handler ->createEntity ($ stepKey , $ scope , $ name );
504
501
505
502
// Call method
@@ -514,7 +511,7 @@ public function testRetrieveEntityInValidField(
514
511
}
515
512
516
513
/**
517
- * Data provider for testRetrieveEntityField
514
+ * Data provider for testRetrieveEntityField.
518
515
*
519
516
* @return array
520
517
*/
@@ -531,45 +528,75 @@ public static function entityDataProvider(): array
531
528
* Create mock curl handler.
532
529
*
533
530
* @param string $response
534
- * @throws Exception
531
+ * @param array $parserOutput
532
+ *
533
+ * @return void
535
534
*/
536
- public function mockCurlHandler (string $ response ): void
535
+ public function mockCurlHandler (string $ response, array $ parserOutput ): void
537
536
{
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
540
548
->method ('executeRequest ' )
541
549
->willReturn ($ response );
542
- $ mockCurlHandler -> expects ( $ this -> once ())
550
+ $ curlHandler
543
551
->method ('getRequestDataArray ' )
544
552
->willReturn ([]);
545
- $ mockCurlHandler -> expects ( $ this -> once ())
553
+ $ curlHandler
546
554
->method ('isContentTypeJson ' )
547
555
->willReturn (true );
548
556
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 ($ class , array $ arguments = []) use ($ curlHandler , $ objectManagerInstance , $ mockDataProfileSchemaParser ) {
564
+ if ($ class === CurlHandler::class) {
565
+ return $ curlHandler ;
566
+ }
567
+
568
+ if ($ class === DataProfileSchemaParser::class) {
569
+ return $ mockDataProfileSchemaParser ;
570
+ }
571
+
572
+ return $ objectManagerInstance ->create ($ class , $ arguments );
573
+ }
574
+ )
575
+ );
576
+
577
+ $ objectManagerProperty = new ReflectionProperty (ObjectManager::class, 'instance ' );
578
+ $ objectManagerProperty ->setAccessible (true );
579
+ $ objectManagerProperty ->setValue ($ objectManagerMockInstance );
552
580
}
553
581
554
582
/**
555
- * @inheritDoc
583
+ * After class functionality.
584
+ *
585
+ * @return void
556
586
*/
557
- public function tearDown (): void
587
+ public static function tearDownAfterClass (): void
558
588
{
589
+ parent ::tearDownAfterClass ();
590
+
559
591
// Clear out Singleton between tests
560
- $ property = new ReflectionProperty (PersistedObjectHandler::class, ' INSTANCE ' );
592
+ $ property = new ReflectionProperty (PersistedObjectHandler::class, " INSTANCE " );
561
593
$ property ->setAccessible (true );
562
594
$ property ->setValue (null );
563
595
564
- parent ::tearDown (); // TODO: Change the autogenerated stub
565
- }
596
+ $ property = new ReflectionProperty (ObjectManager::class, 'instance ' );
597
+ $ property ->setAccessible (true );
598
+ $ property ->setValue (null );
566
599
567
- /**
568
- * @inheritDoc
569
- */
570
- public static function tearDownAfterClass (): void
571
- {
572
600
TestLoggingUtil::getInstance ()->clearMockLoggingUtil ();
573
- parent ::tearDownAfterClass ();
574
601
}
575
602
}
0 commit comments