Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Mqe 1685: Test/Data self extension memory limit error #439

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
merged 3 commits into from
Sep 5, 2019
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 @@ -30,41 +30,105 @@ class DataObjectHandlerTest extends MagentoTestCase
'value' => 'testValue'
]
]
]
],
'EntityTwo' => [
'type' => 'testType',
'extends' => 'EntityOne',
'data' => [
0 => [
'key' => 'testKeyTwo',
'value' => 'testValueTwo'
]
]
],
]
];

/**
* Set up everything required to mock DataObjectHander::getInstance()
* The first call to getInstance() uses these mocks to emulate the parser, initializing internal state
* according to the PARSER_OUTPUT value
*/
public static function setUpBeforeClass()
{
$mockDataProfileSchemaParser = AspectMock::double(DataProfileSchemaParser::class, [
'readDataProfiles' => self::PARSER_OUTPUT
])->make();

$mockObjectManager = AspectMock::double(ObjectManager::class, [
'create' => $mockDataProfileSchemaParser
])->make();
const PARSER_OUTPUT_WITH_EXTEND = [
'entity' => [
'EntityOne' => [
'name' => 'EntityOne',
'type' => 'testType',
'data' => [
0 => [
'key' => 'testKey',
'value' => 'testValue'
]
]
],
'EntityTwo' => [
'name' => 'EntityTwo',
'type' => 'testType',
'extends' => 'EntityOne',
'data' => [
0 => [
'key' => 'testKeyTwo',
'value' => 'testValueTwo'
]
],
],
'EntityThree' => [
'name' => 'EntityThree',
'type' => 'testType',
'extends' => 'EntityOne',
'data' => [
0 => [
'key' => 'testKeyThree',
'value' => 'testValueThree'
]
],
]
]
];

AspectMock::double(ObjectManagerFactory::class, [
'getObjectManager' => $mockObjectManager
]);
}
const PARSER_OUTPUT_WITH_EXTEND_INVALID = [
'entity' => [
'EntityOne' => [
'name' => 'EntityOne',
'type' => 'testType',
'extends' => 'EntityOne',
'data' => [
0 => [
'key' => 'testKey',
'value' => 'testValue'
]
]
],
'EntityTwo' => [
'name' => 'EntityTwo',
'type' => 'testType',
'data' => [
0 => [
'key' => 'testKeyTwo',
'value' => 'testValueTwo'
]
],
],
'EntityThree' => [
'name' => 'EntityThree',
'type' => 'testType',
'extends' => 'EntityThree',
'data' => [
0 => [
'key' => 'testKeyThree',
'value' => 'testValueThree'
]
],
]
]
];

/**
* getAllObjects should contain the expected data object
*/
public function testGetAllObjects()
{
// Call the method under test
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT);

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

// Assert

$expected = new EntityDataObject('EntityOne', 'testType', ['testkey' => 'testValue'], [], null, []);
$this->assertArrayHasKey('EntityOne', $actual);
$this->assertEquals($expected, $actual['EntityOne']);
Expand All @@ -75,22 +139,134 @@ public function testGetAllObjects()
*/
public function testGetObject()
{
// Call the method under test
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT);

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

// Assert

$expected = new EntityDataObject('EntityOne', 'testType', ['testkey' => 'testValue'], [], null, []);
$this->assertEquals($expected, $actual);
}

/**
* getObject should return null if the data object does not exist
* getAllObjects should return the expected data object if it exists
*/
public function testGetObjectNull()
{
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT);

$actual = DataObjectHandler::getInstance()->getObject('h953u789h0g73t521'); // doesnt exist
$this->assertNull($actual);
}

/**
* getAllObjects should contain the expected data object with extends
*/
public function testGetAllObjectsWithDataExtends()
{
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND);

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

// Assert
$expected = new EntityDataObject(
'EntityTwo',
'testType',
['testkey' => 'testValue', 'testkeytwo' => 'testValueTwo'],
[],
null,
[],
'EntityOne'
);
$this->assertArrayHasKey('EntityTwo', $actual);
$this->assertEquals($expected, $actual['EntityTwo']);
}

/**
* getObject should return the expected data object with extended data if it exists
*/
public function testGetObjectWithDataExtends()
{
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND);

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

// Assert
$expected = new EntityDataObject(
'EntityTwo',
'testType',
['testkey' => 'testValue', 'testkeytwo' => 'testValueTwo'],
[],
null,
[],
'EntityOne'
);
$this->assertEquals($expected, $actual);
}

/**
* getAllObjects should throw TestFrameworkException exception if some data extends itself
*/
public function testGetAllObjectsWithDataExtendsItself()
{
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);

$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
$this->expectExceptionMessage(
"Mftf Data can not extend from itself: "
. self::PARSER_OUTPUT_WITH_EXTEND_INVALID['entity']['EntityOne']['name']
);

// Call the method under test
DataObjectHandler::getInstance()->getAllObjects();
}

/**
* getObject should throw TestFrameworkException exception if requested data extends itself
*/
public function testGetObjectWithDataExtendsItself()
{
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);

$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
$this->expectExceptionMessage(
"Mftf Data can not extend from itself: "
. self::PARSER_OUTPUT_WITH_EXTEND_INVALID['entity']['EntityOne']['name']
);

// Call the method under test
DataObjectHandler::getInstance()->getObject(
self::PARSER_OUTPUT_WITH_EXTEND_INVALID['entity']['EntityOne']['name']
);
}

/**
* Set up everything required to mock DataObjectHander::getInstance()
* The first call to getInstance() uses these mocks to emulate the parser, initializing internal state
* according to the PARSER_OUTPUT value
*
* @param array $entityDataArray
*/
private function setUpMockDataObjectHander($entityDataArray)
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this mock function change 👍

{
// Clear DataObjectHandler singleton if already set
$property = new \ReflectionProperty(DataObjectHandler::class, "INSTANCE");
$property->setAccessible(true);
$property->setValue(null);

$mockDataProfileSchemaParser = AspectMock::double(DataProfileSchemaParser::class, [
'readDataProfiles' => $entityDataArray
])->make();

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

AspectMock::double(ObjectManagerFactory::class, [
'getObjectManager' => $mockObjectManager
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Tests\unit\Magento\FunctionalTestFramework\Test\Handlers;

use AspectMock\Test as AspectMock;

use Go\Aop\Aspect;
use Magento\FunctionalTestingFramework\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
use tests\unit\Util\ActionGroupArrayBuilder;
use Magento\FunctionalTestingFramework\Test\Parsers\ActionGroupDataParser;

class ActionGroupObjectHandlerTest extends MagentoTestCase
{
/**
* getObject should throw exception if test extends from itself
*
* @throws \Exception
*/
public function testGetTestObjectWithInvalidExtends()
{
// Set up action group data
$nameOne = 'actionGroupOne';
$actionGroupOne = (new ActionGroupArrayBuilder())
->withName($nameOne)
->withExtendedAction($nameOne)
->withAnnotations()
->withFilename()
->withActionObjects()
->build();
$this->setMockParserOutput(['actionGroups' => $actionGroupOne]);

$handler = ActionGroupObjectHandler::getInstance();

$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
$this->expectExceptionMessage("Mftf Action Group can not extend from itself: " . $nameOne);
$handler->getObject('actionGroupOne');
}

/**
* getAllObjects should throw exception if test extends from itself
*
* @throws \Exception
*/
public function testGetAllTestObjectsWithInvalidExtends()
{
// Set up action group data
$nameOne = 'actionGroupOne';
$nameTwo = 'actionGroupTwo';
$actionGroupOne = (new ActionGroupArrayBuilder())
->withName($nameOne)
->withExtendedAction($nameOne)
->withAnnotations()
->withFilename()
->withActionObjects()
->build();
$actionGroupTwo = (new ActionGroupArrayBuilder())
->withName($nameTwo)
->withExtendedAction()
->withAnnotations()
->withFilename()
->withActionObjects()
->build();

$this->setMockParserOutput(['actionGroups' => array_merge($actionGroupOne, $actionGroupTwo)]);

$handler = ActionGroupObjectHandler::getInstance();

$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
$this->expectExceptionMessage("Mftf Action Group can not extend from itself: " . $nameOne);
$handler->getAllObjects();
}

/**
* Function used to set mock for parser return and force init method to run between tests.
*
* @param array $data
* @throws \Exception
*/
private function setMockParserOutput($data)
{
// Clear action group object handler value to inject parsed content
$property = new \ReflectionProperty(ActionGroupObjectHandler::class, 'instance');
$property->setAccessible(true);
$property->setValue(null);

$mockDataParser = AspectMock::double(ActionGroupDataParser::class, ['readActionGroupData' => $data])->make();
$instance = AspectMock::double(ObjectManager::class, ['create' => $mockDataParser])
->make(); // bypass the private constructor
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
}
}
Loading