Skip to content

Commit 172a1ef

Browse files
committed
33309: Eliminated AspectMock usage from TestGeneratorTest.php
1 parent 9dfd3e3 commit 172a1ef

File tree

3 files changed

+121
-44
lines changed

3 files changed

+121
-44
lines changed

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

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace tests\unit\Magento\FunctionalTestFramework\Util;
89

9-
use AspectMock\Test as AspectMock;
10-
10+
use Exception;
11+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1112
use Magento\FunctionalTestingFramework\Filter\FilterList;
12-
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
1313
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1414
use Magento\FunctionalTestingFramework\Test\Objects\TestHookObject;
1515
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
16+
use Magento\FunctionalTestingFramework\Util\Filesystem\CestFileCreatorUtil;
17+
use ReflectionProperty;
1618
use tests\unit\Util\MagentoTestCase;
1719
use Magento\FunctionalTestingFramework\Util\TestGenerator;
1820
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
@@ -22,43 +24,37 @@
2224
class TestGeneratorTest extends MagentoTestCase
2325
{
2426
/**
25-
* Before method functionality
27+
* Before method functionality.
2628
*/
2729
public function setUp(): void
2830
{
2931
TestLoggingUtil::getInstance()->setMockLoggingUtil();
3032
}
3133

3234
/**
33-
* After method functionality
35+
* After method functionality.
3436
*
3537
* @return void
3638
*/
3739
public function tearDown(): void
3840
{
39-
AspectMock::clean();
4041
GenerationErrorHandler::getInstance()->reset();
4142
}
4243

4344
/**
4445
* Basic test to check exceptions for incorrect entities.
4546
*
46-
* @throws \Exception
47+
* @return void
48+
* @throws Exception
4749
*/
48-
public function testEntityException()
50+
public function testEntityException(): void
4951
{
5052
$actionObject = new ActionObject('fakeAction', 'comment', [
5153
'userInput' => '{{someEntity.entity}}'
5254
]);
5355

5456
$testObject = new TestObject("sampleTest", ["merge123" => $actionObject], [], [], "filename");
55-
56-
AspectMock::double(TestObjectHandler::class, ['initTestData' => '']);
57-
5857
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]);
59-
60-
AspectMock::double(TestGenerator::class, ['loadAllTestObjects' => ["sampleTest" => $testObject]]);
61-
6258
$testGeneratorObject->createAllTestFiles(null, []);
6359

6460
// assert that no exception for createAllTestFiles and generation error is stored in GenerationErrorHandler
@@ -69,11 +65,12 @@ public function testEntityException()
6965
}
7066

7167
/**
72-
* Tests that skipped tests do not have a fully generated body
68+
* Tests that skipped tests do not have a fully generated body.
7369
*
74-
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
70+
* @return void
71+
* @throws TestReferenceException
7572
*/
76-
public function testSkippedNoGeneration()
73+
public function testSkippedNoGeneration(): void
7774
{
7875
$actionInput = 'fakeInput';
7976
$actionObject = new ActionObject('fakeAction', 'comment', [
@@ -91,14 +88,22 @@ public function testSkippedNoGeneration()
9188
}
9289

9390
/**
94-
* Tests that skipped tests have a fully generated body when --allowSkipped is passed in
91+
* Tests that skipped tests have a fully generated body when --allowSkipped is passed in.
9592
*
96-
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
93+
* @return void
94+
* @throws TestReferenceException
9795
*/
98-
public function testAllowSkipped()
96+
public function testAllowSkipped(): void
9997
{
10098
// Mock allowSkipped for TestGenerator
101-
AspectMock::double(MftfApplicationConfig::class, ['allowSkipped' => true]);
99+
$mockConfig = $this->createMock(MftfApplicationConfig::class);
100+
$mockConfig->expects($this->any())
101+
->method('allowSkipped')
102+
->willReturn(true);
103+
104+
$property = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT');
105+
$property->setAccessible(true);
106+
$property->setValue($mockConfig);
102107

103108
$actionInput = 'fakeInput';
104109
$actionObject = new ActionObject('fakeAction', 'comment', [
@@ -128,17 +133,20 @@ public function testAllowSkipped()
128133
}
129134

130135
/**
131-
* Tests that TestGenerator createAllTestFiles correctly filters based on severity
136+
* Tests that TestGenerator createAllTestFiles correctly filters based on severity.
132137
*
133-
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
138+
* @return void
139+
* @throws TestReferenceException
134140
*/
135-
public function testFilter()
141+
public function testFilter(): void
136142
{
137-
// Mock filters for TestGenerator
138-
AspectMock::double(
139-
MftfApplicationConfig::class,
140-
['getFilterList' => new FilterList(['severity' => ["CRITICAL"]])]
141-
);
143+
$mockConfig = $this->createMock(MftfApplicationConfig::class);
144+
$fileList = new FilterList(['severity' => ["CRITICAL"]]);
145+
$mockConfig->expects($this->once())->method('getFilterList')->willReturn($fileList);
146+
147+
$property = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT');
148+
$property->setAccessible(true);
149+
$property->setValue($mockConfig);
142150

143151
$actionInput = 'fakeInput';
144152
$actionObject = new ActionObject('fakeAction', 'comment', [
@@ -161,16 +169,26 @@ public function testFilter()
161169
[],
162170
"filename"
163171
);
164-
AspectMock::double(TestGenerator::class, ['loadAllTestObjects' => ["sampleTest" => $test1, "test2" => $test2]]);
165172

166173
// Mock createCestFile to return name of tests that testGenerator tried to create
167174
$generatedTests = [];
168-
AspectMock::double(TestGenerator::class, ['createCestFile' => function ($arg1, $arg2) use (&$generatedTests) {
169-
$generatedTests[$arg2] = true;
170-
}]);
175+
$cestFileCreatorUtil = $this->createMock(CestFileCreatorUtil::class);
176+
$cestFileCreatorUtil->expects($this->once())
177+
->method('create')
178+
->will(
179+
$this->returnCallback(
180+
function ($filename) use (&$generatedTests) {
181+
$generatedTests[$filename] = true;
182+
}
183+
)
184+
);
185+
186+
$property = new ReflectionProperty(CestFileCreatorUtil::class, 'INSTANCE');
187+
$property->setAccessible(true);
188+
$property->setValue($cestFileCreatorUtil);
171189

172190
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $test1, "test2" => $test2]);
173-
$testGeneratorObject->createAllTestFiles(null, []);
191+
$testGeneratorObject->createAllTestFiles();
174192

175193
// Ensure Test1 was Generated but not Test 2
176194
$this->assertArrayHasKey('test1Cest', $generatedTests);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\FunctionalTestingFramework\Util\Filesystem;
9+
10+
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
11+
12+
class CestFileCreatorUtil
13+
{
14+
/**
15+
* Singleton CestFileCreatorUtil Instance.
16+
*
17+
* @var CestFileCreatorUtil
18+
*/
19+
private static $INSTANCE;
20+
21+
/**
22+
* CestFileCreatorUtil constructor.
23+
*/
24+
private function __construct(){}
25+
26+
/**
27+
* Get CestFileCreatorUtil instance.
28+
*
29+
* @return CestFileCreatorUtil
30+
*/
31+
public static function getInstance()
32+
{
33+
if (self::$INSTANCE === null) {
34+
return new self();
35+
}
36+
37+
return self::$INSTANCE;
38+
}
39+
40+
/**
41+
* Create a single PHP file containing the $cestPhp using the $filename.
42+
* If the _generated directory doesn't exist it will be created.
43+
*
44+
* @param string $exportDirectory
45+
* @param string $testPhp
46+
* @param string $filename
47+
*
48+
* @return void
49+
* @throws TestFrameworkException
50+
*/
51+
public function create(string $filename, string $exportDirectory, string $testPhp): void
52+
{
53+
DirSetupUtil::createGroupDir($exportDirectory);
54+
$exportFilePath = $exportDirectory . DIRECTORY_SEPARATOR . $filename . '.php';
55+
$file = fopen($exportFilePath, 'w');
56+
57+
if (!$file) {
58+
throw new TestFrameworkException(
59+
sprintf('Could not open test file: "%s"', $exportFilePath)
60+
);
61+
}
62+
63+
fwrite($file, $testPhp);
64+
fclose($file);
65+
}
66+
}

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\FunctionalTestingFramework\Test\Objects\TestHookObject;
2323
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
2424
use Magento\FunctionalTestingFramework\Test\Util\BaseObjectExtractor;
25+
use Magento\FunctionalTestingFramework\Util\Filesystem\CestFileCreatorUtil;
2526
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
2627
use Magento\FunctionalTestingFramework\Util\Manifest\BaseTestManifest;
2728
use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor;
@@ -207,21 +208,13 @@ private function loadAllTestObjects($testsToIgnore)
207208
*
208209
* @param string $testPhp
209210
* @param string $filename
211+
*
210212
* @return void
211213
* @throws TestFrameworkException
212214
*/
213215
private function createCestFile(string $testPhp, string $filename)
214216
{
215-
DirSetupUtil::createGroupDir($this->exportDirectory);
216-
$exportFilePath = $this->exportDirectory . DIRECTORY_SEPARATOR . $filename . ".php";
217-
$file = fopen($exportFilePath, 'w');
218-
219-
if (!$file) {
220-
throw new TestFrameworkException(sprintf('Could not open test file: "%s"', $exportFilePath));
221-
}
222-
223-
fwrite($file, $testPhp);
224-
fclose($file);
217+
CestFileCreatorUtil::getInstance()->create($filename, $this->exportDirectory, $testPhp);
225218
}
226219

227220
/**

0 commit comments

Comments
 (0)