Skip to content

Commit 1a71fe7

Browse files
33294: Eliminated aspect mock from AllureHelperTest
1 parent 9484d85 commit 1a71fe7

File tree

3 files changed

+135
-68
lines changed

3 files changed

+135
-68
lines changed

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

+69-31
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
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;
1012
use PHPUnit\Framework\TestCase;
13+
use ReflectionProperty;
1114
use Yandex\Allure\Adapter\Allure;
1215
use Yandex\Allure\Adapter\AllureException;
1316
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
@@ -16,25 +19,32 @@
1619

1720
class AllureHelperTest extends TestCase
1821
{
19-
const MOCK_FILENAME = 'filename';
22+
private const MOCK_FILENAME = 'filename';
2023

2124
/**
22-
* Clear Allure Lifecycle
25+
* Clear Allure Lifecycle.
26+
*
27+
* @return void
2328
*/
24-
public function tearDown(): void
29+
protected function tearDown(): void
2530
{
2631
Allure::setDefaultLifecycle();
32+
$instanceProperty = new ReflectionProperty(AddUniqueAttachmentEvent::class, 'instance');
33+
$instanceProperty->setAccessible(true);
34+
$instanceProperty->setValue(null);
2735
}
2836

2937
/**
30-
* AddAttachmentToStep should add an attachment to the current step
38+
* The AddAttachmentToStep should add an attachment to the current step.
39+
*
40+
* @return void
3141
* @throws AllureException
3242
*/
33-
public function testAddAttachmentToStep()
43+
public function testAddAttachmentToStep(): void
3444
{
35-
$this->mockAttachmentWriteEvent();
36-
$expectedData = "string";
37-
$expectedCaption = "caption";
45+
$expectedData = 'string';
46+
$expectedCaption = 'caption';
47+
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);
3848

3949
//Prepare Allure lifecycle
4050
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
@@ -49,14 +59,16 @@ public function testAddAttachmentToStep()
4959
}
5060

5161
/**
52-
* AddAttachmentToLastStep should add an attachment only to the last step
62+
* The AddAttachmentToLastStep should add an attachment only to the last step.
63+
*
64+
* @return void
5365
* @throws AllureException
5466
*/
55-
public function testAddAttachmentToLastStep()
67+
public function testAddAttachmentToLastStep(): void
5668
{
57-
$this->mockAttachmentWriteEvent();
58-
$expectedData = "string";
59-
$expectedCaption = "caption";
69+
$expectedData = 'string';
70+
$expectedCaption = 'caption';
71+
$this->mockAttachmentWriteEvent($expectedData, $expectedCaption);
6072

6173
//Prepare Allure lifecycle
6274
Allure::lifecycle()->fire(new StepStartedEvent('firstStep'));
@@ -85,14 +97,15 @@ public function testAddAttachmentToLastStep()
8597
}
8698

8799
/**
88-
* AddAttachment actions should have files with different attachment names
100+
* The AddAttachment actions should have files with different attachment names.
101+
*
102+
* @return void
89103
* @throws AllureException
90104
*/
91-
public function testAddAttachementUniqueName()
105+
public function testAddAttachmentUniqueName(): void
92106
{
93-
$expectedData = "string";
94-
$expectedCaption = "caption";
95-
107+
$expectedData = 'string';
108+
$expectedCaption = 'caption';
96109
$this->mockCopyFile($expectedData, $expectedCaption);
97110

98111
//Prepare Allure lifecycle
@@ -110,27 +123,52 @@ public function testAddAttachementUniqueName()
110123
}
111124

112125
/**
113-
* Mock entire attachment writing mechanisms
126+
* Mock entire attachment writing mechanisms.
127+
*
128+
* @param string $filePathOrContents
129+
* @param string $caption
130+
*
131+
* @return void
114132
*/
115-
public function mockAttachmentWriteEvent()
133+
private function mockAttachmentWriteEvent(string $filePathOrContents, string $caption): void
116134
{
117-
$this->createMock(AddUniqueAttachmentEvent::class)
118-
->expects($this->any())
135+
$mockInstance = $this->getMockBuilder(AddUniqueAttachmentEvent::class)
136+
->setConstructorArgs([$filePathOrContents, $caption])
137+
->disallowMockingUnknownTypes()
138+
->onlyMethods(['getAttachmentFileName'])
139+
->getMock();
140+
141+
$mockInstance
119142
->method('getAttachmentFileName')
120143
->willReturn(self::MOCK_FILENAME);
144+
145+
$instanceProperty = new ReflectionProperty(AddUniqueAttachmentEvent::class, 'instance');
146+
$instanceProperty->setAccessible(true);
147+
$instanceProperty->setValue($mockInstance, $mockInstance);
121148
}
122149

123150
/**
124-
* Mock only file writing mechanism
125-
* @throws \ReflectionException
151+
* Mock only file writing mechanism.
152+
*
153+
* @param string $filePathOrContents
154+
* @param string $caption
155+
*
156+
* @return void
126157
*/
127-
public function mockCopyFile(string $expectedData, string $expectedCaption)
158+
private function mockCopyFile(string $filePathOrContents, string $caption): void
128159
{
129-
$addUniqueAttachmentEvent = new AddUniqueAttachmentEvent($expectedData, $expectedCaption);
130-
$reflection = new \ReflectionClass(AddUniqueAttachmentEvent::class);
131-
$reflectionMethod = $reflection->getMethod('copyFile');
132-
$reflectionMethod->setAccessible(true);
133-
$output = $reflectionMethod->invoke($addUniqueAttachmentEvent);
134-
$this->assertEquals(true, $output);
160+
$mockInstance = $this->getMockBuilder(AddUniqueAttachmentEvent::class)
161+
->setConstructorArgs([$filePathOrContents, $caption])
162+
->disallowMockingUnknownTypes()
163+
->onlyMethods(['copyFile'])
164+
->getMock();
165+
166+
$mockInstance
167+
->method('copyFile')
168+
->willReturn(true);
169+
170+
$instanceProperty = new ReflectionProperty(AddUniqueAttachmentEvent::class, 'instance');
171+
$instanceProperty->setAccessible(true);
172+
$instanceProperty->setValue($mockInstance, $mockInstance);
135173
}
136174
}

src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,40 @@
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;
911
use Yandex\Allure\Adapter\Allure;
10-
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
12+
use Yandex\Allure\Adapter\AllureException;
1113

1214
class AllureHelper
1315
{
1416
/**
15-
* Adds attachment to the current step
17+
* Adds attachment to the current step.
18+
*
1619
* @param mixed $data
1720
* @param string $caption
18-
* @throws \Yandex\Allure\Adapter\AllureException
21+
*
1922
* @return void
23+
* @throws AllureException
2024
*/
21-
public static function addAttachmentToCurrentStep($data, $caption)
25+
public static function addAttachmentToCurrentStep($data, $caption): void
2226
{
23-
Allure::lifecycle()->fire(new AddUniqueAttachmentEvent($data, $caption));
27+
Allure::lifecycle()->fire(AddUniqueAttachmentEvent::getInstance($data, $caption));
2428
}
2529

2630
/**
2731
* Adds Attachment to the last executed step.
2832
* Use this when adding attachments outside of an $I->doSomething() step/context.
33+
*
2934
* @param mixed $data
3035
* @param string $caption
36+
*
3137
* @return void
3238
*/
33-
public static function addAttachmentToLastStep($data, $caption)
39+
public static function addAttachmentToLastStep($data, $caption): void
3440
{
3541
$rootStep = Allure::lifecycle()->getStepStorage()->getLast();
3642
$trueLastStep = array_last($rootStep->getSteps());
@@ -40,7 +46,7 @@ public static function addAttachmentToLastStep($data, $caption)
4046
return;
4147
}
4248

43-
$attachmentEvent = new AddUniqueAttachmentEvent($data, $caption);
49+
$attachmentEvent = AddUniqueAttachmentEvent::getInstance($data, $caption);
4450
$attachmentEvent->process($trueLastStep);
4551
}
4652
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,63 @@
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

910
use Symfony\Component\Mime\MimeTypes;
1011
use Yandex\Allure\Adapter\AllureException;
1112
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
1213

13-
const DEFAULT_FILE_EXTENSION = 'txt';
14-
const DEFAULT_MIME_TYPE = 'text/plain';
15-
1614
class AddUniqueAttachmentEvent extends AddAttachmentEvent
1715
{
16+
private const DEFAULT_FILE_EXTENSION = 'txt';
17+
private const DEFAULT_MIME_TYPE = 'text/plain';
18+
1819
/**
19-
* @var string
20+
* @var AddUniqueAttachmentEvent|null
2021
*/
21-
private $type;
22+
private static $instance;
2223

2324
/**
24-
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior
25-
* @param string $filePathOrContents
25+
* An alternative way to instantiate an instance of this class. Used to mock this class object in unit tests.
26+
*
27+
* @param mixed $filePathOrContents
28+
* @param string $caption
29+
* @param string|null $type
30+
*
31+
* @return AddUniqueAttachmentEvent
32+
*/
33+
public static function getInstance(
34+
$filePathOrContents,
35+
string $caption,
36+
?string $type = null
37+
): AddUniqueAttachmentEvent {
38+
if (!self::$instance) {
39+
self::$instance = new AddUniqueAttachmentEvent(
40+
$filePathOrContents,
41+
$caption,
42+
$type
43+
);
44+
}
45+
return self::$instance;
46+
}
47+
48+
/**
49+
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior.
50+
*
51+
* @param mixed $filePathOrContents
2652
* @param string $type
53+
*
2754
* @return string
2855
* @throws AllureException
2956
*/
30-
public function getAttachmentFileName($filePathOrContents, $type)
57+
public function getAttachmentFileName($filePathOrContents, $type): string
3158
{
3259
$filePath = $filePathOrContents;
60+
3361
if (!file_exists($filePath) || !is_file($filePath)) {
3462
//Save contents to temporary file
3563
$filePath = tempnam(sys_get_temp_dir(), 'allure-attachment');
@@ -40,13 +68,11 @@ public function getAttachmentFileName($filePathOrContents, $type)
4068

4169
if (!isset($type)) {
4270
$type = $this->guessFileMimeType($filePath);
43-
$this->type = $type;
4471
}
45-
4672
$fileExtension = $this->guessFileExtension($type);
47-
4873
$fileSha1 = uniqid(sha1_file($filePath));
4974
$outputPath = parent::getOutputPath($fileSha1, $fileExtension);
75+
5076
if (!$this->copyFile($filePath, $outputPath)) {
5177
throw new AllureException("Failed to copy attachment from $filePath to $outputPath.");
5278
}
@@ -56,51 +82,48 @@ public function getAttachmentFileName($filePathOrContents, $type)
5682

5783
/**
5884
* Copies file from one path to another. Wrapper for mocking in unit test.
85+
*
5986
* @param string $filePath
6087
* @param string $outputPath
88+
*
6189
* @return boolean
6290
*/
63-
private function copyFile($filePath, $outputPath)
91+
public function copyFile(string $filePath, string $outputPath): bool
6492
{
6593
return copy($filePath, $outputPath);
6694
}
6795

6896
/**
69-
* Copy of parent private function
97+
* Copy of parent private function.
98+
*
7099
* @param string $filePath
100+
*
71101
* @return string
72102
*/
73-
private function guessFileMimeType($filePath)
103+
private function guessFileMimeType(string $filePath): string
74104
{
75105
$type = MimeTypes::getDefault()->guessMimeType($filePath);
106+
76107
if (!isset($type)) {
77-
return DEFAULT_MIME_TYPE;
108+
return self::DEFAULT_MIME_TYPE;
78109
}
79110
return $type;
80111
}
81112

82113
/**
83-
* Copy of parent private function
114+
* Copy of parent private function.
115+
*
84116
* @param string $mimeType
117+
*
85118
* @return string
86119
*/
87-
private function guessFileExtension($mimeType)
120+
private function guessFileExtension(string $mimeType): string
88121
{
89122
$candidate = MimeTypes::getDefault()->getExtensions($mimeType);
123+
90124
if (empty($candidate)) {
91-
return DEFAULT_FILE_EXTENSION;
125+
return self::DEFAULT_FILE_EXTENSION;
92126
}
93127
return reset($candidate);
94128
}
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-
}
106129
}

0 commit comments

Comments
 (0)