Skip to content

Commit 3373f7d

Browse files
committed
MQE-1765: Introduce API Endpoint and Request Headers to Allure artifacts
- Added new Allure event to MFTF codebase to prevent buggy allure behavior - Changed calls to AllureHelper to use this new event - Added calls to CurlHandler to add artifacts. - Unit Test fix
1 parent f101b0e commit 3373f7d

File tree

4 files changed

+146
-4
lines changed

4 files changed

+146
-4
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Allure/AllureHelperTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Tests\unit\Magento\FunctionalTestingFramework\Allure;
77

88
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
9+
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
910
use Yandex\Allure\Adapter\Allure;
1011
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
1112
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
@@ -84,14 +85,45 @@ public function testAddAttachmentToLastStep()
8485
$this->assertEmpty($thirdStep->getAttachments());
8586
}
8687

88+
public function testAddAttachementUniqueName()
89+
{
90+
$this->mockCopyFile();
91+
$expectedData = "string";
92+
$expectedCaption = "caption";
93+
94+
//Prepare Allure lifecycle
95+
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
96+
97+
//Call function twice
98+
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
99+
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
100+
101+
// Assert file names for both attachments are not the same.
102+
$step = Allure::lifecycle()->getStepStorage()->pollLast();
103+
$attachmentOne = $step->getAttachments()[0]->getSource();
104+
$attachmentTwo = $step->getAttachments()[1]->getSource();
105+
$this->assertNotEquals($attachmentOne, $attachmentTwo);
106+
}
107+
87108
/**
88-
* Mock file system manipulation function
109+
* Mock entire attachment writing mechanisms
89110
* @throws \Exception
90111
*/
91112
public function mockAttachmentWriteEvent()
92113
{
93-
AspectMock::double(AddAttachmentEvent::class, [
114+
AspectMock::double(AddUniqueAttachmentEvent::class, [
94115
"getAttachmentFileName" => self::MOCK_FILENAME
95116
]);
96117
}
118+
119+
/**
120+
* Mock only file writing mechanism
121+
* @throws \Exception
122+
*/
123+
public function mockCopyFile()
124+
{
125+
AspectMock::double(AddUniqueAttachmentEvent::class, [
126+
"copyFile" => true
127+
]);
128+
}
97129
}

src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php

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

8+
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
89
use Yandex\Allure\Adapter\Allure;
910
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
1011

@@ -19,7 +20,7 @@ class AllureHelper
1920
*/
2021
public static function addAttachmentToCurrentStep($data, $caption)
2122
{
22-
Allure::lifecycle()->fire(new AddAttachmentEvent($data, $caption));
23+
Allure::lifecycle()->fire(new AddUniqueAttachmentEvent($data, $caption));
2324
}
2425

2526
/**
@@ -34,7 +35,7 @@ public static function addAttachmentToLastStep($data, $caption)
3435
$rootStep = Allure::lifecycle()->getStepStorage()->getLast();
3536
$trueLastStep = array_last($rootStep->getSteps());
3637

37-
$attachmentEvent = new AddAttachmentEvent($data, $caption);
38+
$attachmentEvent = new AddUniqueAttachmentEvent($data, $caption);
3839
$attachmentEvent->process($trueLastStep);
3940
}
4041
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\FunctionalTestingFramework\Allure\Event;
8+
9+
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
10+
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
11+
use Yandex\Allure\Adapter\AllureException;
12+
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
13+
14+
const DEFAULT_FILE_EXTENSION = 'txt';
15+
const DEFAULT_MIME_TYPE = 'text/plain';
16+
17+
class AddUniqueAttachmentEvent extends AddAttachmentEvent
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $type;
23+
24+
/**
25+
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior
26+
* @param string $filePathOrContents
27+
* @param string $type
28+
* @return string
29+
* @throws AllureException
30+
*/
31+
public function getAttachmentFileName($filePathOrContents, $type)
32+
{
33+
$filePath = $filePathOrContents;
34+
if (!file_exists($filePath) || !is_file($filePath)) {
35+
//Save contents to temporary file
36+
$filePath = tempnam(sys_get_temp_dir(), 'allure-attachment');
37+
if (!file_put_contents($filePath, $filePathOrContents)) {
38+
throw new AllureException("Failed to save attachment contents to $filePath");
39+
}
40+
}
41+
42+
if (!isset($type)) {
43+
$type = $this->guessFileMimeType($filePath);
44+
$this->type = $type;
45+
}
46+
47+
$fileExtension = $this->guessFileExtension($type);
48+
49+
$fileSha1 = uniqid(sha1_file($filePath));
50+
$outputPath = parent::getOutputPath($fileSha1, $fileExtension);
51+
if (!$this->copyFile($filePath, $outputPath)) {
52+
throw new AllureException("Failed to copy attachment from $filePath to $outputPath.");
53+
}
54+
55+
return $this->getOutputFileName($fileSha1, $fileExtension);
56+
}
57+
58+
/**
59+
* Copies file from one path to another. Wrapper for mocking in unit test.
60+
* @param string $filePath
61+
* @param string $outputPath
62+
* @return bool
63+
*/
64+
private function copyFile($filePath, $outputPath)
65+
{
66+
return copy($filePath, $outputPath);
67+
}
68+
69+
/**
70+
* Copy of parent private function
71+
* @param string $filePath
72+
* @return string
73+
*/
74+
private function guessFileMimeType($filePath)
75+
{
76+
$type = MimeTypeGuesser::getInstance()->guess($filePath);
77+
if (!isset($type)) {
78+
return DEFAULT_MIME_TYPE;
79+
}
80+
return $type;
81+
}
82+
83+
/**
84+
* Copy of parent private function
85+
* @param string $mimeType
86+
* @return string
87+
*/
88+
private function guessFileExtension($mimeType)
89+
{
90+
$candidate = ExtensionGuesser::getInstance()->guess($mimeType);
91+
if (!isset($candidate)) {
92+
return DEFAULT_FILE_EXTENSION;
93+
}
94+
return $candidate;
95+
}
96+
97+
/**
98+
* Copy of parent private function
99+
* @param string $sha1
100+
* @param string $extension
101+
* @return string
102+
*/
103+
public function getOutputFileName($sha1, $extension)
104+
{
105+
return $sha1 . '-attachment.' . $extension;
106+
}
107+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public function executeRequest($dependentEntities)
123123
$returnRegex = $this->operationDefinition->getReturnRegex();
124124
$returnIndex = $this->operationDefinition->getReturnIndex();
125125
$method = $this->operationDefinition->getApiMethod();
126+
AllureHelper::addAttachmentToLastStep($apiUrl, 'API Endpoint');
127+
AllureHelper::addAttachmentToLastStep(json_encode($headers, JSON_PRETTY_PRINT), 'Request Headers');
126128

127129
$operationDataResolver = new OperationDataArrayResolver($dependentEntities);
128130
$this->requestData = $operationDataResolver->resolveOperationDataArray(

0 commit comments

Comments
 (0)