Skip to content

Commit 2da5129

Browse files
committed
MQE-425:Create test to validate changes between commits with php unit
- add verification and unit test structure - add verification and unit tests - add test utils - add bootstrap setup file and phpunit.xml configuration - change object handler classes to trigger notices for missing xml
1 parent 9c1f44e commit 2da5129

File tree

18 files changed

+697
-17
lines changed

18 files changed

+697
-17
lines changed

Diff for: dev/tests/_bootstrap.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
error_reporting(~E_USER_NOTICE);
8+
define('PROJECT_ROOT', dirname(dirname(__DIR__)));
9+
require_once PROJECT_ROOT . '/vendor/autoload.php';
10+
11+
// Load needed framework env params
12+
$TEST_ENVS = [
13+
'MAGENTO_BASE_URL' => 'http://baseurl:8080',
14+
'MAGENTO_BACKEND_NAME' => 'admin',
15+
'MAGENTO_ADMIN_USERNAME' => 'admin',
16+
'MAGENTO_ADMIN_PASSWORD' => 'admin123'
17+
];
18+
19+
foreach ($TEST_ENVS as $key => $value) {
20+
$_ENV[$key] = $value;
21+
}
22+
23+
// Add our test module to the whitelist
24+
putenv('MODULE_WHITELIST=Magento_TestModule');
25+
26+
27+
// Define our own set of paths for the tests
28+
defined('FW_BP') || define('FW_BP', PROJECT_ROOT);
29+
30+
$RELATIVE_TESTS_MODULE_PATH = DIRECTORY_SEPARATOR . 'verification';
31+
32+
defined('TESTS_BP') || define('TESTS_BP', __DIR__);
33+
defined('TESTS_MODULE_PATH') || define('TESTS_MODULE_PATH', TESTS_BP . $RELATIVE_TESTS_MODULE_PATH);
34+
35+
$utilDir = DIRECTORY_SEPARATOR . 'Util'. DIRECTORY_SEPARATOR . '*.php';
36+
37+
//Load required util files from functional dir
38+
$functionalUtilFiles = glob(TESTS_BP . DIRECTORY_SEPARATOR . 'verification' . $utilDir);
39+
foreach (sortInterfaces($functionalUtilFiles) as $functionalUtilFile) {
40+
require($functionalUtilFile);
41+
}
42+
43+
//Load required util files from unit dir
44+
$unitUtilFiles = glob(TESTS_BP . DIRECTORY_SEPARATOR . 'unit' . $utilDir);
45+
foreach (sortInterfaces($unitUtilFiles) as $unitUtilFile) {
46+
require($unitUtilFile);
47+
}
48+
49+
function sortInterfaces($files)
50+
{
51+
$bottom = [];
52+
$top = [];
53+
foreach ($files as $file) {
54+
if (strstr(strtolower($file), 'interface')) {
55+
$top[] = $file;
56+
continue;
57+
}
58+
59+
$bottom[] = $file;
60+
}
61+
62+
return array_merge($top, $bottom);
63+
}

Diff for: dev/tests/phpunit.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
4+
convertNoticesToExceptions="false"
5+
bootstrap="_bootstrap.php">
6+
<testsuites>
7+
<testsuite name="verification">
8+
<directory>verification</directory>
9+
</testsuite>
10+
<testsuite name="unit">
11+
<directory>unit</directory>
12+
</testsuite>
13+
</testsuites>
14+
<filter>
15+
<whitelist processUncoveredFilesFromWhitelist="false">
16+
<directory suffix=".php">../../src/Magento/FunctionalTestingFramework/DataGenerator</directory>
17+
<directory suffix=".php">../../src/Magento/FunctionalTestingFramework/Page</directory>
18+
<directory suffix=".php">../../src/Magento/FunctionalTestingFramework/Suite</directory>
19+
<directory suffix=".php">../../src/Magento/FunctionalTestingFramework/Test</directory>
20+
<directory suffix=".php">../../src/Magento/FunctionalTestingFramework/Util</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace tests\unit\Magento\FunctionalTestFramework\Test\Util;
7+
8+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
9+
use Magento\FunctionalTestingFramework\DataGenerator\Objects\EntityDataObject;
10+
use Magento\FunctionalTestingFramework\Test\Objects\ActionObject;
11+
use Magento\FunctionalTestingFramework\Test\Objects\TestObject;
12+
use Magento\FunctionalTestingFramework\Test\Util\ActionMergeUtil;
13+
use Magento\FunctionalTestingFramework\Test\Util\ActionObjectExtractor;
14+
use PHPUnit\Framework\TestCase;
15+
use tests\unit\Util\DataObjectHandlerReflectionUtil;
16+
17+
class ActionMergeUtilTest extends TestCase
18+
{
19+
/**
20+
* Static ActionMergeUtil for all tests.
21+
*
22+
* @var ActionMergeUtil
23+
*/
24+
private static $MERGE_UTIL;
25+
26+
/**
27+
* Set up method for ActionMergeUtil tests.
28+
*
29+
* @return void
30+
*/
31+
public static function setUpBeforeClass()
32+
{
33+
self::$MERGE_UTIL = new ActionMergeUtil();
34+
}
35+
36+
/**
37+
* Test to validate actions are properly ordered during a merge.
38+
*
39+
* @return void
40+
*/
41+
public function testResolveActionStepOrdering()
42+
{
43+
$actions = [];
44+
$actionsLength = 11;
45+
46+
$testObjNamePosBeforeFirst = 'testBeforeBeforeMerge';
47+
$testObjNamePosFirst = 'testBeforeMerge0';
48+
$testObjNamePosEnd = 'testAfterMerge10';
49+
$testObjNamePosAfterEnd = 'testAfterAfterMerge10';
50+
51+
for ($i = 1; $i < $actionsLength; $i++) {
52+
$mergeKey = 'mergeKey'. $i;
53+
$type = 'testType';
54+
$actionAttributes = [];
55+
56+
$actions[] = new ActionObject($mergeKey, $type, $actionAttributes);
57+
}
58+
59+
$actions[] = new ActionObject(
60+
$testObjNamePosAfterEnd,
61+
'mergeType',
62+
[],
63+
$testObjNamePosEnd,
64+
ActionObject::MERGE_ACTION_ORDER_AFTER
65+
);
66+
$actions[] = new ActionObject(
67+
$testObjNamePosBeforeFirst,
68+
'mergeType',
69+
[],
70+
$testObjNamePosFirst,
71+
ActionObjectExtractor::TEST_ACTION_BEFORE
72+
);
73+
$actions[] = new ActionObject(
74+
$testObjNamePosFirst,
75+
'mergeType',
76+
[],
77+
'mergeKey1',
78+
ActionObjectExtractor::TEST_ACTION_BEFORE
79+
);
80+
$actions[] = new ActionObject(
81+
$testObjNamePosEnd,
82+
'mergeType',
83+
[],
84+
'mergeKey' . (string)($actionsLength - 1),
85+
ActionObject::MERGE_ACTION_ORDER_AFTER
86+
);
87+
88+
$orderedActions = self::$MERGE_UTIL->resolveActionSteps($actions);
89+
$orderedActionKeys = array_keys($orderedActions);
90+
91+
$this->assertEquals($testObjNamePosBeforeFirst, $orderedActionKeys[0]);
92+
$this->assertEquals($testObjNamePosFirst, $orderedActionKeys[1]);
93+
$this->assertEquals($testObjNamePosEnd, $orderedActionKeys[$actionsLength + 1]);
94+
$this->assertEquals($testObjNamePosAfterEnd, $orderedActionKeys[$actionsLength + 2]);
95+
}
96+
97+
/**
98+
* Test to validate action steps properly resolve section element references.
99+
*
100+
* @return void
101+
*/
102+
public function testResolveActionStepSectionData()
103+
{
104+
//TODO implement section object mocker and test
105+
}
106+
107+
/**
108+
* Test to validate action steps properly resolve page references.
109+
*
110+
* @return void
111+
*/
112+
public function resolveActionStepPageData()
113+
{
114+
//TODO implement page object mocker and test
115+
}
116+
117+
/**
118+
* Test to validate action steps properly resolve entity data references.
119+
*
120+
* @return void
121+
*/
122+
public function testResolveActionStepEntityData()
123+
{
124+
$dataObjectName = 'myObject';
125+
$dataObjectType = 'testObject';
126+
$dataFieldName = 'myfield';
127+
$dataFieldValue = 'myValue';
128+
$userInputKey = "userInput";
129+
$userinputValue = "{{" . "${dataObjectName}.${dataFieldName}}}";
130+
$actionName = "myAction";
131+
$actionType = "myCustomType";
132+
133+
134+
// Set up mock data object
135+
$mockData = [$dataFieldName => $dataFieldValue];
136+
$mockDataObject = new EntityDataObject($dataObjectName, $dataObjectType, $mockData, null, null, null);
137+
138+
// Set up mock DataObject Handler
139+
$mockDataHandler = $this->createMock(DataObjectHandler::class);
140+
$mockDataHandler->expects($this->any())
141+
->method('getObject')
142+
->with($this->matches($dataObjectName))
143+
->willReturn($mockDataObject);
144+
DataObjectHandlerReflectionUtil::setupMock($mockDataHandler);
145+
146+
// Create test object and action object
147+
$actionAttributes = [$userInputKey => $userinputValue];
148+
$actions[$actionName] = new ActionObject($actionName, $actionType, $actionAttributes);
149+
150+
$this->assertEquals($userinputValue, $actions[$actionName]->getCustomActionAttributes()[$userInputKey]);
151+
152+
$mergeUtil = new ActionMergeUtil();
153+
$resolvedActions = $mergeUtil->resolveActionSteps($actions);
154+
155+
$this->assertEquals($dataFieldValue, $resolvedActions[$actionName]->getCustomActionAttributes()[$userInputKey]);
156+
157+
DataObjectHandlerReflectionUtil::tearDown();
158+
}
159+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace tests\unit\Util;
7+
8+
use Magento\FunctionalTestingFramework\DataGenerator\Handlers\DataObjectHandler;
9+
10+
class DataObjectHandlerReflectionUtil implements ObjectHandlerReflectionUtilInterface
11+
{
12+
/**
13+
* DataObjectHandlerReflectionUtil constructor.
14+
*/
15+
private function __construct()
16+
{
17+
//private constructor
18+
}
19+
20+
/**
21+
* Sets the Data Object Handler singleton instance as the mocked object.
22+
*
23+
* @param PHPUnit_Framework_MockObject_MockObject $mockObject
24+
*/
25+
public static function setupMock($mockObject)
26+
{
27+
$setStatic = function ($val) {
28+
static::$DATA_OBJECT_HANDLER = $val;
29+
};
30+
31+
$setMockStatic = $setStatic->bindTo(null, DataObjectHandler::class);
32+
$setMockStatic($mockObject);
33+
}
34+
35+
/**
36+
* Sets the Data Object Handler Instance to a null value for re-initialization.
37+
*/
38+
public static function tearDown()
39+
{
40+
$resetStatic = function () {
41+
static::$DATA_OBJECT_HANDLER = null;
42+
};
43+
44+
$resetMockStatic = $resetStatic->bindTo(null, DataObjectHandler::class);
45+
$resetMockStatic();
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace tests\unit\Util;
7+
8+
interface ObjectHandlerReflectionUtilInterface
9+
{
10+
/**
11+
* Sets an object handler singleton instance equal to the mock handler passed in.
12+
*
13+
* @param PHPUnit_Framework_MockObject_MockObject $mockObject
14+
* @return void
15+
*/
16+
public static function setupMock($mockObject);
17+
18+
/**
19+
* Restores the singleton object handler instance to a null state for re-initialization.
20+
*
21+
* @return void
22+
*/
23+
public static function tearDown();
24+
}

0 commit comments

Comments
 (0)