Skip to content

MQE-1765: Introduce API Endpoint and Request Headers to Allure artifacts #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Tests\unit\Magento\FunctionalTestingFramework\Allure;

use Magento\FunctionalTestingFramework\Allure\AllureHelper;
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
use Yandex\Allure\Adapter\Allure;
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
Expand All @@ -24,6 +25,7 @@ class AllureHelperTest extends TestCase
public function tearDown()
{
Allure::setDefaultLifecycle();
AspectMock::clean();
}

/**
Expand Down Expand Up @@ -85,13 +87,48 @@ public function testAddAttachmentToLastStep()
}

/**
* Mock file system manipulation function
* AddAttachment actions should have files with different attachment names
* @throws \Yandex\Allure\Adapter\AllureException
*/
public function testAddAttachementUniqueName()
{
$this->mockCopyFile();
$expectedData = "string";
$expectedCaption = "caption";

//Prepare Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));

//Call function twice
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);
AllureHelper::addAttachmentToCurrentStep($expectedData, $expectedCaption);

// Assert file names for both attachments are not the same.
$step = Allure::lifecycle()->getStepStorage()->pollLast();
$attachmentOne = $step->getAttachments()[0]->getSource();
$attachmentTwo = $step->getAttachments()[1]->getSource();
$this->assertNotEquals($attachmentOne, $attachmentTwo);
}

/**
* Mock entire attachment writing mechanisms
* @throws \Exception
*/
public function mockAttachmentWriteEvent()
{
AspectMock::double(AddAttachmentEvent::class, [
AspectMock::double(AddUniqueAttachmentEvent::class, [
"getAttachmentFileName" => self::MOCK_FILENAME
]);
}

/**
* Mock only file writing mechanism
* @throws \Exception
*/
public function mockCopyFile()
{
AspectMock::double(AddUniqueAttachmentEvent::class, [
"copyFile" => true
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\FunctionalTestingFramework\Allure;

use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
use Yandex\Allure\Adapter\Allure;
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;

Expand All @@ -19,7 +20,7 @@ class AllureHelper
*/
public static function addAttachmentToCurrentStep($data, $caption)
{
Allure::lifecycle()->fire(new AddAttachmentEvent($data, $caption));
Allure::lifecycle()->fire(new AddUniqueAttachmentEvent($data, $caption));
}

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

$attachmentEvent = new AddAttachmentEvent($data, $caption);
$attachmentEvent = new AddUniqueAttachmentEvent($data, $caption);
$attachmentEvent->process($trueLastStep);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Allure\Event;

use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use Yandex\Allure\Adapter\AllureException;
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;

const DEFAULT_FILE_EXTENSION = 'txt';
const DEFAULT_MIME_TYPE = 'text/plain';

class AddUniqueAttachmentEvent extends AddAttachmentEvent
{
/**
* @var string
*/
private $type;

/**
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior
* @param string $filePathOrContents
* @param string $type
* @return string
* @throws AllureException
*/
public function getAttachmentFileName($filePathOrContents, $type)
{
$filePath = $filePathOrContents;
if (!file_exists($filePath) || !is_file($filePath)) {
//Save contents to temporary file
$filePath = tempnam(sys_get_temp_dir(), 'allure-attachment');
if (!file_put_contents($filePath, $filePathOrContents)) {
throw new AllureException("Failed to save attachment contents to $filePath");
}
}

if (!isset($type)) {
$type = $this->guessFileMimeType($filePath);
$this->type = $type;
}

$fileExtension = $this->guessFileExtension($type);

$fileSha1 = uniqid(sha1_file($filePath));
$outputPath = parent::getOutputPath($fileSha1, $fileExtension);
if (!$this->copyFile($filePath, $outputPath)) {
throw new AllureException("Failed to copy attachment from $filePath to $outputPath.");
}

return $this->getOutputFileName($fileSha1, $fileExtension);
}

/**
* Copies file from one path to another. Wrapper for mocking in unit test.
* @param string $filePath
* @param string $outputPath
* @return boolean
*/
private function copyFile($filePath, $outputPath)
{
return copy($filePath, $outputPath);
}

/**
* Copy of parent private function
* @param string $filePath
* @return string
*/
private function guessFileMimeType($filePath)
{
$type = MimeTypeGuesser::getInstance()->guess($filePath);
if (!isset($type)) {
return DEFAULT_MIME_TYPE;
}
return $type;
}

/**
* Copy of parent private function
* @param string $mimeType
* @return string
*/
private function guessFileExtension($mimeType)
{
$candidate = ExtensionGuesser::getInstance()->guess($mimeType);
if (!isset($candidate)) {
return DEFAULT_FILE_EXTENSION;
}
return $candidate;
}

/**
* Copy of parent private function
* @param string $sha1
* @param string $extension
* @return string
*/
public function getOutputFileName($sha1, $extension)
{
return $sha1 . '-attachment.' . $extension;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public function executeRequest($dependentEntities)
$returnRegex = $this->operationDefinition->getReturnRegex();
$returnIndex = $this->operationDefinition->getReturnIndex();
$method = $this->operationDefinition->getApiMethod();
AllureHelper::addAttachmentToLastStep($apiUrl, 'API Endpoint');
AllureHelper::addAttachmentToLastStep(json_encode($headers, JSON_PRETTY_PRINT), 'Request Headers');

$operationDataResolver = new OperationDataArrayResolver($dependentEntities);
$this->requestData = $operationDataResolver->resolveOperationDataArray(
Expand Down