d8d9761ffe1a0c41bb9f787333be34983a32ab01
[civicrm-core.git] / Civi / CiUtil / Command / AntagonistCommand.php
1 <?php
2 namespace Civi\CiUtil\Command;
3
4 /**
5 * Class AntagonistCommand
6 *
7 * @package Civi\CiUtil\Command
8 */
9 class AntagonistCommand {
10
11 /**
12 * @param $argv
13 */
14 public static function main($argv) {
15 if (count($argv) != 3) {
16 print "usage: {$argv[0]} <TargetTest::testFunc> </path/to/suite>\n";
17 exit(1);
18 }
19 list ($program, $target, $suite) = $argv;
20
21 $candidateTests = \Civi\CiUtil\PHPUnitScanner::findTestsByPath([$suite]);
22 // $candidateTests = array(
23 // array('class' => 'CRM_Core_RegionTest', 'method' => 'testBlank'),
24 // array('class' => 'CRM_Core_RegionTest', 'method' => 'testDefault'),
25 // array('class' => 'CRM_Core_RegionTest', 'method' => 'testOverride'),
26 // array('class' => 'CRM_Core_RegionTest', 'method' => 'testAllTypes'),
27 // );
28 $antagonist = self::findAntagonist($target, $candidateTests);
29 if ($antagonist) {
30 print_r(['found an antagonist' => $antagonist]);
31 }
32 else {
33 print_r(['found no antagonists']);
34 }
35 }
36
37 /**
38 * @param string $target
39 * E.g. "MyTest::testFoo".
40 * @param array $candidateTests
41 * List of strings (e.g. "MyTest::testFoo").
42 * @return array|null
43 * array contains keys:
44 * - antagonist: array
45 * - file: string
46 * - class: string
47 * - method: string
48 * - expectedResults: array
49 * - actualResults: array
50 */
51 public static function findAntagonist($target, $candidateTests) {
52 //$phpUnit = new \Civi\CiUtil\EnvTestRunner('./scripts/phpunit', 'EnvTests');
53 $phpUnit = new \Civi\CiUtil\EnvTestRunner('phpunit', 'tests/phpunit/EnvTests.php');
54 $expectedResults = $phpUnit->run([$target]);
55 print_r(['$expectedResults' => $expectedResults]);
56
57 foreach ($candidateTests as $candidateTest) {
58 $candidateTestName = $candidateTest['class'] . '::' . $candidateTest['method'];
59 if ($candidateTestName == $target) {
60 continue;
61 }
62 $actualResults = $phpUnit->run([
63 $candidateTestName,
64 $target,
65 ]);
66 print_r(['$actualResults' => $actualResults]);
67 foreach ($expectedResults as $testName => $expectedResult) {
68 if ($actualResults[$testName] != $expectedResult) {
69 return [
70 'antagonist' => $candidateTest,
71 'expectedResults' => $expectedResults,
72 'actualResults' => $actualResults,
73 ];
74 }
75 }
76 }
77 return NULL;
78 }
79
80 }