3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace tests \unit \Magento \FunctionalTestFramework \Util ;
8
9
9
- use AspectMock \Test as AspectMock ;
10
-
10
+ use Exception ;
11
+ use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
12
+ use Magento \FunctionalTestingFramework \Exceptions \TestReferenceException ;
11
13
use Magento \FunctionalTestingFramework \Filter \FilterList ;
12
- use Magento \FunctionalTestingFramework \Test \Handlers \TestObjectHandler ;
13
14
use Magento \FunctionalTestingFramework \Test \Objects \ActionObject ;
14
15
use Magento \FunctionalTestingFramework \Test \Objects \TestHookObject ;
15
16
use Magento \FunctionalTestingFramework \Test \Objects \TestObject ;
16
- use tests \unit \Util \MagentoTestCase ;
17
+ use Magento \FunctionalTestingFramework \Util \Filesystem \CestFileCreatorUtil ;
18
+ use Magento \FunctionalTestingFramework \Util \GenerationErrorHandler ;
17
19
use Magento \FunctionalTestingFramework \Util \TestGenerator ;
18
- use Magento \FunctionalTestingFramework \Config \MftfApplicationConfig ;
20
+ use ReflectionProperty ;
21
+ use tests \unit \Util \MagentoTestCase ;
19
22
use tests \unit \Util \TestLoggingUtil ;
20
- use Magento \FunctionalTestingFramework \Util \GenerationErrorHandler ;
21
23
22
24
class TestGeneratorTest extends MagentoTestCase
23
25
{
24
26
/**
25
- * Before method functionality
27
+ * Before method functionality.
28
+ *
29
+ * @return void
26
30
*/
27
- public function setUp (): void
31
+ protected function setUp (): void
28
32
{
29
33
TestLoggingUtil::getInstance ()->setMockLoggingUtil ();
30
34
}
31
35
32
36
/**
33
- * After method functionality
37
+ * After method functionality.
34
38
*
35
39
* @return void
36
40
*/
37
- public function tearDown (): void
41
+ protected function tearDown (): void
38
42
{
39
- AspectMock::clean ();
40
43
GenerationErrorHandler::getInstance ()->reset ();
41
44
}
42
45
43
46
/**
44
47
* Basic test to check exceptions for incorrect entities.
45
48
*
46
- * @throws \Exception
49
+ * @return void
50
+ * @throws Exception
47
51
*/
48
- public function testEntityException ()
52
+ public function testEntityException (): void
49
53
{
50
54
$ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
51
55
'userInput ' => '{{someEntity.entity}} '
52
56
]);
53
57
54
- $ testObject = new TestObject ("sampleTest " , ["merge123 " => $ actionObject ], [], [], "filename " );
55
-
56
- AspectMock::double (TestObjectHandler::class, ['initTestData ' => '' ]);
57
-
58
- $ testGeneratorObject = TestGenerator::getInstance ("" , ["sampleTest " => $ testObject ]);
59
-
60
- AspectMock::double (TestGenerator::class, ['loadAllTestObjects ' => ["sampleTest " => $ testObject ]]);
61
-
58
+ $ testObject = new TestObject ('sampleTest ' , ['merge123 ' => $ actionObject ], [], [], 'filename ' );
59
+ $ testGeneratorObject = TestGenerator::getInstance ('' , ['sampleTest ' => $ testObject ]);
62
60
$ testGeneratorObject ->createAllTestFiles (null , []);
63
61
64
62
// assert that no exception for createAllTestFiles and generation error is stored in GenerationErrorHandler
65
- $ errorMessage = '/ ' . preg_quote (" Removed invalid test object sampleTest " ) . '/ ' ;
63
+ $ errorMessage = '/ ' . preg_quote (' Removed invalid test object sampleTest ' ) . '/ ' ;
66
64
TestLoggingUtil::getInstance ()->validateMockLogStatmentRegex ('error ' , $ errorMessage , []);
67
65
$ testErrors = GenerationErrorHandler::getInstance ()->getErrorsByType ('test ' );
68
66
$ this ->assertArrayHasKey ('sampleTest ' , $ testErrors );
69
67
}
70
68
71
69
/**
72
- * Tests that skipped tests do not have a fully generated body
70
+ * Tests that skipped tests do not have a fully generated body.
73
71
*
74
- * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
72
+ * @return void
73
+ * @throws TestReferenceException
75
74
*/
76
- public function testSkippedNoGeneration ()
75
+ public function testSkippedNoGeneration (): void
77
76
{
78
77
$ actionInput = 'fakeInput ' ;
79
78
$ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
80
79
'userInput ' => $ actionInput
81
80
]);
82
81
83
82
$ annotations = ['skip ' => ['issue ' ]];
84
- $ testObject = new TestObject (" sampleTest " , [" merge123 " => $ actionObject ], $ annotations , [], " filename " );
83
+ $ testObject = new TestObject (' sampleTest ' , [' merge123 ' => $ actionObject ], $ annotations , [], ' filename ' );
85
84
86
- $ testGeneratorObject = TestGenerator::getInstance ("" , [" sampleTest " => $ testObject ]);
85
+ $ testGeneratorObject = TestGenerator::getInstance ('' , [' sampleTest ' => $ testObject ]);
87
86
$ output = $ testGeneratorObject ->assembleTestPhp ($ testObject );
88
87
89
88
$ this ->assertStringContainsString ('This test is skipped ' , $ output );
90
89
$ this ->assertStringNotContainsString ($ actionInput , $ output );
91
90
}
92
91
93
92
/**
94
- * Tests that skipped tests have a fully generated body when --allowSkipped is passed in
93
+ * Tests that skipped tests have a fully generated body when --allowSkipped is passed in.
95
94
*
96
- * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
95
+ * @return void
96
+ * @throws TestReferenceException
97
97
*/
98
- public function testAllowSkipped ()
98
+ public function testAllowSkipped (): void
99
99
{
100
100
// Mock allowSkipped for TestGenerator
101
- AspectMock::double (MftfApplicationConfig::class, ['allowSkipped ' => true ]);
101
+ $ mockConfig = $ this ->createMock (MftfApplicationConfig::class);
102
+ $ mockConfig
103
+ ->method ('allowSkipped ' )
104
+ ->willReturn (true );
105
+
106
+ $ property = new ReflectionProperty (MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT ' );
107
+ $ property ->setAccessible (true );
108
+ $ property ->setValue ($ mockConfig );
102
109
103
110
$ actionInput = 'fakeInput ' ;
104
111
$ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
@@ -110,16 +117,16 @@ public function testAllowSkipped()
110
117
]);
111
118
112
119
$ annotations = ['skip ' => ['issue ' ]];
113
- $ beforeHook = new TestHookObject (" before " , " sampleTest " , ['beforeAction ' => $ beforeActionObject ]);
120
+ $ beforeHook = new TestHookObject (' before ' , ' sampleTest ' , ['beforeAction ' => $ beforeActionObject ]);
114
121
$ testObject = new TestObject (
115
- " sampleTest " ,
116
- [" fakeAction " => $ actionObject ],
122
+ ' sampleTest ' ,
123
+ [' fakeAction ' => $ actionObject ],
117
124
$ annotations ,
118
- [" before " => $ beforeHook ],
119
- " filename "
125
+ [' before ' => $ beforeHook ],
126
+ ' filename '
120
127
);
121
128
122
- $ testGeneratorObject = TestGenerator::getInstance ("" , [" sampleTest " => $ testObject ]);
129
+ $ testGeneratorObject = TestGenerator::getInstance ('' , [' sampleTest ' => $ testObject ]);
123
130
$ output = $ testGeneratorObject ->assembleTestPhp ($ testObject );
124
131
125
132
$ this ->assertStringNotContainsString ('This test is skipped ' , $ output );
@@ -128,17 +135,22 @@ public function testAllowSkipped()
128
135
}
129
136
130
137
/**
131
- * Tests that TestGenerator createAllTestFiles correctly filters based on severity
138
+ * Tests that TestGenerator createAllTestFiles correctly filters based on severity.
132
139
*
133
- * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
140
+ * @return void
141
+ * @throws TestReferenceException
134
142
*/
135
- public function testFilter ()
143
+ public function testFilter (): void
136
144
{
137
- // Mock filters for TestGenerator
138
- AspectMock::double (
139
- MftfApplicationConfig::class,
140
- ['getFilterList ' => new FilterList (['severity ' => ["CRITICAL " ]])]
141
- );
145
+ $ mockConfig = $ this ->createMock (MftfApplicationConfig::class);
146
+ $ fileList = new FilterList (['severity ' => ['CRITICAL ' ]]);
147
+ $ mockConfig
148
+ ->method ('getFilterList ' )
149
+ ->willReturn ($ fileList );
150
+
151
+ $ property = new ReflectionProperty (MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT ' );
152
+ $ property ->setAccessible (true );
153
+ $ property ->setValue ($ mockConfig );
142
154
143
155
$ actionInput = 'fakeInput ' ;
144
156
$ actionObject = new ActionObject ('fakeAction ' , 'comment ' , [
@@ -148,32 +160,58 @@ public function testFilter()
148
160
$ annotation1 = ['severity ' => ['CRITICAL ' ]];
149
161
$ annotation2 = ['severity ' => ['MINOR ' ]];
150
162
$ test1 = new TestObject (
151
- " test1 " ,
152
- [" fakeAction " => $ actionObject ],
163
+ ' test1 ' ,
164
+ [' fakeAction ' => $ actionObject ],
153
165
$ annotation1 ,
154
166
[],
155
- " filename "
167
+ ' filename '
156
168
);
157
169
$ test2 = new TestObject (
158
- " test2 " ,
159
- [" fakeAction " => $ actionObject ],
170
+ ' test2 ' ,
171
+ [' fakeAction ' => $ actionObject ],
160
172
$ annotation2 ,
161
173
[],
162
- " filename "
174
+ ' filename '
163
175
);
164
- AspectMock::double (TestGenerator::class, ['loadAllTestObjects ' => ["sampleTest " => $ test1 , "test2 " => $ test2 ]]);
165
176
166
177
// Mock createCestFile to return name of tests that testGenerator tried to create
167
178
$ generatedTests = [];
168
- AspectMock::double (TestGenerator::class, ['createCestFile ' => function ($ arg1 , $ arg2 ) use (&$ generatedTests ) {
169
- $ generatedTests [$ arg2 ] = true ;
170
- }]);
171
-
172
- $ testGeneratorObject = TestGenerator::getInstance ("" , ["sampleTest " => $ test1 , "test2 " => $ test2 ]);
173
- $ testGeneratorObject ->createAllTestFiles (null , []);
179
+ $ cestFileCreatorUtil = $ this ->createMock (CestFileCreatorUtil::class);
180
+ $ cestFileCreatorUtil
181
+ ->method ('create ' )
182
+ ->will (
183
+ $ this ->returnCallback (
184
+ function ($ filename ) use (&$ generatedTests ) {
185
+ $ generatedTests [$ filename ] = true ;
186
+ }
187
+ )
188
+ );
189
+
190
+ $ property = new ReflectionProperty (CestFileCreatorUtil::class, 'INSTANCE ' );
191
+ $ property ->setAccessible (true );
192
+ $ property ->setValue ($ cestFileCreatorUtil );
193
+
194
+ $ testGeneratorObject = TestGenerator::getInstance ('' , ['sampleTest ' => $ test1 , 'test2 ' => $ test2 ]);
195
+ $ testGeneratorObject ->createAllTestFiles ();
174
196
175
197
// Ensure Test1 was Generated but not Test 2
176
198
$ this ->assertArrayHasKey ('test1Cest ' , $ generatedTests );
177
199
$ this ->assertArrayNotHasKey ('test2Cest ' , $ generatedTests );
178
200
}
201
+
202
+ /**
203
+ * @inheritDoc
204
+ */
205
+ public static function tearDownAfterClass (): void
206
+ {
207
+ parent ::tearDownAfterClass ();
208
+
209
+ $ cestFileCreatorUtilInstance = new ReflectionProperty (CestFileCreatorUtil::class, 'INSTANCE ' );
210
+ $ cestFileCreatorUtilInstance ->setAccessible (true );
211
+ $ cestFileCreatorUtilInstance ->setValue (null );
212
+
213
+ $ mftfAppConfigInstance = new ReflectionProperty (MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT ' );
214
+ $ mftfAppConfigInstance ->setAccessible (true );
215
+ $ mftfAppConfigInstance ->setValue (null );
216
+ }
179
217
}
0 commit comments