Skip to content

Commit eda5101

Browse files
authored
Merge pull request #79 from magento-commerce/imported-magento-magento2-functional-testing-framework-850
[Imported] 33309: Eliminated AspectMock usage from TestGeneratorTest.php
2 parents 95a445b + c162a04 commit eda5101

File tree

3 files changed

+167
-68
lines changed

3 files changed

+167
-68
lines changed

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

Lines changed: 96 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,109 @@
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\Config\MftfApplicationConfig;
12+
use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException;
1113
use Magento\FunctionalTestingFramework\Filter\FilterList;
12-
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
1314
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
1415
use Magento\FunctionalTestingFramework\Test\Objects\TestHookObject;
1516
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;
1719
use Magento\FunctionalTestingFramework\Util\TestGenerator;
18-
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
20+
use ReflectionProperty;
21+
use tests\unit\Util\MagentoTestCase;
1922
use tests\unit\Util\TestLoggingUtil;
20-
use Magento\FunctionalTestingFramework\Util\GenerationErrorHandler;
2123

2224
class TestGeneratorTest extends MagentoTestCase
2325
{
2426
/**
25-
* Before method functionality
27+
* Before method functionality.
28+
*
29+
* @return void
2630
*/
27-
public function setUp(): void
31+
protected function setUp(): void
2832
{
2933
TestLoggingUtil::getInstance()->setMockLoggingUtil();
3034
}
3135

3236
/**
33-
* After method functionality
37+
* After method functionality.
3438
*
3539
* @return void
3640
*/
37-
public function tearDown(): void
41+
protected function tearDown(): void
3842
{
39-
AspectMock::clean();
4043
GenerationErrorHandler::getInstance()->reset();
4144
}
4245

4346
/**
4447
* Basic test to check exceptions for incorrect entities.
4548
*
46-
* @throws \Exception
49+
* @return void
50+
* @throws Exception
4751
*/
48-
public function testEntityException()
52+
public function testEntityException(): void
4953
{
5054
$actionObject = new ActionObject('fakeAction', 'comment', [
5155
'userInput' => '{{someEntity.entity}}'
5256
]);
5357

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]);
6260
$testGeneratorObject->createAllTestFiles(null, []);
6361

6462
// 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') . '/';
6664
TestLoggingUtil::getInstance()->validateMockLogStatmentRegex('error', $errorMessage, []);
6765
$testErrors = GenerationErrorHandler::getInstance()->getErrorsByType('test');
6866
$this->assertArrayHasKey('sampleTest', $testErrors);
6967
}
7068

7169
/**
72-
* Tests that skipped tests do not have a fully generated body
70+
* Tests that skipped tests do not have a fully generated body.
7371
*
74-
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
72+
* @return void
73+
* @throws TestReferenceException
7574
*/
76-
public function testSkippedNoGeneration()
75+
public function testSkippedNoGeneration(): void
7776
{
7877
$actionInput = 'fakeInput';
7978
$actionObject = new ActionObject('fakeAction', 'comment', [
8079
'userInput' => $actionInput
8180
]);
8281

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

86-
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]);
85+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]);
8786
$output = $testGeneratorObject->assembleTestPhp($testObject);
8887

8988
$this->assertStringContainsString('This test is skipped', $output);
9089
$this->assertStringNotContainsString($actionInput, $output);
9190
}
9291

9392
/**
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.
9594
*
96-
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
95+
* @return void
96+
* @throws TestReferenceException
9797
*/
98-
public function testAllowSkipped()
98+
public function testAllowSkipped(): void
9999
{
100100
// 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);
102109

103110
$actionInput = 'fakeInput';
104111
$actionObject = new ActionObject('fakeAction', 'comment', [
@@ -110,16 +117,16 @@ public function testAllowSkipped()
110117
]);
111118

112119
$annotations = ['skip' => ['issue']];
113-
$beforeHook = new TestHookObject("before", "sampleTest", ['beforeAction' => $beforeActionObject]);
120+
$beforeHook = new TestHookObject('before', 'sampleTest', ['beforeAction' => $beforeActionObject]);
114121
$testObject = new TestObject(
115-
"sampleTest",
116-
["fakeAction" => $actionObject],
122+
'sampleTest',
123+
['fakeAction' => $actionObject],
117124
$annotations,
118-
["before" => $beforeHook],
119-
"filename"
125+
['before' => $beforeHook],
126+
'filename'
120127
);
121128

122-
$testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]);
129+
$testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]);
123130
$output = $testGeneratorObject->assembleTestPhp($testObject);
124131

125132
$this->assertStringNotContainsString('This test is skipped', $output);
@@ -128,17 +135,22 @@ public function testAllowSkipped()
128135
}
129136

130137
/**
131-
* Tests that TestGenerator createAllTestFiles correctly filters based on severity
138+
* Tests that TestGenerator createAllTestFiles correctly filters based on severity.
132139
*
133-
* @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException
140+
* @return void
141+
* @throws TestReferenceException
134142
*/
135-
public function testFilter()
143+
public function testFilter(): void
136144
{
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);
142154

143155
$actionInput = 'fakeInput';
144156
$actionObject = new ActionObject('fakeAction', 'comment', [
@@ -148,32 +160,58 @@ public function testFilter()
148160
$annotation1 = ['severity' => ['CRITICAL']];
149161
$annotation2 = ['severity' => ['MINOR']];
150162
$test1 = new TestObject(
151-
"test1",
152-
["fakeAction" => $actionObject],
163+
'test1',
164+
['fakeAction' => $actionObject],
153165
$annotation1,
154166
[],
155-
"filename"
167+
'filename'
156168
);
157169
$test2 = new TestObject(
158-
"test2",
159-
["fakeAction" => $actionObject],
170+
'test2',
171+
['fakeAction' => $actionObject],
160172
$annotation2,
161173
[],
162-
"filename"
174+
'filename'
163175
);
164-
AspectMock::double(TestGenerator::class, ['loadAllTestObjects' => ["sampleTest" => $test1, "test2" => $test2]]);
165176

166177
// Mock createCestFile to return name of tests that testGenerator tried to create
167178
$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();
174196

175197
// Ensure Test1 was Generated but not Test 2
176198
$this->assertArrayHasKey('test1Cest', $generatedTests);
177199
$this->assertArrayNotHasKey('test2Cest', $generatedTests);
178200
}
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+
}
179217
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
28+
/**
29+
* Get CestFileCreatorUtil instance.
30+
*
31+
* @return CestFileCreatorUtil
32+
*/
33+
public static function getInstance(): CestFileCreatorUtil
34+
{
35+
if (!self::$INSTANCE) {
36+
self::$INSTANCE = new CestFileCreatorUtil();
37+
}
38+
39+
return self::$INSTANCE;
40+
}
41+
42+
/**
43+
* Create a single PHP file containing the $cestPhp using the $filename.
44+
* If the _generated directory doesn't exist it will be created.
45+
*
46+
* @param string $filename
47+
* @param string $exportDirectory
48+
* @param string $testPhp
49+
*
50+
* @return void
51+
* @throws TestFrameworkException
52+
*/
53+
public function create(string $filename, string $exportDirectory, string $testPhp): void
54+
{
55+
DirSetupUtil::createGroupDir($exportDirectory);
56+
$exportFilePath = $exportDirectory . DIRECTORY_SEPARATOR . $filename . '.php';
57+
$file = fopen($exportFilePath, 'w');
58+
59+
if (!$file) {
60+
throw new TestFrameworkException(
61+
sprintf('Could not open test file: "%s"', $exportFilePath)
62+
);
63+
}
64+
65+
fwrite($file, $testPhp);
66+
fclose($file);
67+
}
68+
}

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)