Skip to content

Commit a233e32

Browse files
authored
Merge pull request #439 from magento/MQE-1685
Mqe 1685: Test/Data self extension memory limit error
2 parents ac49cbe + ef67fe2 commit a233e32

File tree

8 files changed

+551
-28
lines changed

8 files changed

+551
-28
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/DataGenerator/Handlers/DataObjectHandlerTest.php

Lines changed: 200 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,105 @@ class DataObjectHandlerTest extends MagentoTestCase
3030
'value' => 'testValue'
3131
]
3232
]
33-
]
33+
],
34+
'EntityTwo' => [
35+
'type' => 'testType',
36+
'extends' => 'EntityOne',
37+
'data' => [
38+
0 => [
39+
'key' => 'testKeyTwo',
40+
'value' => 'testValueTwo'
41+
]
42+
]
43+
],
3444
]
3545
];
3646

37-
/**
38-
* Set up everything required to mock DataObjectHander::getInstance()
39-
* The first call to getInstance() uses these mocks to emulate the parser, initializing internal state
40-
* according to the PARSER_OUTPUT value
41-
*/
42-
public static function setUpBeforeClass()
43-
{
44-
$mockDataProfileSchemaParser = AspectMock::double(DataProfileSchemaParser::class, [
45-
'readDataProfiles' => self::PARSER_OUTPUT
46-
])->make();
47-
48-
$mockObjectManager = AspectMock::double(ObjectManager::class, [
49-
'create' => $mockDataProfileSchemaParser
50-
])->make();
47+
const PARSER_OUTPUT_WITH_EXTEND = [
48+
'entity' => [
49+
'EntityOne' => [
50+
'name' => 'EntityOne',
51+
'type' => 'testType',
52+
'data' => [
53+
0 => [
54+
'key' => 'testKey',
55+
'value' => 'testValue'
56+
]
57+
]
58+
],
59+
'EntityTwo' => [
60+
'name' => 'EntityTwo',
61+
'type' => 'testType',
62+
'extends' => 'EntityOne',
63+
'data' => [
64+
0 => [
65+
'key' => 'testKeyTwo',
66+
'value' => 'testValueTwo'
67+
]
68+
],
69+
],
70+
'EntityThree' => [
71+
'name' => 'EntityThree',
72+
'type' => 'testType',
73+
'extends' => 'EntityOne',
74+
'data' => [
75+
0 => [
76+
'key' => 'testKeyThree',
77+
'value' => 'testValueThree'
78+
]
79+
],
80+
]
81+
]
82+
];
5183

52-
AspectMock::double(ObjectManagerFactory::class, [
53-
'getObjectManager' => $mockObjectManager
54-
]);
55-
}
84+
const PARSER_OUTPUT_WITH_EXTEND_INVALID = [
85+
'entity' => [
86+
'EntityOne' => [
87+
'name' => 'EntityOne',
88+
'type' => 'testType',
89+
'extends' => 'EntityOne',
90+
'data' => [
91+
0 => [
92+
'key' => 'testKey',
93+
'value' => 'testValue'
94+
]
95+
]
96+
],
97+
'EntityTwo' => [
98+
'name' => 'EntityTwo',
99+
'type' => 'testType',
100+
'data' => [
101+
0 => [
102+
'key' => 'testKeyTwo',
103+
'value' => 'testValueTwo'
104+
]
105+
],
106+
],
107+
'EntityThree' => [
108+
'name' => 'EntityThree',
109+
'type' => 'testType',
110+
'extends' => 'EntityThree',
111+
'data' => [
112+
0 => [
113+
'key' => 'testKeyThree',
114+
'value' => 'testValueThree'
115+
]
116+
],
117+
]
118+
]
119+
];
56120

57121
/**
58122
* getAllObjects should contain the expected data object
59123
*/
60124
public function testGetAllObjects()
61125
{
62-
// Call the method under test
126+
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT);
63127

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

66131
// Assert
67-
68132
$expected = new EntityDataObject('EntityOne', 'testType', ['testkey' => 'testValue'], [], null, []);
69133
$this->assertArrayHasKey('EntityOne', $actual);
70134
$this->assertEquals($expected, $actual['EntityOne']);
@@ -75,22 +139,134 @@ public function testGetAllObjects()
75139
*/
76140
public function testGetObject()
77141
{
78-
// Call the method under test
142+
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT);
79143

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

82147
// Assert
83-
84148
$expected = new EntityDataObject('EntityOne', 'testType', ['testkey' => 'testValue'], [], null, []);
85149
$this->assertEquals($expected, $actual);
86150
}
87151

88152
/**
89-
* getObject should return null if the data object does not exist
153+
* getAllObjects should return the expected data object if it exists
90154
*/
91155
public function testGetObjectNull()
92156
{
157+
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT);
158+
93159
$actual = DataObjectHandler::getInstance()->getObject('h953u789h0g73t521'); // doesnt exist
94160
$this->assertNull($actual);
95161
}
162+
163+
/**
164+
* getAllObjects should contain the expected data object with extends
165+
*/
166+
public function testGetAllObjectsWithDataExtends()
167+
{
168+
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND);
169+
170+
// Call the method under test
171+
$actual = DataObjectHandler::getInstance()->getAllObjects();
172+
173+
// Assert
174+
$expected = new EntityDataObject(
175+
'EntityTwo',
176+
'testType',
177+
['testkey' => 'testValue', 'testkeytwo' => 'testValueTwo'],
178+
[],
179+
null,
180+
[],
181+
'EntityOne'
182+
);
183+
$this->assertArrayHasKey('EntityTwo', $actual);
184+
$this->assertEquals($expected, $actual['EntityTwo']);
185+
}
186+
187+
/**
188+
* getObject should return the expected data object with extended data if it exists
189+
*/
190+
public function testGetObjectWithDataExtends()
191+
{
192+
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND);
193+
194+
// Call the method under test
195+
$actual = DataObjectHandler::getInstance()->getObject('EntityTwo');
196+
197+
// Assert
198+
$expected = new EntityDataObject(
199+
'EntityTwo',
200+
'testType',
201+
['testkey' => 'testValue', 'testkeytwo' => 'testValueTwo'],
202+
[],
203+
null,
204+
[],
205+
'EntityOne'
206+
);
207+
$this->assertEquals($expected, $actual);
208+
}
209+
210+
/**
211+
* getAllObjects should throw TestFrameworkException exception if some data extends itself
212+
*/
213+
public function testGetAllObjectsWithDataExtendsItself()
214+
{
215+
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);
216+
217+
$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
218+
$this->expectExceptionMessage(
219+
"Mftf Data can not extend from itself: "
220+
. self::PARSER_OUTPUT_WITH_EXTEND_INVALID['entity']['EntityOne']['name']
221+
);
222+
223+
// Call the method under test
224+
DataObjectHandler::getInstance()->getAllObjects();
225+
}
226+
227+
/**
228+
* getObject should throw TestFrameworkException exception if requested data extends itself
229+
*/
230+
public function testGetObjectWithDataExtendsItself()
231+
{
232+
$this->setUpMockDataObjectHander(self::PARSER_OUTPUT_WITH_EXTEND_INVALID);
233+
234+
$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
235+
$this->expectExceptionMessage(
236+
"Mftf Data can not extend from itself: "
237+
. self::PARSER_OUTPUT_WITH_EXTEND_INVALID['entity']['EntityOne']['name']
238+
);
239+
240+
// Call the method under test
241+
DataObjectHandler::getInstance()->getObject(
242+
self::PARSER_OUTPUT_WITH_EXTEND_INVALID['entity']['EntityOne']['name']
243+
);
244+
}
245+
246+
/**
247+
* Set up everything required to mock DataObjectHander::getInstance()
248+
* The first call to getInstance() uses these mocks to emulate the parser, initializing internal state
249+
* according to the PARSER_OUTPUT value
250+
*
251+
* @param array $entityDataArray
252+
*/
253+
private function setUpMockDataObjectHander($entityDataArray)
254+
{
255+
// Clear DataObjectHandler singleton if already set
256+
$property = new \ReflectionProperty(DataObjectHandler::class, "INSTANCE");
257+
$property->setAccessible(true);
258+
$property->setValue(null);
259+
260+
$mockDataProfileSchemaParser = AspectMock::double(DataProfileSchemaParser::class, [
261+
'readDataProfiles' => $entityDataArray
262+
])->make();
263+
264+
$mockObjectManager = AspectMock::double(ObjectManager::class, [
265+
'create' => $mockDataProfileSchemaParser
266+
])->make();
267+
268+
AspectMock::double(ObjectManagerFactory::class, [
269+
'getObjectManager' => $mockObjectManager
270+
]);
271+
}
96272
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Tests\unit\Magento\FunctionalTestFramework\Test\Handlers;
8+
9+
use AspectMock\Test as AspectMock;
10+
11+
use Go\Aop\Aspect;
12+
use Magento\FunctionalTestingFramework\ObjectManager;
13+
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
14+
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
15+
use Magento\FunctionalTestingFramework\Util\MagentoTestCase;
16+
use tests\unit\Util\ActionGroupArrayBuilder;
17+
use Magento\FunctionalTestingFramework\Test\Parsers\ActionGroupDataParser;
18+
19+
class ActionGroupObjectHandlerTest extends MagentoTestCase
20+
{
21+
/**
22+
* getObject should throw exception if test extends from itself
23+
*
24+
* @throws \Exception
25+
*/
26+
public function testGetTestObjectWithInvalidExtends()
27+
{
28+
// Set up action group data
29+
$nameOne = 'actionGroupOne';
30+
$actionGroupOne = (new ActionGroupArrayBuilder())
31+
->withName($nameOne)
32+
->withExtendedAction($nameOne)
33+
->withAnnotations()
34+
->withFilename()
35+
->withActionObjects()
36+
->build();
37+
$this->setMockParserOutput(['actionGroups' => $actionGroupOne]);
38+
39+
$handler = ActionGroupObjectHandler::getInstance();
40+
41+
$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
42+
$this->expectExceptionMessage("Mftf Action Group can not extend from itself: " . $nameOne);
43+
$handler->getObject('actionGroupOne');
44+
}
45+
46+
/**
47+
* getAllObjects should throw exception if test extends from itself
48+
*
49+
* @throws \Exception
50+
*/
51+
public function testGetAllTestObjectsWithInvalidExtends()
52+
{
53+
// Set up action group data
54+
$nameOne = 'actionGroupOne';
55+
$nameTwo = 'actionGroupTwo';
56+
$actionGroupOne = (new ActionGroupArrayBuilder())
57+
->withName($nameOne)
58+
->withExtendedAction($nameOne)
59+
->withAnnotations()
60+
->withFilename()
61+
->withActionObjects()
62+
->build();
63+
$actionGroupTwo = (new ActionGroupArrayBuilder())
64+
->withName($nameTwo)
65+
->withExtendedAction()
66+
->withAnnotations()
67+
->withFilename()
68+
->withActionObjects()
69+
->build();
70+
71+
$this->setMockParserOutput(['actionGroups' => array_merge($actionGroupOne, $actionGroupTwo)]);
72+
73+
$handler = ActionGroupObjectHandler::getInstance();
74+
75+
$this->expectException(\Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException::class);
76+
$this->expectExceptionMessage("Mftf Action Group can not extend from itself: " . $nameOne);
77+
$handler->getAllObjects();
78+
}
79+
80+
/**
81+
* Function used to set mock for parser return and force init method to run between tests.
82+
*
83+
* @param array $data
84+
* @throws \Exception
85+
*/
86+
private function setMockParserOutput($data)
87+
{
88+
// Clear action group object handler value to inject parsed content
89+
$property = new \ReflectionProperty(ActionGroupObjectHandler::class, 'instance');
90+
$property->setAccessible(true);
91+
$property->setValue(null);
92+
93+
$mockDataParser = AspectMock::double(ActionGroupDataParser::class, ['readActionGroupData' => $data])->make();
94+
$instance = AspectMock::double(ObjectManager::class, ['create' => $mockDataParser])
95+
->make(); // bypass the private constructor
96+
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
97+
}
98+
}

0 commit comments

Comments
 (0)