Skip to content

Commit 0721011

Browse files
33309: Code review/refactoring, fixing failed tests
1 parent ea1b727 commit 0721011

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php

+43-23
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ class TestGeneratorTest extends MagentoTestCase
2525
{
2626
/**
2727
* Before method functionality.
28+
*
29+
* @return void
2830
*/
29-
public function setUp(): void
31+
protected function setUp(): void
3032
{
3133
TestLoggingUtil::getInstance()->setMockLoggingUtil();
3234
}
@@ -36,7 +38,7 @@ public function setUp(): void
3638
*
3739
* @return void
3840
*/
39-
public function tearDown(): void
41+
protected function tearDown(): void
4042
{
4143
GenerationErrorHandler::getInstance()->reset();
4244
}
@@ -53,12 +55,12 @@ public function testEntityException(): void
5355
'userInput' => '{{someEntity.entity}}'
5456
]);
5557

56-
$testObject = new TestObject("sampleTest", ["merge123" => $actionObject], [], [], "filename");
57-
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]);
58+
$testObject = new TestObject('sampleTest', ['merge123' => $actionObject], [], [], 'filename');
59+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]);
5860
$testGeneratorObject->createAllTestFiles(null, []);
5961

6062
// assert that no exception for createAllTestFiles and generation error is stored in GenerationErrorHandler
61-
$errorMessage = '/' . preg_quote("Removed invalid test object sampleTest") . '/';
63+
$errorMessage = '/' . preg_quote('Removed invalid test object sampleTest') . '/';
6264
TestLoggingUtil::getInstance()->validateMockLogStatmentRegex('error', $errorMessage, []);
6365
$testErrors = GenerationErrorHandler::getInstance()->getErrorsByType('test');
6466
$this->assertArrayHasKey('sampleTest', $testErrors);
@@ -78,9 +80,9 @@ public function testSkippedNoGeneration(): void
7880
]);
7981

8082
$annotations = ['skip' => ['issue']];
81-
$testObject = new TestObject("sampleTest", ["merge123" => $actionObject], $annotations, [], "filename");
83+
$testObject = new TestObject('sampleTest', ['merge123' => $actionObject], $annotations, [], 'filename');
8284

83-
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]);
85+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]);
8486
$output = $testGeneratorObject->assembleTestPhp($testObject);
8587

8688
$this->assertStringContainsString('This test is skipped', $output);
@@ -115,16 +117,16 @@ public function testAllowSkipped(): void
115117
]);
116118

117119
$annotations = ['skip' => ['issue']];
118-
$beforeHook = new TestHookObject("before", "sampleTest", ['beforeAction' => $beforeActionObject]);
120+
$beforeHook = new TestHookObject('before', 'sampleTest', ['beforeAction' => $beforeActionObject]);
119121
$testObject = new TestObject(
120-
"sampleTest",
121-
["fakeAction" => $actionObject],
122+
'sampleTest',
123+
['fakeAction' => $actionObject],
122124
$annotations,
123-
["before" => $beforeHook],
124-
"filename"
125+
['before' => $beforeHook],
126+
'filename'
125127
);
126128

127-
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]);
129+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]);
128130
$output = $testGeneratorObject->assembleTestPhp($testObject);
129131

130132
$this->assertStringNotContainsString('This test is skipped', $output);
@@ -141,8 +143,10 @@ public function testAllowSkipped(): void
141143
public function testFilter(): void
142144
{
143145
$mockConfig = $this->createMock(MftfApplicationConfig::class);
144-
$fileList = new FilterList(['severity' => ["CRITICAL"]]);
145-
$mockConfig->expects($this->once())->method('getFilterList')->willReturn($fileList);
146+
$fileList = new FilterList(['severity' => ['CRITICAL']]);
147+
$mockConfig
148+
->method('getFilterList')
149+
->willReturn($fileList);
146150

147151
$property = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT');
148152
$property->setAccessible(true);
@@ -156,24 +160,24 @@ public function testFilter(): void
156160
$annotation1 = ['severity' => ['CRITICAL']];
157161
$annotation2 = ['severity' => ['MINOR']];
158162
$test1 = new TestObject(
159-
"test1",
160-
["fakeAction" => $actionObject],
163+
'test1',
164+
['fakeAction' => $actionObject],
161165
$annotation1,
162166
[],
163-
"filename"
167+
'filename'
164168
);
165169
$test2 = new TestObject(
166-
"test2",
167-
["fakeAction" => $actionObject],
170+
'test2',
171+
['fakeAction' => $actionObject],
168172
$annotation2,
169173
[],
170-
"filename"
174+
'filename'
171175
);
172176

173177
// Mock createCestFile to return name of tests that testGenerator tried to create
174178
$generatedTests = [];
175179
$cestFileCreatorUtil = $this->createMock(CestFileCreatorUtil::class);
176-
$cestFileCreatorUtil->expects($this->once())
180+
$cestFileCreatorUtil
177181
->method('create')
178182
->will(
179183
$this->returnCallback(
@@ -187,11 +191,27 @@ function ($filename) use (&$generatedTests) {
187191
$property->setAccessible(true);
188192
$property->setValue($cestFileCreatorUtil);
189193

190-
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $test1, "test2" => $test2]);
194+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $test1, 'test2' => $test2]);
191195
$testGeneratorObject->createAllTestFiles();
192196

193197
// Ensure Test1 was Generated but not Test 2
194198
$this->assertArrayHasKey('test1Cest', $generatedTests);
195199
$this->assertArrayNotHasKey('test2Cest', $generatedTests);
196200
}
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+
}
197217
}

src/Magento/FunctionalTestingFramework/Util/Filesystem/CestFileCreatorUtil.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ private function __construct()
3030
*
3131
* @return CestFileCreatorUtil
3232
*/
33-
public static function getInstance()
33+
public static function getInstance(): CestFileCreatorUtil
3434
{
35-
if (self::$INSTANCE === null) {
36-
return new self();
35+
if (!self::$INSTANCE) {
36+
self::$INSTANCE = new CestFileCreatorUtil();
3737
}
3838

3939
return self::$INSTANCE;

0 commit comments

Comments
 (0)