Skip to content

Commit 724dd94

Browse files
authored
Merge pull request #96 from magento-commerce/imported-magento-magento2-functional-testing-framework-839
[Imported] 33294 eliminate aspectmock from allureHelperTest
2 parents 4d82517 + 83e5cc0 commit 724dd94

File tree

3 files changed

+137
-84
lines changed

3 files changed

+137
-84
lines changed

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

+74-43
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,36 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace tests\unit\Magento\FunctionalTestFramework\Allure;
79

810
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
911
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
12+
use Magento\FunctionalTestingFramework\ObjectManager;
13+
use PHPUnit\Framework\TestCase;
14+
use ReflectionProperty;
1015
use Yandex\Allure\Adapter\Allure;
11-
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
16+
use Yandex\Allure\Adapter\AllureException;
1217
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
1318
use Yandex\Allure\Adapter\Event\StepStartedEvent;
1419
use Yandex\Allure\Adapter\Model\Attachment;
15-
use AspectMock\Test as AspectMock;
16-
use PHPUnit\Framework\TestCase;
1720

1821
class AllureHelperTest extends TestCase
1922
{
20-
const MOCK_FILENAME = 'filename';
23+
private const MOCK_FILENAME = 'filename';
2124

2225
/**
23-
* Clear Allure Lifecycle
26+
* The AddAttachmentToStep should add an attachment to the current step.
27+
*
28+
* @return void
29+
* @throws AllureException
2430
*/
25-
public function tearDown(): void
31+
public function testAddAttachmentToStep(): void
2632
{
27-
Allure::setDefaultLifecycle();
28-
AspectMock::clean();
29-
}
30-
31-
/**
32-
* AddAtachmentToStep should add an attachment to the current step
33-
* @throws \Yandex\Allure\Adapter\AllureException
34-
*/
35-
public function testAddAttachmentToStep()
36-
{
37-
$this->mockAttachmentWriteEvent();
38-
$expectedData = "string";
39-
$expectedCaption = "caption";
33+
$expectedData = 'string';
34+
$expectedCaption = 'caption';
35+
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);
4036

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

5349
/**
54-
* AddAttachmentToLastStep should add an attachment only to the last step
55-
* @throws \Yandex\Allure\Adapter\AllureException
50+
* The AddAttachmentToLastStep should add an attachment only to the last step.
51+
*
52+
* @return void
53+
* @throws AllureException
5654
*/
57-
public function testAddAttachmentToLastStep()
55+
public function testAddAttachmentToLastStep(): void
5856
{
59-
$this->mockAttachmentWriteEvent();
60-
$expectedData = "string";
61-
$expectedCaption = "caption";
57+
$expectedData = 'string';
58+
$expectedCaption = 'caption';
59+
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);
6260

6361
//Prepare Allure lifecycle
6462
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
@@ -87,14 +85,15 @@ public function testAddAttachmentToLastStep()
8785
}
8886

8987
/**
90-
* AddAttachment actions should have files with different attachment names
91-
* @throws \Yandex\Allure\Adapter\AllureException
88+
* The AddAttachment actions should have files with different attachment names.
89+
*
90+
* @return void
91+
* @throws AllureException
9292
*/
93-
public function testAddAttachementUniqueName()
93+
public function testAddAttachmentUniqueName(): void
9494
{
95-
$this->mockCopyFile();
96-
$expectedData = "string";
97-
$expectedCaption = "caption";
95+
$expectedData = 'string';
96+
$expectedCaption = 'caption';
9897

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

113112
/**
114-
* Mock entire attachment writing mechanisms
115-
* @throws \Exception
113+
* Clear Allure Lifecycle.
114+
*
115+
* @return void
116116
*/
117-
public function mockAttachmentWriteEvent()
117+
protected function tearDown(): void
118118
{
119-
AspectMock::double(AddUniqueAttachmentEvent::class, [
120-
"getAttachmentFileName" => self::MOCK_FILENAME
121-
]);
119+
Allure::setDefaultLifecycle();
120+
121+
$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
122+
$objectManagerProperty->setAccessible(true);
123+
$objectManagerProperty->setValue(null);
122124
}
123125

124126
/**
125-
* Mock only file writing mechanism
126-
* @throws \Exception
127+
* Mock entire attachment writing mechanisms.
128+
*
129+
* @param string $filePathOrContents
130+
* @param string $caption
131+
*
132+
* @return void
127133
*/
128-
public function mockCopyFile()
134+
private function mockAttachmentWriteEvent(string $filePathOrContents, string $caption): void
129135
{
130-
AspectMock::double(AddUniqueAttachmentEvent::class, [
131-
"copyFile" => true
132-
]);
136+
$mockInstance = $this->getMockBuilder(AddUniqueAttachmentEvent::class)
137+
->setConstructorArgs([$filePathOrContents, $caption])
138+
->disallowMockingUnknownTypes()
139+
->onlyMethods(['getAttachmentFileName'])
140+
->getMock();
141+
142+
$mockInstance
143+
->method('getAttachmentFileName')
144+
->willReturn(self::MOCK_FILENAME);
145+
146+
$objectManagerMockInstance = $this->createMock(ObjectManager::class);
147+
$objectManagerMockInstance
148+
->method('create')
149+
->will(
150+
$this->returnCallback(
151+
function (string $class) use ($mockInstance) {
152+
if ($class === AddUniqueAttachmentEvent::class) {
153+
return $mockInstance;
154+
}
155+
156+
return null;
157+
}
158+
)
159+
);
160+
161+
$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
162+
$objectManagerProperty->setAccessible(true);
163+
$objectManagerProperty->setValue($objectManagerMockInstance, $objectManagerMockInstance);
133164
}
134165
}

src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php

+30-8
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,49 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\FunctionalTestingFramework\Allure;
79

810
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
11+
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
912
use Yandex\Allure\Adapter\Allure;
10-
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
13+
use Yandex\Allure\Adapter\AllureException;
1114

1215
class AllureHelper
1316
{
1417
/**
15-
* Adds attachment to the current step
18+
* Adds attachment to the current step.
19+
*
1620
* @param mixed $data
1721
* @param string $caption
18-
* @throws \Yandex\Allure\Adapter\AllureException
22+
*
1923
* @return void
24+
* @throws AllureException
2025
*/
21-
public static function addAttachmentToCurrentStep($data, $caption)
26+
public static function addAttachmentToCurrentStep($data, $caption): void
2227
{
23-
Allure::lifecycle()->fire(new AddUniqueAttachmentEvent($data, $caption));
28+
/** @var AddUniqueAttachmentEvent $event */
29+
$event = ObjectManagerFactory::getObjectManager()->create(
30+
AddUniqueAttachmentEvent::class,
31+
[
32+
'filePathOrContents' => $data,
33+
'caption' => $caption
34+
]
35+
);
36+
Allure::lifecycle()->fire($event);
2437
}
2538

2639
/**
2740
* Adds Attachment to the last executed step.
2841
* Use this when adding attachments outside of an $I->doSomething() step/context.
42+
*
2943
* @param mixed $data
3044
* @param string $caption
45+
*
3146
* @return void
3247
*/
33-
public static function addAttachmentToLastStep($data, $caption)
48+
public static function addAttachmentToLastStep($data, $caption): void
3449
{
3550
$rootStep = Allure::lifecycle()->getStepStorage()->getLast();
3651
$trueLastStep = array_last($rootStep->getSteps());
@@ -39,8 +54,15 @@ public static function addAttachmentToLastStep($data, $caption)
3954
// Nothing to attach to; do not fire off allure event
4055
return;
4156
}
42-
43-
$attachmentEvent = new AddUniqueAttachmentEvent($data, $caption);
57+
58+
/** @var AddUniqueAttachmentEvent $attachmentEvent */
59+
$attachmentEvent = ObjectManagerFactory::getObjectManager()->create(
60+
AddUniqueAttachmentEvent::class,
61+
[
62+
'filePathOrContents' => $data,
63+
'caption' => $caption
64+
]
65+
);
4466
$attachmentEvent->process($trueLastStep);
4567
}
4668
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
<?php
2-
32
/**
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\FunctionalTestingFramework\Allure\Event;
89

10+
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
11+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
912
use Symfony\Component\Mime\MimeTypes;
1013
use Yandex\Allure\Adapter\AllureException;
1114
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
1215

13-
const DEFAULT_FILE_EXTENSION = 'txt';
14-
const DEFAULT_MIME_TYPE = 'text/plain';
15-
1616
class AddUniqueAttachmentEvent extends AddAttachmentEvent
1717
{
18-
/**
19-
* @var string
20-
*/
21-
private $type;
18+
private const DEFAULT_FILE_EXTENSION = 'txt';
19+
private const DEFAULT_MIME_TYPE = 'text/plain';
2220

2321
/**
24-
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior
25-
* @param string $filePathOrContents
22+
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior.
23+
*
24+
* @param mixed $filePathOrContents
2625
* @param string $type
26+
*
2727
* @return string
2828
* @throws AllureException
2929
*/
30-
public function getAttachmentFileName($filePathOrContents, $type)
30+
public function getAttachmentFileName($filePathOrContents, $type): string
3131
{
3232
$filePath = $filePathOrContents;
33-
if (!file_exists($filePath) || !is_file($filePath)) {
33+
34+
if (!is_string($filePath) || !file_exists($filePath) || !is_file($filePath)) {
3435
//Save contents to temporary file
3536
$filePath = tempnam(sys_get_temp_dir(), 'allure-attachment');
3637
if (!file_put_contents($filePath, $filePathOrContents)) {
@@ -40,13 +41,11 @@ public function getAttachmentFileName($filePathOrContents, $type)
4041

4142
if (!isset($type)) {
4243
$type = $this->guessFileMimeType($filePath);
43-
$this->type = $type;
4444
}
45-
4645
$fileExtension = $this->guessFileExtension($type);
47-
4846
$fileSha1 = uniqid(sha1_file($filePath));
4947
$outputPath = parent::getOutputPath($fileSha1, $fileExtension);
48+
5049
if (!$this->copyFile($filePath, $outputPath)) {
5150
throw new AllureException("Failed to copy attachment from $filePath to $outputPath.");
5251
}
@@ -56,51 +55,52 @@ public function getAttachmentFileName($filePathOrContents, $type)
5655

5756
/**
5857
* Copies file from one path to another. Wrapper for mocking in unit test.
58+
*
5959
* @param string $filePath
6060
* @param string $outputPath
61+
*
6162
* @return boolean
63+
* @throws TestFrameworkException
6264
*/
63-
private function copyFile($filePath, $outputPath)
65+
private function copyFile(string $filePath, string $outputPath): bool
6466
{
67+
if (MftfApplicationConfig::getConfig()->getPhase() === MftfApplicationConfig::UNIT_TEST_PHASE) {
68+
return true;
69+
}
6570
return copy($filePath, $outputPath);
6671
}
6772

6873
/**
69-
* Copy of parent private function
74+
* Copy of parent private function.
75+
*
7076
* @param string $filePath
77+
*
7178
* @return string
7279
*/
73-
private function guessFileMimeType($filePath)
80+
private function guessFileMimeType(string $filePath): string
7481
{
7582
$type = MimeTypes::getDefault()->guessMimeType($filePath);
83+
7684
if (!isset($type)) {
77-
return DEFAULT_MIME_TYPE;
85+
return self::DEFAULT_MIME_TYPE;
7886
}
7987
return $type;
8088
}
8189

8290
/**
83-
* Copy of parent private function
91+
* Copy of parent private function.
92+
*
8493
* @param string $mimeType
94+
*
8595
* @return string
8696
*/
87-
private function guessFileExtension($mimeType)
97+
private function guessFileExtension(string $mimeType): string
8898
{
8999
$candidate = MimeTypes::getDefault()->getExtensions($mimeType);
100+
90101
if (empty($candidate)) {
91-
return DEFAULT_FILE_EXTENSION;
102+
return self::DEFAULT_FILE_EXTENSION;
92103
}
93104
return reset($candidate);
94105
}
95-
96-
/**
97-
* Copy of parent private function
98-
* @param string $sha1
99-
* @param string $extension
100-
* @return string
101-
*/
102-
public function getOutputFileName($sha1, $extension)
103-
{
104-
return $sha1 . '-attachment.' . $extension;
105-
}
106106
}

0 commit comments

Comments
 (0)