33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
7+
68namespace tests \unit \Magento \FunctionalTestFramework \Allure ;
79
810use Magento \FunctionalTestingFramework \Allure \AllureHelper ;
911use Magento \FunctionalTestingFramework \Allure \Event \AddUniqueAttachmentEvent ;
12+ use Magento \FunctionalTestingFramework \ObjectManager ;
13+ use PHPUnit \Framework \TestCase ;
14+ use ReflectionProperty ;
1015use Yandex \Allure \Adapter \Allure ;
11- use Yandex \Allure \Adapter \Event \ AddAttachmentEvent ;
16+ use Yandex \Allure \Adapter \AllureException ;
1217use Yandex \Allure \Adapter \Event \StepFinishedEvent ;
1318use Yandex \Allure \Adapter \Event \StepStartedEvent ;
1419use Yandex \Allure \Adapter \Model \Attachment ;
15- use AspectMock \Test as AspectMock ;
16- use PHPUnit \Framework \TestCase ;
1720
1821class 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}
0 commit comments