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 ;
12
+ use Magento \FunctionalTestingFramework \ObjectManager ;
13
+ use PHPUnit \Framework \TestCase ;
14
+ use ReflectionProperty ;
10
15
use Yandex \Allure \Adapter \Allure ;
11
- use Yandex \Allure \Adapter \Event \ AddAttachmentEvent ;
16
+ use Yandex \Allure \Adapter \AllureException ;
12
17
use Yandex \Allure \Adapter \Event \StepFinishedEvent ;
13
18
use Yandex \Allure \Adapter \Event \StepStartedEvent ;
14
19
use Yandex \Allure \Adapter \Model \Attachment ;
15
- use AspectMock \Test as AspectMock ;
16
- use PHPUnit \Framework \TestCase ;
17
20
18
21
class AllureHelperTest extends TestCase
19
22
{
20
- const MOCK_FILENAME = 'filename ' ;
23
+ private const MOCK_FILENAME = 'filename ' ;
21
24
22
25
/**
23
- * Clear Allure Lifecycle
26
+ * The AddAttachmentToStep should add an attachment to the current step.
27
+ *
28
+ * @return void
29
+ * @throws AllureException
24
30
*/
25
- public function tearDown (): void
31
+ public function testAddAttachmentToStep (): void
26
32
{
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 );
40
36
41
37
//Prepare Allure lifecycle
42
38
Allure::lifecycle ()->fire (new StepStartedEvent ('firstStep ' ));
@@ -51,14 +47,16 @@ public function testAddAttachmentToStep()
51
47
}
52
48
53
49
/**
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
56
54
*/
57
- public function testAddAttachmentToLastStep ()
55
+ public function testAddAttachmentToLastStep (): void
58
56
{
59
- $ this -> mockAttachmentWriteEvent () ;
60
- $ expectedData = " string " ;
61
- $ expectedCaption = " caption " ;
57
+ $ expectedData = ' string ' ;
58
+ $ expectedCaption = ' caption ' ;
59
+ $ this -> mockAttachmentWriteEvent ( $ expectedData , $ expectedCaption ) ;
62
60
63
61
//Prepare Allure lifecycle
64
62
Allure::lifecycle ()->fire (new StepStartedEvent ('firstStep ' ));
@@ -87,14 +85,15 @@ public function testAddAttachmentToLastStep()
87
85
}
88
86
89
87
/**
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
92
92
*/
93
- public function testAddAttachementUniqueName ()
93
+ public function testAddAttachmentUniqueName (): void
94
94
{
95
- $ this ->mockCopyFile ();
96
- $ expectedData = "string " ;
97
- $ expectedCaption = "caption " ;
95
+ $ expectedData = 'string ' ;
96
+ $ expectedCaption = 'caption ' ;
98
97
99
98
//Prepare Allure lifecycle
100
99
Allure::lifecycle ()->fire (new StepStartedEvent ('firstStep ' ));
@@ -111,24 +110,56 @@ public function testAddAttachementUniqueName()
111
110
}
112
111
113
112
/**
114
- * Mock entire attachment writing mechanisms
115
- * @throws \Exception
113
+ * Clear Allure Lifecycle.
114
+ *
115
+ * @return void
116
116
*/
117
- public function mockAttachmentWriteEvent ()
117
+ protected function tearDown (): void
118
118
{
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 );
122
124
}
123
125
124
126
/**
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
127
133
*/
128
- public function mockCopyFile ()
134
+ private function mockAttachmentWriteEvent ( string $ filePathOrContents , string $ caption ): void
129
135
{
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 );
133
164
}
134
165
}
0 commit comments