Skip to content

Commit 50b3b95

Browse files
authored
Merge pull request #6608 from magento-lynx/2.3-develop-phpunit9
[MC-40802] Merge 2.3-develop into 2.3-develop-phpunit9
2 parents 1e9f4d1 + 4a78354 commit 50b3b95

File tree

5 files changed

+368
-8
lines changed

5 files changed

+368
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Optional configuration file for dev/tests/utils/phpunitGroupConfig.php
2+
# List graphql phpunit tests that have to be isolated in their own groups, e.g. long running test, special environments, etc
3+
# One per line by class name
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Optional configuration file for dev/tests/utils/phpunitGroupConfig.php
2+
# List rest phpunit tests that have to be isolated in their own groups, e.g. long running test, special environments, etc
3+
# One per line by class name

dev/tests/integration/isolate.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Optional configuration file for dev/tests/utils/phpunitGroupConfig.php
2+
# List integration phpunit tests that have to be isolated in their own groups, e.g. long running test, special environments, etc
3+
# One per line by class name

dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/blacklist/common.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33
# Example:
44
# app/code/Magento/Catalog
55
# dev/tests/static/framework/bootstrap.php
6+
lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
67
lib/internal/Magento/Framework/Cache/Backend/Eaccelerator.php
78
lib/internal/Magento/Framework/Image/Adapter/ImageMagick.php
8-
lib/internal/Magento/Framework/Interception/Test/Unit/Code/Generator/InterceptorTest.php
9-
lib/internal/Magento/Framework/Interception/Test/Unit/Config/ConfigTest.php
109
dev/tests/integration/framework/deployTestModules.php
11-
dev/tests/integration/testsuite/Magento/Framework/Code/Generator/AutoloaderTest.php
12-
dev/tests/integration/testsuite/Magento/Framework/Communication/ConfigTest.php
13-
dev/tests/integration/testsuite/Magento/Framework/Filter/DirectiveProcessor/SimpleDirectiveTest.php
1410
dev/tests/integration/testsuite/Magento/Framework/Session/ConfigTest.php
1511
dev/tests/integration/testsuite/Magento/Framework/Session/SessionManagerTest.php
1612
dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/AbstractFiltersTest.php
17-
dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/CategoryTest.php
1813
dev/tests/api-functional/testsuite/Magento/Customer/Api/AddressRepositoryTest.php
1914
dev/tests/api-functional/testsuite/Magento/Framework/Model/Entity/HydratorTest.php
2015
dev/tests/api-functional/testsuite/Magento/Integration/Model/AdminTokenServiceTest.php
2116
dev/tests/api-functional/testsuite/Magento/Integration/Model/CustomerTokenServiceTest.php
22-
app/code/Magento/Developer/Test/Unit/Console/Command/DevTestsRunCommandTest.php
23-
app/code/Magento/OfflineShipping/Test/Unit/Model/ResourceModel/Carrier/Tablerate/CSV/ColumnResolverTest.php
17+
dev/tests/utils/phpunitGroupConfig.php
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
//phpcs:disable
8+
/**
9+
* Script to operate on a test suite defined in a phpunit configuration xml or xml.dist file; split the tests
10+
* in the suite into groups by required size; return total number of groups or generate phpunit_<index>.xml file
11+
* that defines a new test suite named group_<index> with tests in group <index>
12+
*
13+
* Common scenario:
14+
*
15+
* 1. Query how many groups in a test suite with a given size --group-size=<size>
16+
* php phpunitGroupConfig.php --get-total --configuration=<path-to-phpunit-xml-dist-file> --test-suite=<name> --group-size=<size> --isolate-tests=<path-to-isolate-tests-file>
17+
*
18+
* 2a. Generate the configuration file for group <index>. <index> must be in range of [1, total number of groups])
19+
* php phpunitGroupConfig.php --get-group=<index> --configuration=<path-to-phpunit-xml-dist-file> --test-suite=<name> --group-size=<size> --isolate-tests=<path-to-isolate-tests-file>
20+
*
21+
* 2b. Or generate configuration files for all test groups at once
22+
* php phpunitGroupConfig.php --get-group=all --configuration=<path-to-phpunit-xml-dist-file> --test-suite=<name> --group-size=<size> --isolate-tests=<path-to-isolate-tests-file>
23+
*
24+
* 3. PHPUnit command to run tests for group at <index>
25+
* phpunit --configuration <path_to_phpunit_<index>.xml> --testsuite group_<index>
26+
*/
27+
28+
$scriptName = basename(__FILE__);
29+
30+
define(
31+
'USAGE',
32+
<<<USAGE
33+
Usage:
34+
php -f $scriptName
35+
[--get-total]
36+
Option takes no value, when specified, script will return total number of groups for the test suite specified in --test-suite.
37+
It's the default if both --get-total and --get-group are specified or both --get-total and --get-group are not specified.
38+
[--get-group="<positive integer>|all"]
39+
When option takes a positive integer value <i>, script will generate phpunit_<i>.xml file in the same location as the config
40+
file specified in --configuration with a test suite named "group_<i>" which contains the i-th group of tests from the test
41+
suite specified in --test-suite.
42+
When option takes value "all", script will generate config files for all groups at once.
43+
--test-suite="<name>"
44+
Name of test suite to be splitted into groups.
45+
--group-size="<positive integer>"
46+
Number of tests per group.
47+
--configuration="<path>"
48+
Path to phpunit configuration xml or xml.dist file.
49+
[--isolate-tests="<path>"]
50+
Path to a text file containing tests that require group isolation. One test path per line.
51+
52+
Note:
53+
Script uses getopt() which does not accept " "(space) as a separator for optional values. Use "=" for [--get-group] and [--isolate-tests] instead.
54+
See https://www.php.net/manual/en/function.getopt.php
55+
56+
USAGE
57+
);
58+
//phpcs:enable
59+
60+
$options = getopt(
61+
'',
62+
[
63+
'get-total',
64+
'get-group::',
65+
'test-suite:',
66+
'group-size:',
67+
'configuration:',
68+
'isolate-tests::'
69+
]
70+
);
71+
$requiredOpts = ['test-suite', 'group-size', 'configuration'];
72+
73+
try {
74+
foreach ($requiredOpts as $opt) {
75+
assertUsage(empty($options[$opt]), "Option --$opt: cannot be empty\n");
76+
}
77+
78+
assertUsage(!ctype_digit($options['group-size']), "Option --group-size: must be positive integer\n");
79+
assertUsage(!realpath($options['configuration']), "Option --configuration: file doesn't exist\n");
80+
assertUsage(
81+
isset($options['isolate-tests']) && !realpath($options['isolate-tests']),
82+
"Option --isolate-tests: file doesn't exist\n"
83+
);
84+
$isolateTests = isset($options['isolate-tests']) ? readIsolateTests(realpath($options['isolate-tests'])) : [];
85+
86+
$generateConfig = null;
87+
$groupIndex = null;
88+
if (isset($options['get-total']) || !isset($options['get-group'])) {
89+
$generateConfig = false;
90+
} else {
91+
assertUsage(
92+
(empty($options['get-group']) || !ctype_digit($options['get-group']))
93+
&& strtolower($options['get-group']) != 'all',
94+
"Option --get-group: must be a positive integer or 'all'\n"
95+
);
96+
$generateConfig = true;
97+
$groupIndex = strtolower($options['get-group']);
98+
}
99+
100+
$testSuite = $options['test-suite'];
101+
$groupSize = $options['group-size'];
102+
$configFile = realpath($options['configuration']);
103+
$workingDir = dirname($configFile) . DIRECTORY_SEPARATOR;
104+
105+
$savedCwd = getcwd();
106+
chdir($workingDir);
107+
$allTests = getTestList($configFile, $testSuite);
108+
chdir($savedCwd);
109+
list($allRegularTests, $isolateTests) = fuzzyArrayDiff($allTests, $isolateTests); // diff to separate isolated tests
110+
111+
$totalRegularTests = count($allRegularTests);
112+
if (($totalRegularTests % $groupSize) === 0) {
113+
$totalRegularGroups = $totalRegularTests / $groupSize;
114+
} else {
115+
$totalRegularGroups = (int)($totalRegularTests / $groupSize) + 1;
116+
}
117+
$totalGroups = $totalRegularGroups + count($isolateTests);
118+
assertUsage(
119+
$totalGroups == 0,
120+
"Option --test-suite: no test found for test suite '{$testSuite}'\n"
121+
);
122+
123+
if (!$generateConfig) {
124+
// phpcs:ignore Magento2.Security.LanguageConstruct.DirectOutput
125+
echo $totalGroups;
126+
// phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
127+
exit(0);
128+
}
129+
130+
if ($groupIndex == 'all') {
131+
$sIndex = 1;
132+
$eIndex = $totalGroups;
133+
} else {
134+
assertUsage(
135+
(int)$groupIndex > $totalGroups,
136+
"Option --get-group: can not be greater than $totalGroups\n"
137+
);
138+
$sIndex = (int)$groupIndex;
139+
$eIndex = $sIndex;
140+
}
141+
142+
$successMsg = "PHPUnit configuration files created:\n";
143+
for ($index = $sIndex; $index < $eIndex + 1; $index++) {
144+
$groupTests = [];
145+
if ($index <= $totalRegularGroups) {
146+
$groupTests = array_chunk($allRegularTests, $groupSize)[$index - 1];
147+
} else {
148+
$groupTests[] = $isolateTests[$index - $totalRegularGroups - 1];
149+
}
150+
151+
$groupConfigFile = $workingDir . 'phpunit_' . $index . '.xml';
152+
createGroupConfig($configFile, $groupConfigFile, $groupTests, $index);
153+
$successMsg .= "{$groupConfigFile}, group: {$index}, test suite: group_{$index}\n";
154+
}
155+
// phpcs:ignore Magento2.Security.LanguageConstruct.DirectOutput
156+
echo $successMsg;
157+
} catch (Exception $e) {
158+
// phpcs:ignore Magento2.Security.LanguageConstruct.DirectOutput
159+
echo $e->getMessage();
160+
// phpcs:ignore Magento2.Security.LanguageConstruct.ExitUsage
161+
exit(1);
162+
}
163+
164+
/**
165+
* Generate a phpunit configuration file for a given group
166+
*
167+
* @param string $in
168+
* @param string $out
169+
* @param array $group
170+
* @param integer $index
171+
* @return void
172+
* @throws Exception
173+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
174+
*/
175+
function createGroupConfig($in, $out, $group, $index)
176+
{
177+
$beforeTestSuites = true;
178+
$afterTestSuites = false;
179+
$outLines = '';
180+
$inLines = explode("\n", file_get_contents($in));
181+
foreach ($inLines as $inLine) {
182+
if ($beforeTestSuites) {
183+
// Replacing existing <testsuites> node with new <testsuites> node
184+
preg_match('/<testsuites/', $inLine, $bMatch);
185+
if (isset($bMatch[0])) {
186+
$beforeTestSuites = false;
187+
$outLines .= getFormattedGroup($group, $index);
188+
continue;
189+
}
190+
}
191+
if (!$afterTestSuites) {
192+
preg_match('/<\/\s*testsuites/', $inLine, $aMatch);
193+
if (isset($aMatch[0])) {
194+
$afterTestSuites = true;
195+
continue;
196+
}
197+
}
198+
if ($beforeTestSuites) {
199+
// Adding new <testsuites> node right before </phpunit> if there is no existing <testsuites> node
200+
preg_match('/<\/\s*phpunit/', $inLine, $lMatch);
201+
if (isset($lMatch[0])) {
202+
$outLines .= getFormattedGroup($group, $index);
203+
$outLines .= $inLine . "\n";
204+
break;
205+
}
206+
}
207+
if ($beforeTestSuites || $afterTestSuites) {
208+
$outLines .= $inLine . "\n";
209+
}
210+
}
211+
file_put_contents($out, $outLines);
212+
}
213+
214+
/**
215+
* Format tests in an array into <testsuite> node defined by phpunit xml schema
216+
*
217+
* @param array $group
218+
* @param integer $index
219+
* @return string
220+
*/
221+
function getFormattedGroup($group, $index)
222+
{
223+
$output = "\t<testsuites>\n";
224+
$output .= "\t\t<testsuite name=\"group_{$index}\">\n";
225+
foreach ($group as $ch) {
226+
$output .= "\t\t\t<file>{$ch}</file>\n";
227+
}
228+
$output .= "\t\t</testsuite>\n";
229+
$output .= "\t</testsuites>\n";
230+
return $output;
231+
}
232+
233+
/**
234+
* Return paths for all tests as an array for a given test suite in a phpunit.xml(.dist) file
235+
*
236+
* @param string $configFile
237+
* @param string $suiteName
238+
* @return array
239+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
240+
* @SuppressWarnings(PHPMD.NPathComplexity)
241+
* phpcs:disable Generic.Metrics.NestingLevel
242+
*/
243+
function getTestList($configFile, $suiteName)
244+
{
245+
$testCases = [];
246+
$config = simplexml_load_file($configFile);
247+
foreach ($config->xpath('//testsuite') as $testsuite) {
248+
if (strtolower((string)$testsuite['name']) != strtolower($suiteName)) {
249+
continue;
250+
}
251+
foreach ($testsuite->file as $file) {
252+
$testCases[(string)$file] = true;
253+
}
254+
$excludeFiles = [];
255+
foreach ($testsuite->exclude as $excludeFile) {
256+
$excludeFiles[] = (string)$excludeFile;
257+
}
258+
foreach ($testsuite->directory as $directoryPattern) {
259+
foreach (glob($directoryPattern, GLOB_ONLYDIR) as $directory) {
260+
if (!file_exists((string)$directory)) {
261+
continue;
262+
}
263+
$suffix = isset($directory['suffix']) ? (string)$directory['suffix'] : 'Test.php';
264+
$fileIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator((string)$directory));
265+
foreach ($fileIterator as $fileInfo) {
266+
$pathToTestCase = (string)$fileInfo;
267+
if (substr_compare($pathToTestCase, $suffix, -strlen($suffix)) === 0
268+
&& !isTestClassAbstract($pathToTestCase)
269+
) {
270+
$inExclude = false;
271+
foreach ($excludeFiles as $excludeFile) {
272+
if (strpos($pathToTestCase, $excludeFile) !== false) {
273+
$inExclude = true;
274+
break;
275+
}
276+
}
277+
if (!$inExclude) {
278+
$testCases[$pathToTestCase] = true;
279+
}
280+
}
281+
}
282+
}
283+
}
284+
}
285+
$testCases = array_keys($testCases); // automatically avoid file duplications
286+
sort($testCases);
287+
return $testCases;
288+
}
289+
//phpcs:enable Generic.Metrics.NestingLevel
290+
291+
/**
292+
* Determine if a file contains an abstract class
293+
*
294+
* @param string $testClassPath
295+
* @return bool
296+
*/
297+
function isTestClassAbstract($testClassPath)
298+
{
299+
return strpos(file_get_contents($testClassPath), "\nabstract class") !== false;
300+
}
301+
302+
/**
303+
* Return isolation tests as an array by reading from a file
304+
*
305+
* @param string $file
306+
* @return array
307+
*/
308+
function readIsolateTests($file)
309+
{
310+
$tests = [];
311+
$lines = explode("\n", file_get_contents($file));
312+
foreach ($lines as $line) {
313+
if (!empty(trim($line)) && substr_compare(trim($line), '#', 0, 1) !== 0) {
314+
$tests[] = trim($line);
315+
}
316+
}
317+
return $tests;
318+
}
319+
320+
/**
321+
* Array diff based on partial match
322+
*
323+
* @param array $oArray
324+
* @param array $dArray
325+
* @return array
326+
*/
327+
function fuzzyArrayDiff($oArray, $dArray)
328+
{
329+
$ret1 = [];
330+
$ret2 = [];
331+
foreach ($oArray as $obj) {
332+
$ret1[] = $obj;
333+
foreach ($dArray as $diff) {
334+
if (stripos($obj, $diff) !== false) {
335+
$ret2[] = $obj;
336+
array_pop($ret1);
337+
break;
338+
}
339+
}
340+
}
341+
return [$ret1, $ret2];
342+
}
343+
344+
/**
345+
* Assert usage by throwing exception on condition evaluating to true
346+
*
347+
* @param bool $condition
348+
* @param string $error
349+
* @throws Exception
350+
*/
351+
function assertUsage($condition, $error)
352+
{
353+
if ($condition) {
354+
$error .= "\n" . USAGE;
355+
throw new Exception($error);
356+
}
357+
}

0 commit comments

Comments
 (0)