Skip to content

Commit 4720af5

Browse files
authored
Merge branch 'develop' into MQE-1703
2 parents 5f81deb + 167c7e7 commit 4720af5

27 files changed

+1133
-82
lines changed

dev/tests/_bootstrap.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
$kernel = \AspectMock\Kernel::getInstance();
1818
$kernel->init([
1919
'debug' => true,
20-
'includePaths' => [PROJECT_ROOT . DIRECTORY_SEPARATOR . 'src'],
20+
'includePaths' => [
21+
PROJECT_ROOT . DIRECTORY_SEPARATOR . 'src',
22+
PROJECT_ROOT . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'allure-framework'
23+
],
2124
'cacheDir' => PROJECT_ROOT .
2225
DIRECTORY_SEPARATOR .
2326
'dev' .
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Tests\unit\Magento\FunctionalTestingFramework\Allure;
7+
8+
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
9+
use Yandex\Allure\Adapter\Allure;
10+
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
11+
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
12+
use Yandex\Allure\Adapter\Event\StepStartedEvent;
13+
use Yandex\Allure\Adapter\Model\Attachment;
14+
use AspectMock\Test as AspectMock;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class AllureHelperTest extends TestCase
18+
{
19+
const MOCK_FILENAME = 'filename';
20+
21+
/**
22+
* Clear Allure Lifecycle
23+
*/
24+
public function tearDown()
25+
{
26+
Allure::setDefaultLifecycle();
27+
}
28+
29+
/**
30+
* AddAtachmentToStep should add an attachment to the current step
31+
* @throws \Yandex\Allure\Adapter\AllureException
32+
*/
33+
public function testAddAttachmentToStep()
34+
{
35+
$this->mockAttachmentWriteEvent();
36+
$expectedData = "string";
37+
$expectedCaption = "caption";
38+
39+
//Prepare Allure lifecycle
40+
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
41+
42+
//Call function
43+
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
44+
45+
// Assert Attachment is created as expected
46+
$step = Allure::lifecycle()->getStepStorage()->pollLast();
47+
$expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null);
48+
$this->assertEquals($step->getAttachments()[0], $expectedAttachment);
49+
}
50+
51+
/**
52+
* AddAttachmentToLastStep should add an attachment only to the last step
53+
* @throws \Yandex\Allure\Adapter\AllureException
54+
*/
55+
public function testAddAttachmentToLastStep()
56+
{
57+
$this->mockAttachmentWriteEvent();
58+
$expectedData = "string";
59+
$expectedCaption = "caption";
60+
61+
//Prepare Allure lifecycle
62+
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
63+
Allure::lifecycle()->fire(new StepFinishedEvent('firstStep'));
64+
Allure::lifecycle()->fire(new StepStartedEvent('secondStep'));
65+
Allure::lifecycle()->fire(new StepFinishedEvent('secondStep'));
66+
67+
//Call function
68+
AllureHelper::addAttachmentToLastStep($expectedData, $expectedCaption);
69+
70+
//Continue Allure lifecycle
71+
Allure::lifecycle()->fire(new StepStartedEvent('thirdStep'));
72+
Allure::lifecycle()->fire(new StepFinishedEvent('thirdStep'));
73+
74+
// Assert Attachment is created as expected on the right step
75+
$rootStep = Allure::lifecycle()->getStepStorage()->pollLast();
76+
77+
$firstStep = $rootStep->getSteps()[0];
78+
$secondStep = $rootStep->getSteps()[1];
79+
$thirdStep = $rootStep->getSteps()[2];
80+
81+
$expectedAttachment = new Attachment($expectedCaption, self::MOCK_FILENAME, null);
82+
$this->assertEmpty($firstStep->getAttachments());
83+
$this->assertEquals($secondStep->getAttachments()[0], $expectedAttachment);
84+
$this->assertEmpty($thirdStep->getAttachments());
85+
}
86+
87+
/**
88+
* Mock file system manipulation function
89+
* @throws \Exception
90+
*/
91+
public function mockAttachmentWriteEvent()
92+
{
93+
AspectMock::double(AddAttachmentEvent::class, [
94+
"getAttachmentFileName" => self::MOCK_FILENAME
95+
]);
96+
}
97+
}

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
}

0 commit comments

Comments
 (0)