|
| 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