Skip to content

Commit 300619d

Browse files
committed
MQE-1633: Add API Request To Output for MFTF API Actions
- Added AllureHelper + Unit tests - Added attachment event to curlHandler classes
1 parent 0fe97bb commit 300619d

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\FunctionalTestingFramework\Allure;
7+
8+
use Yandex\Allure\Adapter\Allure;
9+
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
10+
11+
class AllureHelper
12+
{
13+
/**
14+
* Adds attachment to the current step
15+
* @param mixed $data
16+
* @param string $caption
17+
* @throws \Yandex\Allure\Adapter\AllureException
18+
* @return void
19+
*/
20+
public static function addAttachmentToCurrentStep($data, $caption)
21+
{
22+
Allure::lifecycle()->fire(new AddAttachmentEvent($data, $caption));
23+
}
24+
25+
/**
26+
* Adds Attachment to the last executed step. Required due to Allure root-step behavior
27+
* @param mixed $data
28+
* @param string $caption
29+
* @return void
30+
*/
31+
public static function addAttachmentToLastStep($data, $caption)
32+
{
33+
$rootStep = Allure::lifecycle()->getStepStorage()->getLast();
34+
$trueLastStep = array_last($rootStep->getSteps());
35+
36+
$attachmentEvent = new AddAttachmentEvent($data, $caption);
37+
$attachmentEvent->process($trueLastStep);
38+
}
39+
}

src/Magento/FunctionalTestingFramework/DataGenerator/Persist/CurlHandler.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\FunctionalTestingFramework\DataGenerator\Persist;
77

8+
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
89
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\AdminExecutor;
910
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\FrontendExecutor;
1011
use Magento\FunctionalTestingFramework\DataGenerator\Persist\Curl\WebapiExecutor;
@@ -166,6 +167,14 @@ public function executeRequest($dependentEntities)
166167
$response = $executor->read($successRegex, $returnRegex, $returnIndex);
167168
$executor->close();
168169

170+
AllureHelper::addAttachmentToLastStep($apiUrl, 'API Endpoint');
171+
AllureHelper::addAttachmentToLastStep(json_encode($headers, JSON_PRETTY_PRINT), 'Request Headers');
172+
AllureHelper::addAttachmentToLastStep(json_encode($this->requestData, JSON_PRETTY_PRINT), 'Request Body');
173+
AllureHelper::addAttachmentToLastStep(
174+
json_encode(json_decode($response, true), JSON_PRETTY_PRINT+JSON_UNESCAPED_UNICODE+JSON_UNESCAPED_SLASHES),
175+
'Response Data'
176+
);
177+
169178
return $response;
170179
}
171180

0 commit comments

Comments
 (0)