Skip to content

Commit

Permalink
Merge pull request #464 from denniscoorn-paqt/fix/complexity-table-co…
Browse files Browse the repository at this point in the history
…lumns

Fixed issue with incorrect table header on the complexity report
  • Loading branch information
Halleck45 committed Mar 23, 2022
2 parents e51b3d2 + 2e3b2bc commit b64695f
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"require-dev": {
"phpunit/phpunit": "^4.8.36 || ^5.7.27 || ^6.5.14",
"sebastian/comparator": ">=1.2.3",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.5",
"symfony/dom-crawler": "^3.0 || ^4.0 || ^5.0"
},
"bin": [
"bin/phpmetrics"
Expand Down
5 changes: 3 additions & 2 deletions templates/html_report/complexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@
<th class="js-sort-number">WMC</th>
<th class="js-sort-number">Class cycl.</th>
<th class="js-sort-number">Max method cycl.</th>
<th class="js-sort-number">Relative system complexity</th>
<th class="js-sort-number">Relative data complexity</th>
<th class="js-sort-number">Relative structural complexity</th>
<th class="js-sort-number">Bugs</th>
<th class="js-sort-number">Defects</th>
<?php if ($config->has('junit')) { ?>
<th class="js-sort-number">Unit testsuites calling it</th>
<?php } ?><th class="js-sort-number">Relative system complexity</th>
<?php } ?>
</tr>
</thead>
<?php
Expand All @@ -74,7 +75,7 @@
<td><span class="path"><?php echo $class['name']; ?></span></td>
<?php foreach (['wmc', 'ccn', 'ccnMethodMax', 'relativeSystemComplexity', 'relativeDataComplexity', 'relativeStructuralComplexity', 'bugs', 'kanDefect'] as $attribute) {?>
<td>
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
</span>
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/html_report/coupling.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<td><span class="path"><?php echo $class['name']; ?></span></td>
<?php foreach (['afferentCoupling', 'efferentCoupling', 'instability', 'pageRank'] as $attribute) {?>
<td>
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
</span>
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/html_report/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
foreach ($classesS as $class) { ?>
<tr>
<td>
<span class="badge" <?php echo gradientStyleFor($classes, 'pageRank', $class['pageRank']);?>);">
<span class="badge" <?php echo gradientStyleFor($classes, 'pageRank', $class['pageRank']);?>>
<?php echo $class['pageRank']; ?>
</td>
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/html_report/junit.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ classes never called by tests
<td><span class="path"><?php echo $class['name']; ?></span></td>
<?php foreach (['ccn', 'bugs'] as $attribute) {?>
<td>
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
</span>
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/html_report/loc.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<td><span class="path"><?php echo $class['name']; ?></span></td>
<?php foreach (['lloc', 'cloc', 'volume', 'intelligentContent', 'commentWeight'] as $attribute) {?>
<td>
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
</span>
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/html_report/oop.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<td><span class="path"><?php echo $class['name']; ?></span></td>
<?php foreach (['lcom', 'volume', 'ccn', 'ccnMethodMax', 'bugs', 'difficulty'] as $attribute) {?>
<td>
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>);">
<span class="badge" <?php echo gradientStyleFor($classes, $attribute, $class[$attribute]);?>>
<?php echo isset($class[$attribute]) ? $class[$attribute] : ''; ?>
</span>
</td>
Expand Down
91 changes: 91 additions & 0 deletions tests/Report/Html/ComplexityReportRegressionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Test\Hal\Reporter\Html;

use DOMNode;
use Hal\Application\Config\Config;
use Hal\Component\Output\TestOutput;
use Hal\Metric\Group\Group;
use Hal\Metric\Metrics;
use Hal\Report\Html\Reporter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DomCrawler\Crawler;

/**
* @group reporter
* @group html
*/
class ComplexityReportRegressionTest extends TestCase
{
/**
* @dataProvider tableHeaderDataProvider
*/
public function testComplexityHtmlReportContainsCorrectOrderOfTableColumns($junitEnabled, $expectedTableHeader)
{
$config = new Config();
$output = new TestOutput();
$reporter = new Reporter($config, $output);

// prepares data for report
$groups = [];
$groups[] = new Group('group', '.*');
$config->set('groups', $groups);

if ($junitEnabled) {
$config->set('junit', ['file' => '/tmp/junit.xml']);
}

// prepares destination
$destination = implode(DIRECTORY_SEPARATOR, [
sys_get_temp_dir(),
'phpmetrics-html' . uniqid('', true)
]);

$config->set('report-html', $destination);

// generates report
$metrics = new Metrics();
$reporter->generate($metrics);

// ensure complexity report contains expected table header columns
$content = file_get_contents(sprintf('%s/complexity.html', $destination));
$actualTableHeader = $this->getActualTableHeader($content);

$this->assertEquals($expectedTableHeader, $actualTableHeader);
}

public function tableHeaderDataProvider()
{
$defaultTableHeader = [
'Class',
'WMC',
'Class cycl.',
'Max method cycl.',
'Relative system complexity',
'Relative data complexity',
'Relative structural complexity',
'Bugs',
'Defects',
];

$junitTableHeader = array_merge($defaultTableHeader, [
'Unit testsuites calling it'
]);

return [
'junit disabled' => [false, $defaultTableHeader],
'junit enabled' => [true, $junitTableHeader],
];
}

private function getActualTableHeader($content)
{
$tableHeaderColumnNodes = (new Crawler($content))
->filterXPath('.//table[contains(concat(" ",normalize-space(@class)," ")," js-sort-table ")]/thead/tr')
->children();

return array_map(function (DomNode $node) {
return $node->textContent;
}, iterator_to_array($tableHeaderColumnNodes));
}
}

0 comments on commit b64695f

Please sign in to comment.