diff --git a/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php b/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php index 8817d89c7..730493191 100644 --- a/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php +++ b/dev/tests/unit/Magento/FunctionalTestFramework/Util/TestGeneratorTest.php @@ -3,77 +3,76 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace tests\unit\Magento\FunctionalTestFramework\Util; -use AspectMock\Test as AspectMock; - +use Exception; +use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; +use Magento\FunctionalTestingFramework\Exceptions\TestReferenceException; use Magento\FunctionalTestingFramework\Filter\FilterList; -use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler; use Magento\FunctionalTestingFramework\Test\Objects\ActionObject; use Magento\FunctionalTestingFramework\Test\Objects\TestHookObject; use Magento\FunctionalTestingFramework\Test\Objects\TestObject; -use tests\unit\Util\MagentoTestCase; +use Magento\FunctionalTestingFramework\Util\Filesystem\CestFileCreatorUtil; +use Magento\FunctionalTestingFramework\Util\GenerationErrorHandler; use Magento\FunctionalTestingFramework\Util\TestGenerator; -use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig; +use ReflectionProperty; +use tests\unit\Util\MagentoTestCase; use tests\unit\Util\TestLoggingUtil; -use Magento\FunctionalTestingFramework\Util\GenerationErrorHandler; class TestGeneratorTest extends MagentoTestCase { /** - * Before method functionality + * Before method functionality. + * + * @return void */ - public function setUp(): void + protected function setUp(): void { TestLoggingUtil::getInstance()->setMockLoggingUtil(); } /** - * After method functionality + * After method functionality. * * @return void */ - public function tearDown(): void + protected function tearDown(): void { - AspectMock::clean(); GenerationErrorHandler::getInstance()->reset(); } /** * Basic test to check exceptions for incorrect entities. * - * @throws \Exception + * @return void + * @throws Exception */ - public function testEntityException() + public function testEntityException(): void { $actionObject = new ActionObject('fakeAction', 'comment', [ 'userInput' => '{{someEntity.entity}}' ]); - $testObject = new TestObject("sampleTest", ["merge123" => $actionObject], [], [], "filename"); - - AspectMock::double(TestObjectHandler::class, ['initTestData' => '']); - - $testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]); - - AspectMock::double(TestGenerator::class, ['loadAllTestObjects' => ["sampleTest" => $testObject]]); - + $testObject = new TestObject('sampleTest', ['merge123' => $actionObject], [], [], 'filename'); + $testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]); $testGeneratorObject->createAllTestFiles(null, []); // assert that no exception for createAllTestFiles and generation error is stored in GenerationErrorHandler - $errorMessage = '/' . preg_quote("Removed invalid test object sampleTest") . '/'; + $errorMessage = '/' . preg_quote('Removed invalid test object sampleTest') . '/'; TestLoggingUtil::getInstance()->validateMockLogStatmentRegex('error', $errorMessage, []); $testErrors = GenerationErrorHandler::getInstance()->getErrorsByType('test'); $this->assertArrayHasKey('sampleTest', $testErrors); } /** - * Tests that skipped tests do not have a fully generated body + * Tests that skipped tests do not have a fully generated body. * - * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException + * @return void + * @throws TestReferenceException */ - public function testSkippedNoGeneration() + public function testSkippedNoGeneration(): void { $actionInput = 'fakeInput'; $actionObject = new ActionObject('fakeAction', 'comment', [ @@ -81,9 +80,9 @@ public function testSkippedNoGeneration() ]); $annotations = ['skip' => ['issue']]; - $testObject = new TestObject("sampleTest", ["merge123" => $actionObject], $annotations, [], "filename"); + $testObject = new TestObject('sampleTest', ['merge123' => $actionObject], $annotations, [], 'filename'); - $testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]); + $testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]); $output = $testGeneratorObject->assembleTestPhp($testObject); $this->assertStringContainsString('This test is skipped', $output); @@ -91,14 +90,22 @@ public function testSkippedNoGeneration() } /** - * Tests that skipped tests have a fully generated body when --allowSkipped is passed in + * Tests that skipped tests have a fully generated body when --allowSkipped is passed in. * - * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException + * @return void + * @throws TestReferenceException */ - public function testAllowSkipped() + public function testAllowSkipped(): void { // Mock allowSkipped for TestGenerator - AspectMock::double(MftfApplicationConfig::class, ['allowSkipped' => true]); + $mockConfig = $this->createMock(MftfApplicationConfig::class); + $mockConfig + ->method('allowSkipped') + ->willReturn(true); + + $property = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT'); + $property->setAccessible(true); + $property->setValue($mockConfig); $actionInput = 'fakeInput'; $actionObject = new ActionObject('fakeAction', 'comment', [ @@ -110,16 +117,16 @@ public function testAllowSkipped() ]); $annotations = ['skip' => ['issue']]; - $beforeHook = new TestHookObject("before", "sampleTest", ['beforeAction' => $beforeActionObject]); + $beforeHook = new TestHookObject('before', 'sampleTest', ['beforeAction' => $beforeActionObject]); $testObject = new TestObject( - "sampleTest", - ["fakeAction" => $actionObject], + 'sampleTest', + ['fakeAction' => $actionObject], $annotations, - ["before" => $beforeHook], - "filename" + ['before' => $beforeHook], + 'filename' ); - $testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $testObject]); + $testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $testObject]); $output = $testGeneratorObject->assembleTestPhp($testObject); $this->assertStringNotContainsString('This test is skipped', $output); @@ -128,17 +135,22 @@ public function testAllowSkipped() } /** - * Tests that TestGenerator createAllTestFiles correctly filters based on severity + * Tests that TestGenerator createAllTestFiles correctly filters based on severity. * - * @throws \Magento\FunctionalTestingFramework\Exceptions\TestReferenceException + * @return void + * @throws TestReferenceException */ - public function testFilter() + public function testFilter(): void { - // Mock filters for TestGenerator - AspectMock::double( - MftfApplicationConfig::class, - ['getFilterList' => new FilterList(['severity' => ["CRITICAL"]])] - ); + $mockConfig = $this->createMock(MftfApplicationConfig::class); + $fileList = new FilterList(['severity' => ['CRITICAL']]); + $mockConfig + ->method('getFilterList') + ->willReturn($fileList); + + $property = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT'); + $property->setAccessible(true); + $property->setValue($mockConfig); $actionInput = 'fakeInput'; $actionObject = new ActionObject('fakeAction', 'comment', [ @@ -148,32 +160,58 @@ public function testFilter() $annotation1 = ['severity' => ['CRITICAL']]; $annotation2 = ['severity' => ['MINOR']]; $test1 = new TestObject( - "test1", - ["fakeAction" => $actionObject], + 'test1', + ['fakeAction' => $actionObject], $annotation1, [], - "filename" + 'filename' ); $test2 = new TestObject( - "test2", - ["fakeAction" => $actionObject], + 'test2', + ['fakeAction' => $actionObject], $annotation2, [], - "filename" + 'filename' ); - AspectMock::double(TestGenerator::class, ['loadAllTestObjects' => ["sampleTest" => $test1, "test2" => $test2]]); // Mock createCestFile to return name of tests that testGenerator tried to create $generatedTests = []; - AspectMock::double(TestGenerator::class, ['createCestFile' => function ($arg1, $arg2) use (&$generatedTests) { - $generatedTests[$arg2] = true; - }]); - - $testGeneratorObject = TestGenerator::getInstance("", ["sampleTest" => $test1, "test2" => $test2]); - $testGeneratorObject->createAllTestFiles(null, []); + $cestFileCreatorUtil = $this->createMock(CestFileCreatorUtil::class); + $cestFileCreatorUtil + ->method('create') + ->will( + $this->returnCallback( + function ($filename) use (&$generatedTests) { + $generatedTests[$filename] = true; + } + ) + ); + + $property = new ReflectionProperty(CestFileCreatorUtil::class, 'INSTANCE'); + $property->setAccessible(true); + $property->setValue($cestFileCreatorUtil); + + $testGeneratorObject = TestGenerator::getInstance('', ['sampleTest' => $test1, 'test2' => $test2]); + $testGeneratorObject->createAllTestFiles(); // Ensure Test1 was Generated but not Test 2 $this->assertArrayHasKey('test1Cest', $generatedTests); $this->assertArrayNotHasKey('test2Cest', $generatedTests); } + + /** + * @inheritDoc + */ + public static function tearDownAfterClass(): void + { + parent::tearDownAfterClass(); + + $cestFileCreatorUtilInstance = new ReflectionProperty(CestFileCreatorUtil::class, 'INSTANCE'); + $cestFileCreatorUtilInstance->setAccessible(true); + $cestFileCreatorUtilInstance->setValue(null); + + $mftfAppConfigInstance = new ReflectionProperty(MftfApplicationConfig::class, 'MFTF_APPLICATION_CONTEXT'); + $mftfAppConfigInstance->setAccessible(true); + $mftfAppConfigInstance->setValue(null); + } } diff --git a/src/Magento/FunctionalTestingFramework/Util/Filesystem/CestFileCreatorUtil.php b/src/Magento/FunctionalTestingFramework/Util/Filesystem/CestFileCreatorUtil.php new file mode 100644 index 000000000..9fe205151 --- /dev/null +++ b/src/Magento/FunctionalTestingFramework/Util/Filesystem/CestFileCreatorUtil.php @@ -0,0 +1,68 @@ +exportDirectory); - $exportFilePath = $this->exportDirectory . DIRECTORY_SEPARATOR . $filename . ".php"; - $file = fopen($exportFilePath, 'w'); - - if (!$file) { - throw new TestFrameworkException(sprintf('Could not open test file: "%s"', $exportFilePath)); - } - - fwrite($file, $testPhp); - fclose($file); + CestFileCreatorUtil::getInstance()->create($filename, $this->exportDirectory, $testPhp); } /**