Skip to content

Commit b64695f

Browse files
authored
Merge pull request #464 from denniscoorn-paqt/fix/complexity-table-columns
Fixed issue with incorrect table header on the complexity report
2 parents e51b3d2 + 2e3b2bc commit b64695f

File tree

8 files changed

+101
-8
lines changed

8 files changed

+101
-8
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: 3 additions & 2 deletions
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
@@ -74,7 +75,7 @@
7475
<td><span class="path"><?php echo $class['name']; ?></span></td>
7576
<?php foreach (['wmc', 'ccn', 'ccnMethodMax', 'relativeSystemComplexity', 'relativeDataComplexity', 'relativeStructuralComplexity', 'bugs', 'kanDefect'] as $attribute) {?>
7677
<td>
77-
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
78+
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
7879
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
7980
</span>
8081
</td>

templates/html_report/coupling.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<td><span class="path"><?php echo $class['name']; ?></span></td>
2727
<?php foreach (['afferentCoupling', 'efferentCoupling', 'instability', 'pageRank'] as $attribute) {?>
2828
<td>
29-
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
29+
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
3030
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
3131
</span>
3232
</td>

templates/html_report/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
foreach ($classesS as $class) { ?>
148148
<tr>
149149
<td>
150-
<span class="badge" <?php echo gradientStyleFor($classes, 'pageRank', $class['pageRank']);?>);">
150+
<span class="badge" <?php echo gradientStyleFor($classes, 'pageRank', $class['pageRank']);?>>
151151
<?php echo $class['pageRank']; ?>
152152
</td>
153153
</td>

templates/html_report/junit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ classes never called by tests
8282
<td><span class="path"><?php echo $class['name']; ?></span></td>
8383
<?php foreach (['ccn', 'bugs'] as $attribute) {?>
8484
<td>
85-
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
85+
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
8686
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
8787
</span>
8888
</td>

templates/html_report/loc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<td><span class="path"><?php echo $class['name']; ?></span></td>
5858
<?php foreach (['lloc', 'cloc', 'volume', 'intelligentContent', 'commentWeight'] as $attribute) {?>
5959
<td>
60-
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
60+
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
6161
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
6262
</span>
6363
</td>

templates/html_report/oop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<td><span class="path"><?php echo $class['name']; ?></span></td>
7878
<?php foreach (['lcom', 'volume', 'ccn', 'ccnMethodMax', 'bugs', 'difficulty'] as $attribute) {?>
7979
<td>
80-
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
80+
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
8181
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
8282
</span>
8383
</td>
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)