Skip to content

33294 eliminate aspectmock from allureHelperTest #839

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,36 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace tests\unit\Magento\FunctionalTestFramework\Allure;

use Magento\FunctionalTestingFramework\Allure\AllureHelper;
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
use Magento\FunctionalTestingFramework\ObjectManager;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use Yandex\Allure\Adapter\Allure;
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
use Yandex\Allure\Adapter\AllureException;
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
use Yandex\Allure\Adapter\Event\StepStartedEvent;
use Yandex\Allure\Adapter\Model\Attachment;
use AspectMock\Test as AspectMock;
use PHPUnit\Framework\TestCase;

class AllureHelperTest extends TestCase
{
const MOCK_FILENAME = 'filename';
private const MOCK_FILENAME = 'filename';

/**
* Clear Allure Lifecycle
* The AddAttachmentToStep should add an attachment to the current step.
*
* @return void
* @throws AllureException
*/
public function tearDown(): void
public function testAddAttachmentToStep(): void
{
Allure::setDefaultLifecycle();
AspectMock::clean();
}

/**
* AddAtachmentToStep should add an attachment to the current step
* @throws \Yandex\Allure\Adapter\AllureException
*/
public function testAddAttachmentToStep()
{
$this->mockAttachmentWriteEvent();
$expectedData = "string";
$expectedCaption = "caption";
$expectedData = 'string';
$expectedCaption = 'caption';
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);

//Prepare Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
Expand All @@ -51,14 +47,16 @@ public function testAddAttachmentToStep()
}

/**
* AddAttachmentToLastStep should add an attachment only to the last step
* @throws \Yandex\Allure\Adapter\AllureException
* The AddAttachmentToLastStep should add an attachment only to the last step.
*
* @return void
* @throws AllureException
*/
public function testAddAttachmentToLastStep()
public function testAddAttachmentToLastStep(): void
{
$this->mockAttachmentWriteEvent();
$expectedData = "string";
$expectedCaption = "caption";
$expectedData = 'string';
$expectedCaption = 'caption';
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);

//Prepare Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
Expand Down Expand Up @@ -87,14 +85,15 @@ public function testAddAttachmentToLastStep()
}

/**
* AddAttachment actions should have files with different attachment names
* @throws \Yandex\Allure\Adapter\AllureException
* The AddAttachment actions should have files with different attachment names.
*
* @return void
* @throws AllureException
*/
public function testAddAttachementUniqueName()
public function testAddAttachmentUniqueName(): void
{
$this->mockCopyFile();
$expectedData = "string";
$expectedCaption = "caption";
$expectedData = 'string';
$expectedCaption = 'caption';

//Prepare Allure lifecycle
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
Expand All @@ -111,24 +110,56 @@ public function testAddAttachementUniqueName()
}

/**
* Mock entire attachment writing mechanisms
* @throws \Exception
* Clear Allure Lifecycle.
*
* @return void
*/
public function mockAttachmentWriteEvent()
protected function tearDown(): void
{
AspectMock::double(AddUniqueAttachmentEvent::class, [
"getAttachmentFileName" => self::MOCK_FILENAME
]);
Allure::setDefaultLifecycle();

$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
$objectManagerProperty->setAccessible(true);
$objectManagerProperty->setValue(null);
}

/**
* Mock only file writing mechanism
* @throws \Exception
* Mock entire attachment writing mechanisms.
*
* @param string $filePathOrContents
* @param string $caption
*
* @return void
*/
public function mockCopyFile()
private function mockAttachmentWriteEvent(string $filePathOrContents, string $caption): void
{
AspectMock::double(AddUniqueAttachmentEvent::class, [
"copyFile" => true
]);
$mockInstance = $this->getMockBuilder(AddUniqueAttachmentEvent::class)
->setConstructorArgs([$filePathOrContents, $caption])
->disallowMockingUnknownTypes()
->onlyMethods(['getAttachmentFileName'])
->getMock();

$mockInstance
->method('getAttachmentFileName')
->willReturn(self::MOCK_FILENAME);

$objectManagerMockInstance = $this->createMock(ObjectManager::class);
$objectManagerMockInstance
->method('create')
->will(
$this->returnCallback(
function (string $class) use ($mockInstance) {
if ($class === AddUniqueAttachmentEvent::class) {
return $mockInstance;
}

return null;
}
)
);

$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
$objectManagerProperty->setAccessible(true);
$objectManagerProperty->setValue($objectManagerMockInstance, $objectManagerMockInstance);
}
}
38 changes: 30 additions & 8 deletions src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,49 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\FunctionalTestingFramework\Allure;

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

class AllureHelper
{
/**
* Adds attachment to the current step
* Adds attachment to the current step.
*
* @param mixed $data
* @param string $caption
* @throws \Yandex\Allure\Adapter\AllureException
*
* @return void
* @throws AllureException
*/
public static function addAttachmentToCurrentStep($data, $caption)
public static function addAttachmentToCurrentStep($data, $caption): void
{
Allure::lifecycle()->fire(new AddUniqueAttachmentEvent($data, $caption));
/** @var AddUniqueAttachmentEvent $event */
$event = ObjectManagerFactory::getObjectManager()->create(
AddUniqueAttachmentEvent::class,
[
'filePathOrContents' => $data,
'caption' => $caption
]
);
Allure::lifecycle()->fire($event);
}

/**
* Adds Attachment to the last executed step.
* Use this when adding attachments outside of an $I->doSomething() step/context.
*
* @param mixed $data
* @param string $caption
*
* @return void
*/
public static function addAttachmentToLastStep($data, $caption)
public static function addAttachmentToLastStep($data, $caption): void
{
$rootStep = Allure::lifecycle()->getStepStorage()->getLast();
$trueLastStep = array_last($rootStep->getSteps());
Expand All @@ -39,8 +54,15 @@ public static function addAttachmentToLastStep($data, $caption)
// Nothing to attach to; do not fire off allure event
return;
}

$attachmentEvent = new AddUniqueAttachmentEvent($data, $caption);

/** @var AddUniqueAttachmentEvent $attachmentEvent */
$attachmentEvent = ObjectManagerFactory::getObjectManager()->create(
AddUniqueAttachmentEvent::class,
[
'filePathOrContents' => $data,
'caption' => $caption
]
);
$attachmentEvent->process($trueLastStep);
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\FunctionalTestingFramework\Allure\Event;

use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Symfony\Component\Mime\MimeTypes;
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;
private const DEFAULT_FILE_EXTENSION = 'txt';
private const DEFAULT_MIME_TYPE = 'text/plain';

/**
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior
* @param string $filePathOrContents
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior.
*
* @param mixed $filePathOrContents
* @param string $type
*
* @return string
* @throws AllureException
*/
public function getAttachmentFileName($filePathOrContents, $type)
public function getAttachmentFileName($filePathOrContents, $type): string
{
$filePath = $filePathOrContents;

if (!file_exists($filePath) || !is_file($filePath)) {
//Save contents to temporary file
$filePath = tempnam(sys_get_temp_dir(), 'allure-attachment');
Expand All @@ -40,13 +41,11 @@ public function getAttachmentFileName($filePathOrContents, $type)

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.");
}
Expand All @@ -56,51 +55,52 @@ public function getAttachmentFileName($filePathOrContents, $type)

/**
* Copies file from one path to another. Wrapper for mocking in unit test.
*
* @param string $filePath
* @param string $outputPath
*
* @return boolean
* @throws TestFrameworkException
*/
private function copyFile($filePath, $outputPath)
private function copyFile(string $filePath, string $outputPath): bool
{
if (MftfApplicationConfig::getConfig()->getPhase() === MftfApplicationConfig::UNIT_TEST_PHASE) {
return true;
}
return copy($filePath, $outputPath);
}

/**
* Copy of parent private function
* Copy of parent private function.
*
* @param string $filePath
*
* @return string
*/
private function guessFileMimeType($filePath)
private function guessFileMimeType(string $filePath): string
{
$type = MimeTypes::getDefault()->guessMimeType($filePath);

if (!isset($type)) {
return DEFAULT_MIME_TYPE;
return self::DEFAULT_MIME_TYPE;
}
return $type;
}

/**
* Copy of parent private function
* Copy of parent private function.
*
* @param string $mimeType
*
* @return string
*/
private function guessFileExtension($mimeType)
private function guessFileExtension(string $mimeType): string
{
$candidate = MimeTypes::getDefault()->getExtensions($mimeType);

if (empty($candidate)) {
return DEFAULT_FILE_EXTENSION;
return self::DEFAULT_FILE_EXTENSION;
}
return reset($candidate);
}

/**
* Copy of parent private function
* @param string $sha1
* @param string $extension
* @return string
*/
public function getOutputFileName($sha1, $extension)
{
return $sha1 . '-attachment.' . $extension;
}
}