Skip to content

Issue 33297: Eliminate AspectMock from DataExtensionUtilTest #856

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 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 @@ -3,37 +3,28 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace tests\unit\Magento\FunctionalTestFramework\DataGenerator\Util;

use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Parsers\DataProfileSchemaParser;
use Magento\FunctionalTestingFramework\ObjectManager\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use Magento\FunctionalTestingFramework\ObjectManager;
use ReflectionProperty;
use tests\unit\Util\MagentoTestCase;
use AspectMock\Test as AspectMock;

/**
* Class DataExtensionUtilTest
*/
class DataExtensionUtilTest extends MagentoTestCase
{
/**
* Before method functionality
* @return void
*/
protected function setUp(): void
{
AspectMock::clean();
}

public function testNoParentData()
public function testNoParentData(): void
{
$extendedDataObject = [
'entity' => [
'extended' => [
'type' => 'testType',
'extends' => "parent",
'extends' => 'parent',
'data' => [
0 => [
'key' => 'testKey',
Expand All @@ -46,21 +37,21 @@ public function testNoParentData()

$this->setMockEntities($extendedDataObject);

$this->expectExceptionMessage("Parent Entity parent not defined for Entity extended.");
DataObjectHandler::getInstance()->getObject("extended");
$this->expectExceptionMessage('Parent Entity parent not defined for Entity extended.');
DataObjectHandler::getInstance()->getObject('extended');
}

public function testAlreadyExtendedParentData()
public function testAlreadyExtendedParentData(): void
{
$extendedDataObjects = [
'entity' => [
'extended' => [
'type' => 'testType',
'extends' => "parent"
'extends' => 'parent'
],
'parent' => [
'type' => 'type',
'extends' => "grandparent"
'extends' => 'grandparent'
],
'grandparent' => [
'type' => 'grand'
Expand All @@ -71,18 +62,18 @@ public function testAlreadyExtendedParentData()
$this->setMockEntities($extendedDataObjects);

$this->expectExceptionMessage(
"Cannot extend an entity that already extends another entity. Entity: parent." . PHP_EOL
'Cannot extend an entity that already extends another entity. Entity: parent.' . PHP_EOL
);
DataObjectHandler::getInstance()->getObject("extended");
DataObjectHandler::getInstance()->getObject('extended');
}

public function testExtendedVarGetter()
public function testExtendedVarGetter(): void
{
$extendedDataObjects = [
'entity' => [
'extended' => [
'type' => 'testType',
'extends' => "parent"
'extends' => 'parent'
],
'parent' => [
'type' => 'type',
Expand All @@ -98,18 +89,18 @@ public function testExtendedVarGetter()
];

$this->setMockEntities($extendedDataObjects);
$resultextendedDataObject = DataObjectHandler::getInstance()->getObject("extended");
$resultextendedDataObject = DataObjectHandler::getInstance()->getObject('extended');
// Perform Asserts
$this->assertEquals("someOtherEntity->id", $resultextendedDataObject->getVarReference("someOtherEntity"));
$this->assertEquals('someOtherEntity->id', $resultextendedDataObject->getVarReference('someOtherEntity'));
}

public function testGetLinkedEntities()
public function testGetLinkedEntities(): void
{
$extendedDataObjects = [
'entity' => [
'extended' => [
'type' => 'testType',
'extends' => "parent"
'extends' => 'parent'
],
'parent' => [
'type' => 'type',
Expand All @@ -129,27 +120,36 @@ public function testGetLinkedEntities()

$this->setMockEntities($extendedDataObjects);
// Perform Asserts
$resultextendedDataObject = DataObjectHandler::getInstance()->getObject("extended");
$this->assertEquals("linkedEntity1", $resultextendedDataObject->getLinkedEntitiesOfType("linkedEntityType")[0]);
$this->assertEquals("linkedEntity2", $resultextendedDataObject->getLinkedEntitiesOfType("otherEntityType")[0]);
$resultextendedDataObject = DataObjectHandler::getInstance()->getObject('extended');
$this->assertEquals('linkedEntity1', $resultextendedDataObject->getLinkedEntitiesOfType('linkedEntityType')[0]);
$this->assertEquals('linkedEntity2', $resultextendedDataObject->getLinkedEntitiesOfType('otherEntityType')[0]);
}

private function setMockEntities($mockEntityData)
/**
* Prepare mock entites.
*
* @param $mockEntityData
*
* @return void
*/
private function setMockEntities($mockEntityData): void
{
$property = new \ReflectionProperty(DataObjectHandler::class, 'INSTANCE');
$property = new ReflectionProperty(DataObjectHandler::class, 'INSTANCE');
$property->setAccessible(true);
$property->setValue(null);

$mockDataProfileSchemaParser = AspectMock::double(DataProfileSchemaParser::class, [
'readDataProfiles' => $mockEntityData
])->make();
$mockDataProfileSchemaParser = $this->createMock(DataProfileSchemaParser::class);
$mockDataProfileSchemaParser->expects($this->any())
->method('readDataProfiles')
->willReturn($mockEntityData);

$mockObjectManager = AspectMock::double(ObjectManager::class, [
'create' => $mockDataProfileSchemaParser
])->make();
$mockObjectManager = $this->createMock(ObjectManager::class);
$mockObjectManager
->method('create')
->willReturn($mockDataProfileSchemaParser);

AspectMock::double(ObjectManagerFactory::class, [
'getObjectManager' => $mockObjectManager
]);
$property = new ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue($mockObjectManager);
}
}
2 changes: 2 additions & 0 deletions dev/tests/unit/Util/MagentoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static function setUpBeforeClass(): void
if (!self::fileExists(DOCS_OUTPUT_DIR)) {
mkdir(DOCS_OUTPUT_DIR, 0755, true);
}
// Should be used to clean AspectMock mocking before using PHPUnit mocking and Reflection.
AspectMock::clean();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Aspect Mock usage needs to be removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, @jilu1
It is temporarily. When all PRs merged we will proceed with the AspectMock eliminating in this class.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bohdan-harniuk
Got it. Thanks for the explanation!

parent::setUpBeforeClass();
}

Expand Down