Skip to content

Commit ecf1f25

Browse files
authored
Merge branch 'develop' into MQE-1470
2 parents bcd6105 + cf353e4 commit ecf1f25

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed

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

Lines changed: 39 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;
@@ -24,6 +25,7 @@ class AllureHelperTest extends TestCase
2425
public function tearDown()
2526
{
2627
Allure::setDefaultLifecycle();
28+
AspectMock::clean();
2729
}
2830

2931
/**
@@ -85,13 +87,48 @@ public function testAddAttachmentToLastStep()
8587
}
8688

8789
/**
88-
* Mock file system manipulation function
90+
* AddAttachment actions should have files with different attachment names
91+
* @throws \Yandex\Allure\Adapter\AllureException
92+
*/
93+
public function testAddAttachementUniqueName()
94+
{
95+
$this->mockCopyFile();
96+
$expectedData = "string";
97+
$expectedCaption = "caption";
98+
99+
//Prepare Allure lifecycle
100+
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
101+
102+
//Call function twice
103+
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
104+
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
105+
106+
// Assert file names for both attachments are not the same.
107+
$step = Allure::lifecycle()->getStepStorage()->pollLast();
108+
$attachmentOne = $step->getAttachments()[0]->getSource();
109+
$attachmentTwo = $step->getAttachments()[1]->getSource();
110+
$this->assertNotEquals($attachmentOne, $attachmentTwo);
111+
}
112+
113+
/**
114+
* Mock entire attachment writing mechanisms
89115
* @throws \Exception
90116
*/
91117
public function mockAttachmentWriteEvent()
92118
{
93-
AspectMock::double(AddAttachmentEvent::class, [
119+
AspectMock::double(AddUniqueAttachmentEvent::class, [
94120
"getAttachmentFileName" => self::MOCK_FILENAME
95121
]);
96122
}
123+
124+
/**
125+
* Mock only file writing mechanism
126+
* @throws \Exception
127+
*/
128+
public function mockCopyFile()
129+
{
130+
AspectMock::double(AddUniqueAttachmentEvent::class, [
131+
"copyFile" => true
132+
]);
133+
}
97134
}

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 boolean
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)