Skip to content
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

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

Merged
Show file tree
Hide file tree
Changes from all 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();
}
}
Loading