Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MFTF-33305: Eliminate AspectMock from ObjectExtensionUtilTest #843

Merged
merged 4 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,40 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;

use AspectMock\Proxy\Verifier;
use AspectMock\Test as AspectMock;
use Magento\FunctionalTestingFramework\ObjectManager\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use Exception;
use Magento\FunctionalTestingFramework\ObjectManager;
use Magento\FunctionalTestingFramework\Test\Handlers\ActionGroupObjectHandler;
use Magento\FunctionalTestingFramework\Test\Handlers\TestObjectHandler;
use Magento\FunctionalTestingFramework\Test\Parsers\ActionGroupDataParser;
use Magento\FunctionalTestingFramework\Test\Parsers\TestDataParser;
use Magento\FunctionalTestingFramework\Util\Logger\LoggingUtil;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;
use tests\unit\Util\MockModuleResolverBuilder;
use tests\unit\Util\TestDataArrayBuilder;
use tests\unit\Util\TestLoggingUtil;
use tests\unit\Util\MockModuleResolverBuilder;

class ObjectExtensionUtilTest extends TestCase
{
/**
* Before test functionality
* Before test functionality.
*
* @return void
* @throws Exception
*/
public function setUp(): void
protected function setUp(): void
{
TestLoggingUtil::getInstance()->setMockLoggingUtil();
$resolverMock = new MockModuleResolverBuilder();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, @jilu1!

Could I remove this part of the code?

Thank you.

$resolverMock->setup();
}

/**
* After class functionality
* After class functionality.
*
* @return void
*/
public static function tearDownAfterClass(): void
Expand All @@ -45,10 +45,12 @@ public static function tearDownAfterClass(): void
}

/**
* Tests generating a test that extends another test
* @throws \Exception
* Tests generating a test that extends another test.
*
* @return void
* @throws Exception
*/
public function testGenerateExtendedTest()
public function testGenerateExtendedTest(): void
{
$mockActions = [
"mockStep" => ["nodeName" => "mockNode", "stepKey" => "mockStep"]
Expand Down Expand Up @@ -86,10 +88,12 @@ public function testGenerateExtendedTest()
}

/**
* Tests generating a test that extends another test
* @throws \Exception
* Tests generating a test that extends another test.
*
* @return void
* @throws Exception
*/
public function testGenerateExtendedWithHooks()
public function testGenerateExtendedWithHooks(): void
{
$mockBeforeHooks = [
"beforeHookAction" => ["nodeName" => "mockNodeBefore", "stepKey" => "mockStepBefore"]
Expand Down Expand Up @@ -132,10 +136,12 @@ public function testGenerateExtendedWithHooks()
}

/**
* Tests generating a test that extends another test
* @throws \Exception
* Tests generating a test that extends another test.
*
* @return void
* @throws Exception
*/
public function testExtendedTestNoParent()
public function testExtendedTestNoParent(): void
{
$testDataArrayBuilder = new TestDataArrayBuilder();
$mockExtendedTest = $testDataArrayBuilder
Expand All @@ -158,10 +164,12 @@ public function testExtendedTestNoParent()
}

/**
* Tests generating a test that extends another test
* @throws \Exception
* Tests generating a test that extends another test.
*
* @return void
* @throws Exception
*/
public function testExtendingExtendedTest()
public function testExtendingExtendedTest(): void
{
$testDataArrayBuilder = new TestDataArrayBuilder();
$mockParentTest = $testDataArrayBuilder
Expand Down Expand Up @@ -200,10 +208,12 @@ public function testExtendingExtendedTest()
}

/**
* Tests generating an action group that extends another action group
* @throws \Exception
* Tests generating an action group that extends another action group.
*
* @return void
* @throws Exception
*/
public function testGenerateExtendedActionGroup()
public function testGenerateExtendedActionGroup(): void
{
$mockSimpleActionGroup = [
"nodeName" => "actionGroup",
Expand Down Expand Up @@ -259,10 +269,12 @@ public function testGenerateExtendedActionGroup()
}

/**
* Tests generating an action group that extends an action group that does not exist
* @throws \Exception
* Tests generating an action group that extends an action group that does not exist.
*
* @return void
* @throws Exception
*/
public function testGenerateExtendedActionGroupNoParent()
public function testGenerateExtendedActionGroupNoParent(): void
{
$mockExtendedActionGroup = [
"nodeName" => "actionGroup",
Expand Down Expand Up @@ -292,10 +304,12 @@ public function testGenerateExtendedActionGroupNoParent()
}

/**
* Tests generating an action group that extends another action group that is already extended
* @throws \Exception
* Tests generating an action group that extends another action group that is already extended.
*
* @return void
* @throws Exception
*/
public function testExtendingExtendedActionGroup()
public function testExtendingExtendedActionGroup(): void
{
$mockParentActionGroup = [
"nodeName" => "actionGroup",
Expand Down Expand Up @@ -333,7 +347,7 @@ public function testExtendingExtendedActionGroup()
// parse and generate test object with mocked data
try {
ActionGroupObjectHandler::getInstance()->getObject('mockExtendedActionGroup');
} catch (\Exception $e) {
} catch (Exception $e) {
// validate log statement
TestLoggingUtil::getInstance()->validateMockLogStatement(
'error',
Expand All @@ -347,11 +361,12 @@ public function testExtendingExtendedActionGroup()
}

/**
* Tests generating a test that extends a skipped parent test
* Tests generating a test that extends a skipped parent test.
*
* @throws \Exception
* @return void
* @throws Exception
*/
public function testExtendedTestSkippedParent()
public function testExtendedTestSkippedParent(): void
{
$testDataArrayBuilder = new TestDataArrayBuilder();
$mockParentTest = $testDataArrayBuilder
Expand Down Expand Up @@ -384,43 +399,55 @@ public function testExtendedTestSkippedParent()
/**
* Function used to set mock for parser return and force init method to run between tests.
*
* @param array $testData
* @throws \Exception
* @param array|null $testData
* @param array|null $actionGroupData
*
* @return void
* @throws Exception
*/
private function setMockTestOutput($testData = null, $actionGroupData = null)
private function setMockTestOutput(array $testData = null, array $actionGroupData = null): void
{
// clear test object handler value to inject parsed content
$property = new \ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
$property = new ReflectionProperty(TestObjectHandler::class, 'testObjectHandler');
$property->setAccessible(true);
$property->setValue(null);

// clear test object handler value to inject parsed content
$property = new \ReflectionProperty(ActionGroupObjectHandler::class, 'instance');
$property = new ReflectionProperty(ActionGroupObjectHandler::class, 'instance');
$property->setAccessible(true);
$property->setValue(null);

$mockDataParser = AspectMock::double(TestDataParser::class, ['readTestData' => $testData])->make();
$mockActionGroupParser = AspectMock::double(
ActionGroupDataParser::class,
['readActionGroupData' => $actionGroupData]
)->make();
$instance = AspectMock::double(
ObjectManager::class,
[
'create' => function ($className) use (
$mockDataParser,
$mockActionGroupParser
) {
if ($className == TestDataParser::class) {
return $mockDataParser;
}
if ($className == ActionGroupDataParser::class) {
return $mockActionGroupParser;
$mockDataParser = $this->createMock(TestDataParser::class);
$mockDataParser
->method('readTestData')
->willReturn($testData);

$mockActionGroupParser = $this->createMock(ActionGroupDataParser::class);
$mockActionGroupParser
->method('readActionGroupData')
->willReturn($actionGroupData);

$instance = $this->createMock(ObjectManager::class);
$instance
->method('create')
->will(
$this->returnCallback(
function ($className) use ($mockDataParser, $mockActionGroupParser) {
if ($className === TestDataParser::class) {
return $mockDataParser;
}

if ($className === ActionGroupDataParser::class) {
return $mockActionGroupParser;
}

return null;
}
}
]
)->make();
// bypass the private constructor
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
)
);
// clear object manager value to inject expected instance
$property = new ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue($instance);
}
}
2 changes: 2 additions & 0 deletions dev/tests/unit/Util/MagentoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static function setUpBeforeClass(): void
if (!self::fileExists(DOCS_OUTPUT_DIR)) {
mkdir(DOCS_OUTPUT_DIR, 0755, true);
}
// Should be used to clean AspectMock mocking before using PHPUnit mocking and Reflection.
AspectMock::clean();
parent::setUpBeforeClass();
}

Expand Down
33 changes: 22 additions & 11 deletions dev/tests/unit/Util/MockModuleResolverBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,48 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace tests\unit\Util;

use AspectMock\Test as AspectMock;
use Exception;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\ObjectManager;
use Magento\FunctionalTestingFramework\ObjectManagerFactory;
use Magento\FunctionalTestingFramework\Util\ModuleResolver;
use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use ReflectionProperty;

class MockModuleResolverBuilder
{
/**
* Default paths for mock ModuleResolver
* Default paths for mock ModuleResolver.
*
* @var array
*/
private $defaultPaths = ['Magento_Module' => '/base/path/some/other/path/Magento/Module'];

/**
* Mock ModuleResolver builder
* Mock ModuleResolver builder.
*
* @param array|null $paths
*
* @param array $paths
* @return void
* @throws \Exception
* @throws Exception
*/
public function setup($paths = null)
public function setup(array $paths = null): void
{
if (empty($paths)) {
$paths = $this->defaultPaths;
}

$mockConfig = AspectMock::double(MftfApplicationConfig::class, ['forceGenerateEnabled' => false]);
$instance = AspectMock::double(ObjectManager::class, ['create' => $mockConfig->make(), 'get' => null])->make();
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
// clear object manager value to inject expected instance
$property = new ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue($instance);

$property = new \ReflectionProperty(ModuleResolver::class, 'instance');
$property = new ReflectionProperty(ModuleResolver::class, 'instance');
$property->setAccessible(true);
$property->setValue(null);

Expand All @@ -51,10 +58,14 @@ public function setup($paths = null)
);
$instance = AspectMock::double(ObjectManager::class, ['create' => $mockResolver->make(), 'get' => null])
->make();
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);

// clear object manager value to inject expected instance
$property = new ReflectionProperty(ObjectManager::class, 'instance');
$property->setAccessible(true);
$property->setValue($instance);

$resolver = ModuleResolver::getInstance();
$property = new \ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths');
$property = new ReflectionProperty(ModuleResolver::class, 'enabledModuleNameAndPaths');
$property->setAccessible(true);
$property->setValue($resolver, $paths);
}
Expand Down