Skip to content

Commit b0e9c2e

Browse files
33294: Eliminated singleton usage
1 parent 3bf3648 commit b0e9c2e

File tree

3 files changed

+52
-48
lines changed

3 files changed

+52
-48
lines changed

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

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\FunctionalTestingFramework\Allure\AllureHelper;
1111
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
12+
use Magento\FunctionalTestingFramework\ObjectManager;
1213
use PHPUnit\Framework\TestCase;
1314
use ReflectionProperty;
1415
use Yandex\Allure\Adapter\Allure;
@@ -21,19 +22,6 @@ class AllureHelperTest extends TestCase
2122
{
2223
private const MOCK_FILENAME = 'filename';
2324

24-
/**
25-
* Clear Allure Lifecycle.
26-
*
27-
* @return void
28-
*/
29-
protected function tearDown(): void
30-
{
31-
Allure::setDefaultLifecycle();
32-
$instanceProperty = new ReflectionProperty(AddUniqueAttachmentEvent::class, 'instance');
33-
$instanceProperty->setAccessible(true);
34-
$instanceProperty->setValue(null);
35-
}
36-
3725
/**
3826
* The AddAttachmentToStep should add an attachment to the current step.
3927
*
@@ -121,6 +109,20 @@ public function testAddAttachmentUniqueName(): void
121109
$this->assertNotEquals($attachmentOne, $attachmentTwo);
122110
}
123111

112+
/**
113+
* Clear Allure Lifecycle.
114+
*
115+
* @return void
116+
*/
117+
protected function tearDown(): void
118+
{
119+
Allure::setDefaultLifecycle();
120+
121+
$objectManagerProperty = new ReflectionProperty(ObjectManager::class, 'instance');
122+
$objectManagerProperty->setAccessible(true);
123+
$objectManagerProperty->setValue(null);
124+
}
125+
124126
/**
125127
* Mock entire attachment writing mechanisms.
126128
*
@@ -141,8 +143,23 @@ private function mockAttachmentWriteEvent(string $filePathOrContents, string $ca
141143
->method('getAttachmentFileName')
142144
->willReturn(self::MOCK_FILENAME);
143145

144-
$instanceProperty = new ReflectionProperty(AddUniqueAttachmentEvent::class, 'instance');
145-
$instanceProperty->setAccessible(true);
146-
$instanceProperty->setValue($mockInstance, $mockInstance);
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);
147164
}
148165
}

src/Magento/FunctionalTestingFramework/Allure/AllureHelper.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\FunctionalTestingFramework\Allure;
99

1010
use Magento\FunctionalTestingFramework\Allure\Event\AddUniqueAttachmentEvent;
11+
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
1112
use Yandex\Allure\Adapter\Allure;
1213
use Yandex\Allure\Adapter\AllureException;
1314

@@ -24,7 +25,15 @@ class AllureHelper
2425
*/
2526
public static function addAttachmentToCurrentStep($data, $caption): void
2627
{
27-
Allure::lifecycle()->fire(AddUniqueAttachmentEvent::getInstance($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);
2837
}
2938

3039
/**
@@ -45,8 +54,15 @@ public static function addAttachmentToLastStep($data, $caption): void
4554
// Nothing to attach to; do not fire off allure event
4655
return;
4756
}
48-
49-
$attachmentEvent = AddUniqueAttachmentEvent::getInstance($data, $caption);
57+
58+
/** @var AddUniqueAttachmentEvent $attachmentEvent */
59+
$attachmentEvent = ObjectManagerFactory::getObjectManager()->create(
60+
AddUniqueAttachmentEvent::class,
61+
[
62+
'filePathOrContents' => $data,
63+
'caption' => $caption
64+
]
65+
);
5066
$attachmentEvent->process($trueLastStep);
5167
}
5268
}

src/Magento/FunctionalTestingFramework/Allure/Event/AddUniqueAttachmentEvent.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ class AddUniqueAttachmentEvent extends AddAttachmentEvent
1818
private const DEFAULT_FILE_EXTENSION = 'txt';
1919
private const DEFAULT_MIME_TYPE = 'text/plain';
2020

21-
/**
22-
* @var AddUniqueAttachmentEvent|null
23-
*/
24-
private static $instance;
25-
2621
/**
2722
* Near copy of parent function, added uniqid call for filename to prevent buggy allure behavior.
2823
*
@@ -58,30 +53,6 @@ public function getAttachmentFileName($filePathOrContents, $type): string
5853
return $this->getOutputFileName($fileSha1, $fileExtension);
5954
}
6055

61-
/**
62-
* Unit test helper function.
63-
*
64-
* @param mixed $filePathOrContents
65-
* @param string $caption
66-
* @param string|null $type
67-
*
68-
* @return AddUniqueAttachmentEvent
69-
*/
70-
public static function getInstance(
71-
$filePathOrContents,
72-
string $caption,
73-
?string $type = null
74-
): AddUniqueAttachmentEvent {
75-
if (!self::$instance) {
76-
self::$instance = new AddUniqueAttachmentEvent(
77-
$filePathOrContents,
78-
$caption,
79-
$type
80-
);
81-
}
82-
return self::$instance;
83-
}
84-
8556
/**
8657
* Copies file from one path to another. Wrapper for mocking in unit test.
8758
*

0 commit comments

Comments
 (0)