Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build/logs
build/pdepend
cache.properties
phpunit.xml
/tests/_files/tmp
/vendor
/composer.lock
/composer.phar
Expand Down
19 changes: 15 additions & 4 deletions src/CodeCoverage/Report/Clover.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
}

$lines[$method['startLine']] = [
'count' => $methodCount,
'crap' => $method['crap'],
'type' => 'method',
'name' => $methodName
'ccn' => $method['ccn'],
'count' => $methodCount,
'crap' => $method['crap'],
'type' => 'method',
'visibility' => $method['visibility'],
'name' => $methodName
];
}

Expand Down Expand Up @@ -131,6 +133,7 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
$xmlFile->appendChild($xmlClass);

$xmlMetrics = $xmlDocument->createElement('metrics');
$xmlMetrics->setAttribute('complexity', $class['ccn']);
$xmlMetrics->setAttribute('methods', $classMethods);
$xmlMetrics->setAttribute('coveredmethods', $coveredMethods);
$xmlMetrics->setAttribute('conditionals', 0);
Expand Down Expand Up @@ -176,6 +179,14 @@ public function process(PHP_CodeCoverage $coverage, $target = null, $name = null
$xmlLine->setAttribute('name', $data['name']);
}

if (isset($data['visibility'])) {
$xmlLine->setAttribute('visibility', $data['visibility']);
}

if (isset($data['ccn'])) {
$xmlLine->setAttribute('complexity', $data['ccn']);
}

if (isset($data['crap'])) {
$xmlLine->setAttribute('crap', $data['crap']);
}
Expand Down
50 changes: 26 additions & 24 deletions src/CodeCoverage/Report/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,18 +558,7 @@ protected function processClasses(PHP_Token_Stream $tokens)
$this->endLines[$class['endLine']] = &$this->classes[$className];

foreach ($class['methods'] as $methodName => $method) {
$this->classes[$className]['methods'][$methodName] = [
'methodName' => $methodName,
'signature' => $method['signature'],
'startLine' => $method['startLine'],
'endLine' => $method['endLine'],
'executableLines' => 0,
'executedLines' => 0,
'ccn' => $method['ccn'],
'coverage' => 0,
'crap' => 0,
'link' => $link . $method['startLine']
];
$this->classes[$className]['methods'][$methodName] = $this->newMethod($methodName, $method, $link);

$this->startLines[$method['startLine']] = &$this->classes[$className]['methods'][$methodName];
$this->endLines[$method['endLine']] = &$this->classes[$className]['methods'][$methodName];
Expand Down Expand Up @@ -605,18 +594,7 @@ protected function processTraits(PHP_Token_Stream $tokens)
$this->endLines[$trait['endLine']] = &$this->traits[$traitName];

foreach ($trait['methods'] as $methodName => $method) {
$this->traits[$traitName]['methods'][$methodName] = [
'methodName' => $methodName,
'signature' => $method['signature'],
'startLine' => $method['startLine'],
'endLine' => $method['endLine'],
'executableLines' => 0,
'executedLines' => 0,
'ccn' => $method['ccn'],
'coverage' => 0,
'crap' => 0,
'link' => $link . $method['startLine']
];
$this->traits[$traitName]['methods'][$methodName] = $this->newMethod($methodName, $method, $link);

$this->startLines[$method['startLine']] = &$this->traits[$traitName]['methods'][$methodName];
$this->endLines[$method['endLine']] = &$this->traits[$traitName]['methods'][$methodName];
Expand Down Expand Up @@ -676,4 +654,28 @@ protected function crap($ccn, $coverage)
pow($ccn, 2) * pow(1 - $coverage/100, 3) + $ccn
);
}

/**
* @param string $methodName
* @param array $method
* @param string $link
*
* @return array
*/
private function newMethod($methodName, array $method, $link)
{
return [
'methodName' => $methodName,
'visibility' => $method['visibility'],
'signature' => $method['signature'],
'startLine' => $method['startLine'],
'endLine' => $method['endLine'],
'executableLines' => 0,
'executedLines' => 0,
'ccn' => $method['ccn'],
'coverage' => 0,
'crap' => 0,
'link' => $link . $method['startLine'],
];
}
}
4 changes: 2 additions & 2 deletions src/CodeCoverage/Report/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ protected function getCoverageColor($numberOfCoveredElements, $totalNumberOfElem
return $this->colors['red'];
}

protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $presicion)
protected function printCoverageCounts($numberOfCoveredElements, $totalNumberOfElements, $precision)
{
$format = '%' . $presicion . 's';
$format = '%' . $precision . 's';

return PHP_CodeCoverage_Util::percent(
$numberOfCoveredElements,
Expand Down
8 changes: 1 addition & 7 deletions tests/PHP/CodeCoverage/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
* file that was distributed with this source code.
*/

if (!defined('TEST_FILES_PATH')) {
define(
'TEST_FILES_PATH',
dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR .
'_files' . DIRECTORY_SEPARATOR
);
}
require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestCase.php';

/**
* Tests for the PHP_CodeCoverage_Filter class.
Expand Down
10 changes: 1 addition & 9 deletions tests/PHP/CodeCoverage/Report/CloverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
* file that was distributed with this source code.
*/

if (!defined('TEST_FILES_PATH')) {
define(
'TEST_FILES_PATH',
dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR .
'_files' . DIRECTORY_SEPARATOR
);
}

require_once TEST_FILES_PATH . '../TestCase.php';
require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php';

/**
* Tests for the PHP_CodeCoverage_Report_Clover class.
Expand Down
58 changes: 58 additions & 0 deletions tests/PHP/CodeCoverage/Report/Crap4jTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the PHP_CodeCoverage package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php';

/**
* Tests for the PHP_CodeCoverage_Report_Crap4j class.
*
* @since Class available since Release 3.0.2
*/
class PHP_CodeCoverage_Report_Crap4jTest extends PHP_CodeCoverage_TestCase
{
/**
* @covers PHP_CodeCoverage_Report_Crap4j
*/
public function testForBankAccountTest()
{
$crap4j = new PHP_CodeCoverage_Report_Crap4j;

$this->assertStringMatchesFormatFile(
TEST_FILES_PATH . 'BankAccount-crap4j.xml',
$crap4j->process($this->getCoverageForBankAccount(), null, 'BankAccount')
);
}

/**
* @covers PHP_CodeCoverage_Report_Crap4j
*/
public function testForFileWithIgnoredLines()
{
$crap4j = new PHP_CodeCoverage_Report_Crap4j;

$this->assertStringMatchesFormatFile(
TEST_FILES_PATH . 'ignored-lines-crap4j.xml',
$crap4j->process($this->getCoverageForFileWithIgnoredLines(), null, 'CoverageForFileWithIgnoredLines')
);
}

/**
* @covers PHP_CodeCoverage_Report_Crap4j
*/
public function testForClassWithAnonymousFunction()
{
$crap4j = new PHP_CodeCoverage_Report_Crap4j;

$this->assertStringMatchesFormatFile(
TEST_FILES_PATH . 'class-with-anonymous-function-crap4j.xml',
$crap4j->process($this->getCoverageForClassWithAnonymousFunction(), null, 'CoverageForClassWithAnonymousFunction')
);
}
}
22 changes: 9 additions & 13 deletions tests/PHP/CodeCoverage/Report/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
* file that was distributed with this source code.
*/

if (!defined('TEST_FILES_PATH')) {
define(
'TEST_FILES_PATH',
dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR .
'_files' . DIRECTORY_SEPARATOR
);
}

require_once TEST_FILES_PATH . '../TestCase.php';
require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php';

/**
* Tests for the PHP_CodeCoverage_Report_Factory class.
Expand Down Expand Up @@ -69,7 +61,8 @@ public function testSomething()
'coverage' => 100,
'crap' => '1',
'link' => 'BankAccount.php.html#6',
'methodName' => 'getBalance'
'methodName' => 'getBalance',
'visibility' => 'public',
],
'setBalance' => [
'signature' => 'setBalance($balance)',
Expand All @@ -81,7 +74,8 @@ public function testSomething()
'coverage' => 0,
'crap' => 6,
'link' => 'BankAccount.php.html#11',
'methodName' => 'setBalance'
'methodName' => 'setBalance',
'visibility' => 'protected',
],
'depositMoney' => [
'signature' => 'depositMoney($balance)',
Expand All @@ -93,7 +87,8 @@ public function testSomething()
'coverage' => 100,
'crap' => '1',
'link' => 'BankAccount.php.html#20',
'methodName' => 'depositMoney'
'methodName' => 'depositMoney',
'visibility' => 'public',
],
'withdrawMoney' => [
'signature' => 'withdrawMoney($balance)',
Expand All @@ -105,7 +100,8 @@ public function testSomething()
'coverage' => 100,
'crap' => '1',
'link' => 'BankAccount.php.html#27',
'methodName' => 'withdrawMoney'
'methodName' => 'withdrawMoney',
'visibility' => 'public',
],
],
'startLine' => 2,
Expand Down
112 changes: 112 additions & 0 deletions tests/PHP/CodeCoverage/Report/HTMLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/*
* This file is part of the PHP_CodeCoverage package.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestCase.php';

/**
* Tests for the PHP_CodeCoverage_Report_HTML class.
*
* @since Class available since Release 3.0.2
*/
class PHP_CodeCoverage_Report_HTMLTest extends PHP_CodeCoverage_TestCase
{
static private $TEST_REPORT_PATH_SOURCE;

public static function setUpBeforeClass()
{
parent::setUpBeforeClass();

self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'HTML';
}

protected function tearDown()
{
parent::tearDown();

$tmpFilesIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(self::$TEST_TMP_PATH, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($tmpFilesIterator as $path => $fileInfo) {
/* @var SplFileInfo $fileInfo */
$pathname = $fileInfo->getPathname();
$fileInfo->isDir() ? rmdir($pathname) : unlink($pathname);
}
}

/**
* @covers PHP_CodeCoverage_Report_HTML
*/
public function testForBankAccountTest()
{
$expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount';

$report = new PHP_CodeCoverage_Report_HTML;
$report->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH);

$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
}

/**
* @covers PHP_CodeCoverage_Report_HTML
*/
public function testForFileWithIgnoredLines()
{
$expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines';

$report = new PHP_CodeCoverage_Report_HTML;
$report->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH);

$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
}

/**
* @covers PHP_CodeCoverage_Report_HTML
*/
public function testForClassWithAnonymousFunction()
{
$expectedFilesPath =
self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForClassWithAnonymousFunction';

$report = new PHP_CodeCoverage_Report_HTML;
$report->process($this->getCoverageForClassWithAnonymousFunction(), self::$TEST_TMP_PATH);

$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
}

/**
* @param string $expectedFilesPath
* @param string $actualFilesPath
*/
protected function assertFilesEquals($expectedFilesPath, $actualFilesPath)
{
$expectedFilesIterator = new FilesystemIterator($expectedFilesPath);
$actualFilesIterator = new RegexIterator(new FilesystemIterator($actualFilesPath), '/.html/');

$this->assertEquals(
iterator_count($expectedFilesIterator),
iterator_count($actualFilesIterator),
'Generated files and expected files not match'
);
foreach ($expectedFilesIterator as $path => $fileInfo) {
/* @var SplFileInfo $fileInfo */
$filename = $fileInfo->getFilename();

$actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename;

$this->assertFileExists($actualFile);
$this->assertStringMatchesFormatFile(
$fileInfo->getPathname(),
str_replace(PHP_EOL, "\n", file_get_contents($actualFile)),
"${filename} not match"
);
}
}
}
Loading