Skip to content

Improvement/mftf 33584 eliminate aspect mock from object handler uti #865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use Exception;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
use Magento\FunctionalTestingFramework\DataGenerator\Parsers\DataProfileSchemaParser;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use ReflectionProperty;
use tests\unit\Util\MagentoTestCase;
use tests\unit\Util\ObjectHandlerUtil;
use tests\unit\Util\TestLoggingUtil;

/**
Expand Down Expand Up @@ -151,7 +154,7 @@ protected function setUp(): void
*/
public function testGetAllObjects(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT);

// Call the method under test
$actual = DataObjectHandler::getInstance()->getAllObjects();
Expand All @@ -170,10 +173,10 @@ public function testGetAllObjects(): void
*/
public function testDeprecatedDataObject(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT_DEPRECATED);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT_DEPRECATED);

// Call the method under test
$actual = DataObjectHandler::getInstance()->getAllObjects();
DataObjectHandler::getInstance()->getAllObjects();

//validate deprecation warning
TestLoggingUtil::getInstance()->validateMockLogStatement(
Expand All @@ -191,7 +194,7 @@ public function testDeprecatedDataObject(): void
*/
public function testGetObject(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT);

// Call the method under test
$actual = DataObjectHandler::getInstance()->getObject('EntityOne');
Expand All @@ -209,7 +212,7 @@ public function testGetObject(): void
*/
public function testGetObjectNull(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT);

$actual = DataObjectHandler::getInstance()->getObject('h953u789h0g73t521'); // doesnt exist
$this->assertNull($actual);
Expand All @@ -223,7 +226,7 @@ public function testGetObjectNull(): void
*/
public function testGetAllObjectsWithDataExtends(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND);

// Call the method under test
$actual = DataObjectHandler::getInstance()->getAllObjects();
Expand All @@ -250,7 +253,7 @@ public function testGetAllObjectsWithDataExtends(): void
*/
public function testGetObjectWithDataExtends(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND);

// Call the method under test
$actual = DataObjectHandler::getInstance()->getObject('EntityTwo');
Expand All @@ -276,7 +279,7 @@ public function testGetObjectWithDataExtends(): void
*/
public function testGetAllObjectsWithDataExtendsItself(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);

$this->expectException(TestFrameworkException::class);
$this->expectExceptionMessage(
Expand All @@ -296,7 +299,7 @@ public function testGetAllObjectsWithDataExtendsItself(): void
*/
public function testGetObjectWithDataExtendsItself(): void
{
ObjectHandlerUtil::mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);
$this->mockDataObjectHandlerWithData(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);

$this->expectException(TestFrameworkException::class);
$this->expectExceptionMessage(
Expand All @@ -310,11 +313,66 @@ public function testGetObjectWithDataExtendsItself(): void
);
}

/**
* Create mock data object handler with data.
*
* @param array $mockData
*
* @return void
*/
private function mockDataObjectHandlerWithData(array $mockData): void
{
$dataObjectHandlerProperty = new ReflectionProperty(DataObjectHandler::class, "INSTANCE");
$dataObjectHandlerProperty->setAccessible(true);
$dataObjectHandlerProperty->setValue(null);

$mockDataProfileSchemaParser = $this->createMock(DataProfileSchemaParser::class);
$mockDataProfileSchemaParser
->method('readDataProfiles')
->willReturn($mockData);

$objectManager = ObjectManagerFactory::getObjectManager();
$mockObjectManagerInstance = $this->createMock(ObjectManager::class);
$mockObjectManagerInstance
->method('create')
->will(
$this->returnCallback(
function (
string $class,
array $arguments = []
) use (
$objectManager,
$mockDataProfileSchemaParser
) {
if ($class === DataProfileSchemaParser::class) {
return $mockDataProfileSchemaParser;
}

return $objectManager->create($class, $arguments);
}
)
);

$property = new ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue($mockObjectManagerInstance);
}

/**
* @inheritDoc
*/
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();

$dataObjectHandlerProperty = new ReflectionProperty(DataObjectHandler::class, "INSTANCE");
$dataObjectHandlerProperty->setAccessible(true);
$dataObjectHandlerProperty->setValue(null);

$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
$objectManagerProperty->setAccessible(true);
$objectManagerProperty->setValue(null);

TestLoggingUtil::getInstance()->clearMockLoggingUtil();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\OperationDefinitionObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationDefinitionObject;
use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;
use Magento\FunctionalTestingFramework\DataGenerator\Parsers\OperationDefinitionParser;
use Magento\FunctionalTestingFramework\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use ReflectionProperty;
use tests\unit\Util\MagentoTestCase;
use tests\unit\Util\ObjectHandlerUtil;
use tests\unit\Util\TestLoggingUtil;

/**
Expand Down Expand Up @@ -63,8 +66,8 @@ public function testGetMultipleObjects(): void
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_KEY => 'id',
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => 'integer'
],
]
],[
]
],[
OperationDefinitionObjectHandler::ENTITY_OPERATION_DATA_TYPE => $dataType1,
OperationDefinitionObjectHandler::ENTITY_OPERATION_TYPE => $operationType2,
OperationDefinitionObjectHandler::ENTITY_OPERATION_AUTH => 'auth',
Expand All @@ -76,8 +79,8 @@ public function testGetMultipleObjects(): void
OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY_VALUE => 'integer'
],
]
]]];
ObjectHandlerUtil::mockOperationHandlerWithData($mockData);
]]];
$this->mockOperationHandlerWithData($mockData);

//Perform Assertions
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
Expand Down Expand Up @@ -120,7 +123,7 @@ public function testDeprecatedOperation(): void
],
OperationDefinitionObjectHandler::OBJ_DEPRECATED => 'deprecation message'
]]];
ObjectHandlerUtil::mockOperationHandlerWithData($mockData);
$this->mockOperationHandlerWithData($mockData);

//Perform Assertions
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
Expand Down Expand Up @@ -274,7 +277,7 @@ public function testObjectCreation(): void
);

// Set up mocked data output
ObjectHandlerUtil::mockOperationHandlerWithData($mockData);
$this->mockOperationHandlerWithData($mockData);

// Get Operation
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
Expand Down Expand Up @@ -382,7 +385,7 @@ public function testObjectArrayCreation(): void
);

// Set up mocked data output
ObjectHandlerUtil::mockOperationHandlerWithData($mockData);
$this->mockOperationHandlerWithData($mockData);

// Get Operation
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
Expand Down Expand Up @@ -456,7 +459,7 @@ public function testLooseJsonCreation(): void
);

// Set up mocked data output
ObjectHandlerUtil::mockOperationHandlerWithData($mockData);
$this->mockOperationHandlerWithData($mockData);

// get Operations
$operationDefinitionManager = OperationDefinitionObjectHandler::getInstance();
Expand All @@ -467,13 +470,72 @@ public function testLooseJsonCreation(): void
$this->assertEquals($array, $operation->getOperationMetadata()[1]);
}

/**
* Create mock operation handler with data.
*
* @param array $mockData
*
* @return void
*/
private function mockOperationHandlerWithData(array $mockData): void
{
$operationDefinitionObjectHandlerProperty = new ReflectionProperty(
OperationDefinitionObjectHandler::class,
'INSTANCE'
);
$operationDefinitionObjectHandlerProperty->setAccessible(true);
$operationDefinitionObjectHandlerProperty->setValue(null);

$mockOperationParser = $this->createMock(OperationDefinitionParser::class);
$mockOperationParser
->method('readOperationMetadata')
->willReturn($mockData);

$objectManager = ObjectManagerFactory::getObjectManager();
$mockObjectManagerInstance = $this->createMock(ObjectManager::class);
$mockObjectManagerInstance
->method('create')
->will(
$this->returnCallback(
function (
string $class,
array $arguments = []
) use (
$objectManager,
$mockOperationParser
) {
if ($class === OperationDefinitionParser::class) {
return $mockOperationParser;
}

return $objectManager->create($class, $arguments);
}
)
);

$property = new ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue($mockObjectManagerInstance);
}

/**
* @inheritDoc
*/
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();

$operationDefinitionObjectHandlerProperty = new ReflectionProperty(
OperationDefinitionObjectHandler::class,
'INSTANCE'
);
$operationDefinitionObjectHandlerProperty->setAccessible(true);
$operationDefinitionObjectHandlerProperty->setValue(null);

$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
$objectManagerProperty->setAccessible(true);
$objectManagerProperty->setValue(null);

TestLoggingUtil::getInstance()->clearMockLoggingUtil();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,13 @@ public static function tearDownAfterClass(): void
parent::tearDownAfterClass();

// Clear out Singleton between tests
$property = new ReflectionProperty(PersistedObjectHandler::class, "INSTANCE");
$property->setAccessible(true);
$property->setValue(null);
$persistedObjectHandlerProperty = new ReflectionProperty(PersistedObjectHandler::class, "INSTANCE");
$persistedObjectHandlerProperty->setAccessible(true);
$persistedObjectHandlerProperty->setValue(null);

$property = new ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue(null);
$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
$objectManagerProperty->setAccessible(true);
$objectManagerProperty->setValue(null);

TestLoggingUtil::getInstance()->clearMockLoggingUtil();
}
Expand Down
Loading