Skip to content

Commit 0e6737e

Browse files
author
Dennis Coorn
committed
Fixed issue with incorrect table header on the complexity report page
PR #421 accidentally moved the column "Relative system complexity" to the end of the table header causing an issue where the order of the header columns don't match the order of the data columns. A unit test has been added to validate the expected order of the columns.
1 parent dbe88ae commit 0e6737e

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"require-dev": {
4040
"phpunit/phpunit": "^4.8.36 || ^5.7.27 || ^6.5.14",
4141
"sebastian/comparator": ">=1.2.3",
42-
"squizlabs/php_codesniffer": "^3.5"
42+
"squizlabs/php_codesniffer": "^3.5",
43+
"symfony/dom-crawler": "^3.0 || ^4.0 || ^5.0"
4344
},
4445
"bin": [
4546
"bin/phpmetrics"

templates/html_report/complexity.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@
5959
<th class="js-sort-number">WMC</th>
6060
<th class="js-sort-number">Class cycl.</th>
6161
<th class="js-sort-number">Max method cycl.</th>
62+
<th class="js-sort-number">Relative system complexity</th>
6263
<th class="js-sort-number">Relative data complexity</th>
6364
<th class="js-sort-number">Relative structural complexity</th>
6465
<th class="js-sort-number">Bugs</th>
6566
<th class="js-sort-number">Defects</th>
6667
<?php if ($config->has('junit')) { ?>
6768
<th class="js-sort-number">Unit testsuites calling it</th>
68-
<?php } ?><th class="js-sort-number">Relative system complexity</th>
69+
<?php } ?>
6970
</tr>
7071
</thead>
7172
<?php
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Test\Hal\Reporter\Html;
4+
5+
use DOMNode;
6+
use Hal\Application\Config\Config;
7+
use Hal\Component\Output\TestOutput;
8+
use Hal\Metric\Group\Group;
9+
use Hal\Metric\Metrics;
10+
use Hal\Report\Html\Reporter;
11+
use PHPUnit\Framework\TestCase;
12+
use Symfony\Component\DomCrawler\Crawler;
13+
14+
/**
15+
* @group reporter
16+
* @group html
17+
*/
18+
class ComplexityReportRegressionTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider tableHeaderDataProvider
22+
*/
23+
public function testComplexityHtmlReportContainsCorrectOrderOfTableColumns($junitEnabled, $expectedTableHeader)
24+
{
25+
$config = new Config();
26+
$output = new TestOutput();
27+
$reporter = new Reporter($config, $output);
28+
29+
// prepares data for report
30+
$groups = [];
31+
$groups[] = new Group('group', '.*');
32+
$config->set('groups', $groups);
33+
34+
if ($junitEnabled) {
35+
$config->set('junit', ['file' => '/tmp/junit.xml']);
36+
}
37+
38+
// prepares destination
39+
$destination = implode(DIRECTORY_SEPARATOR, [
40+
sys_get_temp_dir(),
41+
'phpmetrics-html' . uniqid('', true)
42+
]);
43+
44+
$config->set('report-html', $destination);
45+
46+
// generates report
47+
$metrics = new Metrics();
48+
$reporter->generate($metrics);
49+
50+
// ensure complexity report contains expected table header columns
51+
$content = file_get_contents(sprintf('%s/complexity.html', $destination));
52+
$actualTableHeader = $this->getActualTableHeader($content);
53+
54+
$this->assertEquals($expectedTableHeader, $actualTableHeader);
55+
}
56+
57+
public function tableHeaderDataProvider()
58+
{
59+
$defaultTableHeader = [
60+
'Class',
61+
'WMC',
62+
'Class cycl.',
63+
'Max method cycl.',
64+
'Relative system complexity',
65+
'Relative data complexity',
66+
'Relative structural complexity',
67+
'Bugs',
68+
'Defects',
69+
];
70+
71+
$junitTableHeader = array_merge($defaultTableHeader, [
72+
'Unit testsuites calling it'
73+
]);
74+
75+
return [
76+
'junit disabled' => [false, $defaultTableHeader],
77+
'junit enabled' => [true, $junitTableHeader],
78+
];
79+
}
80+
81+
private function getActualTableHeader($content)
82+
{
83+
$tableHeaderColumnNodes = (new Crawler($content))
84+
->filterXPath('.//table[contains(concat(" ",normalize-space(@class)," ")," js-sort-table ")]/thead/tr')
85+
->children();
86+
87+
return array_map(function (DomNode $node) {
88+
return $node->textContent;
89+
}, iterator_to_array($tableHeaderColumnNodes));
90+
}
91+
}

0 commit comments

Comments
 (0)