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 ;
1012use PHPUnit \Framework \TestCase ;
13+ use ReflectionProperty ;
1114use Yandex \Allure \Adapter \Allure ;
1215use Yandex \Allure \Adapter \AllureException ;
1316use Yandex \Allure \Adapter \Event \StepFinishedEvent ;
1619
1720class 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}
0 commit comments