Skip to content

General refactor: ext-curl dependency + review of singletones (refactor constructs) #266

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

Merged
merged 9 commits into from
Nov 19, 2018
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"require": {
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0|~7.2.0",
"ext-curl": "*",
"allure-framework/allure-codeception": "~1.2.6",
"codeception/codeception": "~2.3.4",
"consolidation/robo": "^1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function setMockTestAndSuiteParserOutput($testData, $suiteData)
$property->setValue(null);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function testGenerateEmptySuite()
*/
private function setMockTestAndSuiteParserOutput($testData, $suiteData)
{
$property = new \ReflectionProperty(SuiteGenerator::class, 'SUITE_GENERATOR_INSTANCE');
$property = new \ReflectionProperty(SuiteGenerator::class, 'instance');
$property->setAccessible(true);
$property->setValue(null);

Expand All @@ -159,7 +159,7 @@ private function setMockTestAndSuiteParserOutput($testData, $suiteData)
$property->setValue(null);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

use AspectMock\Proxy\Verifier;
Expand Down Expand Up @@ -31,26 +32,35 @@ public function setUp()
TestLoggingUtil::getInstance()->setMockLoggingUtil();
}

/**
* After class functionality
* @return void
*/
public static function tearDownAfterClass()
{
TestLoggingUtil::getInstance()->clearMockLoggingUtil();
}

/**
* Tests generating a test that extends another test
* @throws \Exception
*/
public function testGenerateExtendedTest()
{
$mockActions = [
"mockStep" => ["nodeName" => "mockNode", "stepKey" => "mockStep"]
"mockStep" => ["nodeName" => "mockNode", "stepKey" => "mockStep"]
];

$testDataArrayBuilder = new TestDataArrayBuilder();
$mockSimpleTest = $testDataArrayBuilder
->withName('simpleTest')
->withAnnotations(['title'=>[['value' => 'simpleTest']]])
->withAnnotations(['title' => [['value' => 'simpleTest']]])
->withTestActions($mockActions)
->build();

$mockExtendedTest = $testDataArrayBuilder
->withName('extendedTest')
->withAnnotations(['title'=>[['value' => 'extendedTest']]])
->withAnnotations(['title' => [['value' => 'extendedTest']]])
->withTestReference("simpleTest")
->build();

Expand Down Expand Up @@ -88,14 +98,14 @@ public function testGenerateExtendedWithHooks()
$testDataArrayBuilder = new TestDataArrayBuilder();
$mockSimpleTest = $testDataArrayBuilder
->withName('simpleTest')
->withAnnotations(['title'=>[['value' => 'simpleTest']]])
->withAnnotations(['title' => [['value' => 'simpleTest']]])
->withBeforeHook($mockBeforeHooks)
->withAfterHook($mockAfterHooks)
->build();

$mockExtendedTest = $testDataArrayBuilder
->withName('extendedTest')
->withAnnotations(['title'=>[['value' => 'extendedTest']]])
->withAnnotations(['title' => [['value' => 'extendedTest']]])
->withTestReference("simpleTest")
->build();

Expand All @@ -117,7 +127,7 @@ public function testGenerateExtendedWithHooks()
$this->assertArrayHasKey("mockStepBefore", $testObject->getHooks()['before']->getActions());
$this->assertArrayHasKey("mockStepAfter", $testObject->getHooks()['after']->getActions());
}

/**
* Tests generating a test that extends another test
* @throws \Exception
Expand Down Expand Up @@ -158,14 +168,14 @@ public function testExtendingExtendedTest()

$mockSimpleTest = $testDataArrayBuilder
->withName('simpleTest')
->withAnnotations(['title'=>[['value' => 'simpleTest']]])
->withAnnotations(['title' => [['value' => 'simpleTest']]])
->withTestActions()
->withTestReference("anotherTest")
->build();

$mockExtendedTest = $testDataArrayBuilder
->withName('extendedTest')
->withAnnotations(['title'=>[['value' => 'extendedTest']]])
->withAnnotations(['title' => [['value' => 'extendedTest']]])
->withTestReference("simpleTest")
->build();

Expand Down Expand Up @@ -347,7 +357,7 @@ private function setMockTestOutput($testData = null, $actionGroupData = null)
$property->setValue(null);

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

Expand All @@ -358,28 +368,21 @@ private function setMockTestOutput($testData = null, $actionGroupData = null)
)->make();
$instance = AspectMock::double(
ObjectManager::class,
['create' => function ($clazz) use (
$mockDataParser,
$mockActionGroupParser
) {
if ($clazz == TestDataParser::class) {
return $mockDataParser;
[
'create' => function ($className) use (
$mockDataParser,
$mockActionGroupParser
) {
if ($className == TestDataParser::class) {
return $mockDataParser;
}
if ($className == ActionGroupDataParser::class) {
return $mockActionGroupParser;
}
}
if ($clazz == ActionGroupDataParser::class) {
return $mockActionGroupParser;
}
}]
]
)->make();
// bypass the private constructor
AspectMock::double(ObjectManagerFactory::class, ['getObjectManager' => $instance]);
}

/**
* After class functionality
* @return void
*/
public static function tearDownAfterClass()
{
TestLoggingUtil::getInstance()->clearMockLoggingUtil();
}
}
10 changes: 5 additions & 5 deletions dev/tests/unit/Util/TestLoggingUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TestLoggingUtil extends Assert
/**
* @var TestLoggingUtil
*/
private static $INSTANCE;
private static $instance;

/**
* @var TestHandler
Expand All @@ -40,11 +40,11 @@ private function __construct()
*/
public static function getInstance()
{
if (self::$INSTANCE == null) {
self::$INSTANCE = new TestLoggingUtil();
if (self::$instance == null) {
self::$instance = new TestLoggingUtil();
}

return self::$INSTANCE;
return self::$instance;
}

/**
Expand All @@ -61,7 +61,7 @@ public function setMockLoggingUtil()
LoggingUtil::class,
['getLogger' => $testLogger]
)->make();
$property = new \ReflectionProperty(LoggingUtil::class, 'INSTANCE');
$property = new \ReflectionProperty(LoggingUtil::class, 'instance');
$property->setAccessible(true);
$property->setValue($mockLoggingUtil);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SuiteObjectHandler implements ObjectHandlerInterface
*
* @var SuiteObjectHandler
*/
private static $SUITE_OBJECT_HANLDER_INSTANCE;
private static $instance;

/**
* Array of suite objects keyed by suite name.
Expand All @@ -33,11 +33,19 @@ class SuiteObjectHandler implements ObjectHandlerInterface
private $suiteObjects;

/**
* SuiteObjectHandler constructor.
* Avoids instantiation of SuiteObjectHandler by new.
* @return void
*/
private function __construct()
{
// empty constructor
}

/**
* Avoids instantiation of SuiteObjectHandler by clone.
* @return void
*/
private function __clone()
{
}

/**
Expand All @@ -46,14 +54,14 @@ private function __construct()
* @return ObjectHandlerInterface
* @throws XmlException
*/
public static function getInstance()
public static function getInstance(): ObjectHandlerInterface
{
if (self::$SUITE_OBJECT_HANLDER_INSTANCE == null) {
self::$SUITE_OBJECT_HANLDER_INSTANCE = new SuiteObjectHandler();
self::$SUITE_OBJECT_HANLDER_INSTANCE->initSuiteData();
if (self::$instance == null) {
self::$instance = new SuiteObjectHandler();
self::$instance->initSuiteData();
}

return self::$SUITE_OBJECT_HANLDER_INSTANCE;
return self::$instance;
}

/**
Expand All @@ -62,7 +70,7 @@ public static function getInstance()
* @param string $objectName
* @return SuiteObject
*/
public function getObject($objectName)
public function getObject($objectName): SuiteObject
{
if (!array_key_exists($objectName, $this->suiteObjects)) {
trigger_error("Suite ${objectName} is not defined.", E_USER_ERROR);
Expand All @@ -75,7 +83,7 @@ public function getObject($objectName)
*
* @return array
*/
public function getAllObjects()
public function getAllObjects(): array
{
return $this->suiteObjects;
}
Expand All @@ -85,7 +93,7 @@ public function getAllObjects()
*
* @return array
*/
public function getAllTestReferences()
public function getAllTestReferences(): array
{
$testsReferencedInSuites = [];
$suites = $this->getAllObjects();
Expand Down
21 changes: 15 additions & 6 deletions src/Magento/FunctionalTestingFramework/Suite/SuiteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class SuiteGenerator
*
* @var SuiteGenerator
*/
private static $SUITE_GENERATOR_INSTANCE;
private static $instance;

/**
* Group Class Generator initialized in constructor.
Expand All @@ -43,28 +43,37 @@ class SuiteGenerator
private $groupClassGenerator;

/**
* SuiteGenerator constructor.
* Avoids instantiation of LoggingUtil by new.
* @return void
*/
private function __construct()
{
$this->groupClassGenerator = new GroupClassGenerator();
}

/**
* Avoids instantiation of SuiteGenerator by clone.
* @return void
*/
private function __clone()
{
}

/**
* Singleton method which is used to retrieve the instance of the suite generator.
*
* @return SuiteGenerator
*/
public static function getInstance()
public static function getInstance(): SuiteGenerator
{
if (!self::$SUITE_GENERATOR_INSTANCE) {
if (!self::$instance) {
// clear any previous configurations before any generation occurs.
self::clearPreviousGroupPreconditions();
self::clearPreviousSessionConfigEntries();
self::$SUITE_GENERATOR_INSTANCE = new SuiteGenerator();
self::$instance = new SuiteGenerator();
}

return self::$SUITE_GENERATOR_INSTANCE;
return self::$instance;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ActionGroupObjectHandler implements ObjectHandlerInterface
*
* @var ActionGroupObjectHandler
*/
private static $ACTION_GROUP_OBJECT_HANDLER;
private static $instance;

/**
* Array of action groups indexed by name
Expand All @@ -48,14 +48,13 @@ class ActionGroupObjectHandler implements ObjectHandlerInterface
*
* @return ActionGroupObjectHandler
*/
public static function getInstance()
public static function getInstance(): ActionGroupObjectHandler
{
if (!self::$ACTION_GROUP_OBJECT_HANDLER) {
self::$ACTION_GROUP_OBJECT_HANDLER = new ActionGroupObjectHandler();
self::$ACTION_GROUP_OBJECT_HANDLER->initActionGroups();
if (!self::$instance) {
self::$instance = new ActionGroupObjectHandler();
}

return self::$ACTION_GROUP_OBJECT_HANDLER;
return self::$instance;
}

/**
Expand All @@ -64,6 +63,7 @@ public static function getInstance()
private function __construct()
{
$this->extendUtil = new ObjectExtensionUtil();
$this->initActionGroups();
}

/**
Expand All @@ -87,7 +87,7 @@ public function getObject($actionGroupName)
*
* @return array
*/
public function getAllObjects()
public function getAllObjects(): array
{
foreach ($this->actionGroups as $actionGroupName => $actionGroup) {
$this->actionGroups[$actionGroupName] = $this->extendActionGroup($actionGroup);
Expand Down Expand Up @@ -125,7 +125,7 @@ private function initActionGroups()
* @param ActionGroupObject $actionGroupObject
* @return ActionGroupObject
*/
private function extendActionGroup($actionGroupObject)
private function extendActionGroup($actionGroupObject): ActionGroupObject
{
if ($actionGroupObject->getParentName() !== null) {
return $this->extendUtil->extendActionGroup($actionGroupObject);
Expand Down
Loading