Skip to content

MQE-809: Throw a warning or error when step key referencing in merges is invalid or ambiguous #95

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 3 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 6 additions & 2 deletions dev/tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
'includePaths' => [PROJECT_ROOT . '/src']
]);

// force php to generate
$GLOBALS['FORCE_PHP_GENERATE'] = true;
// set mftf appplication context
\Magento\FunctionalTestingFramework\Config\MftfApplicationConfig::create(
true,
\Magento\FunctionalTestingFramework\Config\MftfApplicationConfig::GENERATION_PHASE,
true
);

// Load needed framework env params
$TEST_ENVS = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;

use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor;
use PHPUnit\Framework\TestCase;

class ActionObjectExtractorTest extends TestCase
{
/** @var ActionObjectExtractor */
private $testActionObjectExtractor;

/**
* Setup method
*/
public function setUp()
{
$this->testActionObjectExtractor = new ActionObjectExtractor();
}

/**
* Tests basic action object extraction with a valid parser array.
*/
public function testBasicActionObjectExtration()
{
$actionObjects = $this->testActionObjectExtractor->extractActions($this->createBasicActionObjectArray());
$this->assertCount(1, $actionObjects);

/** @var ActionObject $firstElement */
$firstElement = array_values($actionObjects)[0];
$this->assertEquals('testAction1', $firstElement->getStepKey());
$this->assertCount(1, $firstElement->getCustomActionAttributes());
}

/**
* Tests an invalid merge order reference (i.e. a step referencing itself).
*/
public function testInvalidMergeOrderReference()
{
$invalidArray = $this->createBasicActionObjectArray('invalidTestAction1', 'invalidTestAction1');

$this->expectException('\Magento\FunctionalTestingFramework\Exceptions\TestReferenceException');
$expectedExceptionMessage = "Invalid ordering configuration in test TestWithSelfReferencingStepKey with step" .
" key(s):\n\tinvalidTestAction1\n";
$this->expectExceptionMessage($expectedExceptionMessage);

$this->testActionObjectExtractor->extractActions($invalidArray, 'TestWithSelfReferencingStepKey');
}

/**
* Validates a warning is printed to the console when multiple actions reference the same actions for merging.
*/
public function testAmbiguousMergeOrderRefernece()
{
$ambiguousArray = $this->createBasicActionObjectArray('testAction1');
$ambiguousArray = array_merge(
$ambiguousArray,
$this->createBasicActionObjectArray('testAction2', 'testAction1')
);

$ambiguousArray = array_merge(
$ambiguousArray,
$this->createBasicActionObjectArray('testAction3', null, 'testAction1')
);

$outputString = "multiple actions referencing step key testAction1 in test AmbiguousRefTest:\n" .
"\ttestAction2\n" .
"\ttestAction3\n";

$this->expectOutputString($outputString);
$this->testActionObjectExtractor->extractActions($ambiguousArray, 'AmbiguousRefTest');
}

/**
* Utility function to return mock parser output for testing extraction into ActionObjects.
*
* @param string $stepKey
* @param string $before
* @param string $after
* @return array
*/
private function createBasicActionObjectArray($stepKey = 'testAction1', $before = null, $after = null)
{
$baseArray = [
$stepKey => [
"nodeName" => "sampleAction",
"stepKey" => $stepKey,
"someAttribute" => "someAttributeValue"
]
];

if ($before) {
$baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['before' => $before]);
}

if ($after) {
$baseArray[$stepKey] = array_merge($baseArray[$stepKey], ['after' => $after]);
}

return $baseArray;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Config;

use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;

class MftfApplicationConfig
{
const GENERATION_PHASE = "generation";
const EXECUTION_PHASE = "execution";
const MFTF_PHASES = [self::GENERATION_PHASE, self::EXECUTION_PHASE];

/**
* Determines whether the user has specified a force option for generation
*
* @var bool
*/
private $forceGenerate;

/**
* String which identifies the current phase of mftf execution
*
* @var string
*/
private $phase;

/**
* Determines whether the user would like to execute mftf in a verbose run.
*
* @var bool
*/
private $verboseEnabled;

/**
* MftfApplicationConfig Singelton Instance
*
* @var MftfApplicationConfig
*/
private static $MFTF_APPLICATION_CONTEXT;

/**
* MftfApplicationConfig constructor.
*
* @param bool $forceGenerate
* @param string $phase
* @param bool $verboseEnabled
* @throws TestFrameworkException
*/
private function __construct($forceGenerate = false, $phase = self::EXECUTION_PHASE, $verboseEnabled = null)
{
$this->forceGenerate = $forceGenerate;

if (!in_array($phase, self::MFTF_PHASES)) {
throw new TestFrameworkException("{$phase} is not an mftf phase");
}

$this->phase = $phase;
$this->verboseEnabled = $verboseEnabled;
}

/**
* Creates an instance of the configuration instance for reference once application has started. This function
* returns void and is only run once during the lifetime of the application.
*
* @param bool $forceGenerate
* @param string $phase
* @param bool $verboseEnabled
* @return void
*/
public static function create($forceGenerate, $phase, $verboseEnabled)
{
if (self::$MFTF_APPLICATION_CONTEXT == null) {
self::$MFTF_APPLICATION_CONTEXT = new MftfApplicationConfig($forceGenerate, $phase, $verboseEnabled);
}
}

/**
* This function returns an instance of the MftfApplicationConfig which is created once the application starts.
*
* @return MftfApplicationConfig
*/
public static function getConfig()
{
// TODO explicitly set this with AcceptanceTester or MagentoWebDriver
// during execution we cannot guarantee the use of the robofile so we return the default application config,
// we don't want to set the application context in case the user explicitly does so at a later time.
if (self::$MFTF_APPLICATION_CONTEXT == null) {
return new MftfApplicationConfig();
}

return self::$MFTF_APPLICATION_CONTEXT;
}

/**
* Returns a booelan indiciating whether or not the user has indicated a forced generation.
*
* @return bool
*/
public function forceGenerateEnabled()
{
return $this->forceGenerate;
}

/**
* Returns a boolean indicating whether the user has indicated a verbose run, which will cause all applicable
* text to print to the console.
*
* @return bool
*/
public function verboseEnabled()
{
return $this->verboseEnabled ?? getenv('MFTF_DEBUG');
}

/**
* Returns a string which indicates the phase of mftf execution.
*
* @return string
*/
public function getPhase()
{
return $this->phase;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\FunctionalTestingFramework\Test\Objects;

use Magento\FunctionalTestingFramework\Config\MftfApplicationConfig;
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
use Magento\FunctionalTestingFramework\Exceptions\XmlException;
Expand Down Expand Up @@ -264,7 +265,8 @@ public function trimAssertionAttributes()
$oldAttributes = array_intersect($actionAttributeKeys, ActionObject::OLD_ASSERTION_ATTRIBUTES);
if (!empty($oldAttributes)) {
// @codingStandardsIgnoreStart
if ($GLOBALS['GENERATE_TESTS'] ?? false == true) {
$appConfig = MftfApplicationConfig::getConfig();
if ($appConfig->getPhase() == MftfApplicationConfig::GENERATION_PHASE && $appConfig->verboseEnabled()) {
echo("WARNING: Use of one line Assertion actions will be deprecated in MFTF 3.0.0, please use nested syntax (Action: {$this->type} StepKey: {$this->stepKey})" . PHP_EOL);
}
// @codingStandardsIgnoreEnd
Expand Down
Loading