3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace tests \unit \Magento \FunctionalTestFramework \Allure ;
7
9
8
10
use Magento \FunctionalTestingFramework \Allure \AllureHelper ;
9
11
use Magento \FunctionalTestingFramework \Allure \Event \AddUniqueAttachmentEvent ;
10
12
use PHPUnit \Framework \TestCase ;
13
+ use ReflectionProperty ;
11
14
use Yandex \Allure \Adapter \Allure ;
12
15
use Yandex \Allure \Adapter \AllureException ;
13
16
use Yandex \Allure \Adapter \Event \StepFinishedEvent ;
16
19
17
20
class AllureHelperTest extends TestCase
18
21
{
19
- const MOCK_FILENAME = 'filename ' ;
22
+ private const MOCK_FILENAME = 'filename ' ;
20
23
21
24
/**
22
- * Clear Allure Lifecycle
25
+ * Clear Allure Lifecycle.
26
+ *
27
+ * @return void
23
28
*/
24
- public function tearDown (): void
29
+ protected function tearDown (): void
25
30
{
26
31
Allure::setDefaultLifecycle ();
32
+ $ instanceProperty = new ReflectionProperty (AddUniqueAttachmentEvent::class, 'instance ' );
33
+ $ instanceProperty ->setAccessible (true );
34
+ $ instanceProperty ->setValue (null );
27
35
}
28
36
29
37
/**
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
31
41
* @throws AllureException
32
42
*/
33
- public function testAddAttachmentToStep ()
43
+ public function testAddAttachmentToStep (): void
34
44
{
35
- $ this -> mockAttachmentWriteEvent () ;
36
- $ expectedData = " string " ;
37
- $ expectedCaption = " caption " ;
45
+ $ expectedData = ' string ' ;
46
+ $ expectedCaption = ' caption ' ;
47
+ $ this -> mockAttachmentWriteEvent ( $ expectedData , $ expectedCaption ) ;
38
48
39
49
//Prepare Allure lifecycle
40
50
Allure::lifecycle ()->fire (new StepStartedEvent ('firstStep ' ));
@@ -49,14 +59,16 @@ public function testAddAttachmentToStep()
49
59
}
50
60
51
61
/**
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
53
65
* @throws AllureException
54
66
*/
55
- public function testAddAttachmentToLastStep ()
67
+ public function testAddAttachmentToLastStep (): void
56
68
{
57
- $ this -> mockAttachmentWriteEvent () ;
58
- $ expectedData = " string " ;
59
- $ expectedCaption = " caption " ;
69
+ $ expectedData = ' string ' ;
70
+ $ expectedCaption = ' caption ' ;
71
+ $ this -> mockAttachmentWriteEvent ( $ expectedData , $ expectedCaption ) ;
60
72
61
73
//Prepare Allure lifecycle
62
74
Allure::lifecycle ()->fire (new StepStartedEvent ('firstStep ' ));
@@ -85,14 +97,15 @@ public function testAddAttachmentToLastStep()
85
97
}
86
98
87
99
/**
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
89
103
* @throws AllureException
90
104
*/
91
- public function testAddAttachementUniqueName ()
105
+ public function testAddAttachmentUniqueName (): void
92
106
{
93
- $ expectedData = "string " ;
94
- $ expectedCaption = "caption " ;
95
-
107
+ $ expectedData = 'string ' ;
108
+ $ expectedCaption = 'caption ' ;
96
109
$ this ->mockCopyFile ($ expectedData , $ expectedCaption );
97
110
98
111
//Prepare Allure lifecycle
@@ -110,27 +123,52 @@ public function testAddAttachementUniqueName()
110
123
}
111
124
112
125
/**
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
114
132
*/
115
- public function mockAttachmentWriteEvent ()
133
+ private function mockAttachmentWriteEvent (string $ filePathOrContents , string $ caption ): void
116
134
{
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
119
142
->method ('getAttachmentFileName ' )
120
143
->willReturn (self ::MOCK_FILENAME );
144
+
145
+ $ instanceProperty = new ReflectionProperty (AddUniqueAttachmentEvent::class, 'instance ' );
146
+ $ instanceProperty ->setAccessible (true );
147
+ $ instanceProperty ->setValue ($ mockInstance , $ mockInstance );
121
148
}
122
149
123
150
/**
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
126
157
*/
127
- public function mockCopyFile (string $ expectedData , string $ expectedCaption )
158
+ private function mockCopyFile (string $ filePathOrContents , string $ caption ): void
128
159
{
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 );
135
173
}
136
174
}
0 commit comments