phpunit-compare - Add support for CSV output
[civicrm-core.git] / Civi / CiUtil / Command / CompareCommand.php
1 <?php
2 namespace Civi\CiUtil\Command;
3
4 class CompareCommand {
5 static function main($argv) {
6 if (empty($argv[1])) {
7 echo "summary: Compares the output of different test runs\n";
8 echo "usage: phpunit-compare [--out=txt|csv] [--phpunit-json|--jenkins-xml] <file1> <file2>...\n";
9 exit(1);
10 }
11
12 $parser = array('\Civi\CiUtil\PHPUnitParser', 'parseJsonResults');
13 $printerType = 'txt';
14 $suites = array(); // array('file' => string, 'results' => array)
15 for ($i = 1; $i < count($argv); $i++) {
16 switch ($argv[$i]) {
17 case '--phpunit-json':
18 $parser = array('\Civi\CiUtil\PHPUnitParser', 'parseJsonResults');
19 break;
20 case '--jenkins-xml':
21 $parser = array('\Civi\CiUtil\JenkinsParser', 'parseXmlResults');
22 break;
23 case '--out=txt':
24 $printerType = 'txt';
25 break;
26 case '--out=csv':
27 $printerType = 'csv';
28 break;
29 default:
30 $suites[] = array(
31 'file' => $argv[$i],
32 'results' => call_user_func($parser, file_get_contents($argv[$i])),
33 );
34 }
35 }
36
37 $tests = array(); // array(string $name)
38 foreach ($suites as $suite) {
39 $tests = array_unique(array_merge(
40 $tests,
41 array_keys($suite['results'])
42 ));
43 }
44 sort($tests);
45
46 if ($printerType == 'csv') {
47 $printer = new \Civi\CiUtil\CsvPrinter('php://stdout', \Civi\CiUtil\Arrays::collect($suites, 'file'));
48 } else {
49 $printer = new \Civi\CiUtil\ComparisonPrinter(\Civi\CiUtil\Arrays::collect($suites, 'file'));
50 }
51 foreach ($tests as $test) {
52 $values = array();
53 foreach ($suites as $suite) {
54 $values[] = isset($suite['results'][$test]) ? $suite['results'][$test] : 'MISSING';
55 }
56
57 if (count(array_unique($values)) > 1) {
58 $printer->printRow($test, $values);
59 }
60 }
61 }
62 }