Skip to content

Commit

Permalink
Tests for XML Report
Browse files Browse the repository at this point in the history
  • Loading branch information
Maks3w authored and sebastianbergmann committed Nov 6, 2015
1 parent 50d650c commit 795fe55
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 0 deletions.
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
108 changes: 108 additions & 0 deletions tests/PHP/CodeCoverage/Report/XMLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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_XML class.
*
* @since Class available since Release 3.0.2
*/
class PHP_CodeCoverage_Report_XMLTest 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 . 'XML';
}

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

$tmpFilesIterator = new FilesystemIterator(self::$TEST_TMP_PATH);
foreach ($tmpFilesIterator as $path => $fileInfo) {
/* @var SplFileInfo $fileInfo */
unlink($fileInfo->getPathname());
}
}

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

$xml = new PHP_CodeCoverage_Report_XML;
$xml->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH);

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

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

$xml = new PHP_CodeCoverage_Report_XML;
$xml->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH);

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

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

$xml = new PHP_CodeCoverage_Report_XML;
$xml->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 FilesystemIterator($actualFilesPath);

$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(),
file_get_contents($actualFile),
"${filename} not match"
);
}
}
}
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
abstract class PHP_CodeCoverage_TestCase extends PHPUnit_Framework_TestCase
{
static protected $TEST_TMP_PATH;

public static function setUpBeforeClass()
{
self::$TEST_TMP_PATH = TEST_FILES_PATH . 'tmp';
}

protected function getXdebugDataForBankAccount()
{
return [
Expand Down
40 changes: 40 additions & 0 deletions tests/_files/Report/XML/CoverageForBankAccount/BankAccount.php.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0"?>
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
<file name="BankAccount.php">
<totals>
<lines total="33" comments="0" code="33" executable="10" executed="5" percent="50.00%"/>
<methods count="4" tested="3" percent="75.00%"/>
<functions count="0" tested="0" percent=""/>
<classes count="1" tested="0" percent="0.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
<class name="BankAccount" start="2" executable="10" executed="5" crap="8.12">
<package full="" name="" sub="" category=""/>
<namespace name=""/>
<method name="getBalance" signature="getBalance()" start="6" end="9" crap="1" executable="1" executed="1" coverage="100"/>
<method name="setBalance" signature="setBalance($balance)" start="11" end="18" crap="6" executable="5" executed="0" coverage="0"/>
<method name="depositMoney" signature="depositMoney($balance)" start="20" end="25" crap="1" executable="2" executed="2" coverage="100"/>
<method name="withdrawMoney" signature="withdrawMoney($balance)" start="27" end="32" crap="1" executable="2" executed="2" coverage="100"/>
</class>
<coverage>
<line nr="8">
<covered by="BankAccountTest::testBalanceIsInitiallyZero"/>
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
</line>
<line nr="22">
<covered by="BankAccountTest::testBalanceCannotBecomeNegative2"/>
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
</line>
<line nr="24">
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
</line>
<line nr="29">
<covered by="BankAccountTest::testBalanceCannotBecomeNegative"/>
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
</line>
<line nr="31">
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
</line>
</coverage>
</file>
</phpunit>
29 changes: 29 additions & 0 deletions tests/_files/Report/XML/CoverageForBankAccount/index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
<project name="%s">
<tests>
<test name="BankAccountTest::testBalanceIsInitiallyZero" size="unknown" result="0" status="PASSED"/>
<test name="BankAccountTest::testBalanceCannotBecomeNegative" size="unknown" result="0" status="PASSED"/>
<test name="BankAccountTest::testBalanceCannotBecomeNegative2" size="unknown" result="0" status="PASSED"/>
<test name="BankAccountTest::testDepositWithdrawMoney" size="unknown" result="0" status="PASSED"/>
</tests>
<directory name="%s">
<totals>
<lines total="33" comments="0" code="33" executable="10" executed="5" percent="50.00%"/>
<methods count="4" tested="3" percent="75.00%"/>
<functions count="0" tested="0" percent=""/>
<classes count="1" tested="0" percent="0.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
<file name="BankAccount.php" href="BankAccount.php.xml">
<totals>
<lines total="33" comments="0" code="33" executable="10" executed="5" percent="50.00%"/>
<methods count="4" tested="3" percent="75.00%"/>
<functions count="0" tested="0" percent=""/>
<classes count="1" tested="0" percent="0.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
</file>
</directory>
</project>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
<project name="%s">
<tests>
<test name="ClassWithAnonymousFunction" size="unknown" result="0" status="PASSED"/>
</tests>
<directory name="%s">
<totals>
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
<methods count="2" tested="1" percent="50.00%"/>
<functions count="0" tested="0" percent=""/>
<classes count="1" tested="0" percent="0.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
<file name="source_with_class_and_anonymous_function.php" href="source_with_class_and_anonymous_function.php.xml">
<totals>
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
<methods count="2" tested="1" percent="50.00%"/>
<functions count="0" tested="0" percent=""/>
<classes count="1" tested="0" percent="0.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
</file>
</directory>
</project>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0"?>
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
<file name="source_with_class_and_anonymous_function.php">
<totals>
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
<methods count="2" tested="1" percent="50.00%"/>
<functions count="0" tested="0" percent=""/>
<classes count="1" tested="0" percent="0.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
<class name="CoveredClassWithAnonymousFunctionInStaticMethod" start="3" executable="8" executed="7" crap="2.01">
<package full="" name="" sub="" category=""/>
<namespace name=""/>
<method name="runAnonymous" signature="runAnonymous()" start="5" end="18" crap="1.04" executable="3" executed="2" coverage="66.666666666667"/>
<method name="anonymous function" signature="anonymous function (&amp;$val, $key)" start="11" end="13" crap="1" executable="2" executed="2" coverage="100"/>
</class>
<coverage>
<line nr="7">
<covered by="ClassWithAnonymousFunction"/>
</line>
<line nr="9">
<covered by="ClassWithAnonymousFunction"/>
</line>
<line nr="12">
<covered by="ClassWithAnonymousFunction"/>
</line>
<line nr="13">
<covered by="ClassWithAnonymousFunction"/>
</line>
<line nr="14">
<covered by="ClassWithAnonymousFunction"/>
</line>
<line nr="17">
<covered by="ClassWithAnonymousFunction"/>
</line>
<line nr="18">
<covered by="ClassWithAnonymousFunction"/>
</line>
</coverage>
</file>
</phpunit>
26 changes: 26 additions & 0 deletions tests/_files/Report/XML/CoverageForFileWithIgnoredLines/index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
<project name="%s">
<tests>
<test name="FileWithIgnoredLines" size="unknown" result="0" status="PASSED"/>
</tests>
<directory name="%s">
<totals>
<lines total="37" comments="12" code="25" executable="2" executed="1" percent="50.00%"/>
<methods count="0" tested="0" percent=""/>
<functions count="1" tested="0" percent="0.00%"/>
<classes count="2" tested="2" percent="100.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
<file name="source_with_ignore.php" href="source_with_ignore.php.xml">
<totals>
<lines total="37" comments="12" code="25" executable="2" executed="1" percent="50.00%"/>
<methods count="0" tested="0" percent=""/>
<functions count="1" tested="0" percent="0.00%"/>
<classes count="2" tested="2" percent="100.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
</file>
</directory>
</project>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0"?>
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
<file name="source_with_ignore.php">
<totals>
<lines total="37" comments="12" code="25" executable="2" executed="1" percent="50.00%"/>
<methods count="0" tested="0" percent=""/>
<functions count="1" tested="0" percent="0.00%"/>
<classes count="2" tested="2" percent="100.00%"/>
<traits count="0" tested="0" percent=""/>
</totals>
<class name="Foo" start="11" executable="0" executed="0" crap="1">
<package full="" name="" sub="" category=""/>
<namespace name=""/>
<method name="bar" signature="bar()" start="13" end="15" crap="1" executable="0" executed="0" coverage="100"/>
</class>
<class name="Bar" start="18" executable="0" executed="0" crap="1">
<package full="" name="" sub="" category=""/>
<namespace name=""/>
<method name="foo" signature="foo()" start="23" end="25" crap="1" executable="0" executed="0" coverage="100"/>
</class>
<function name="baz" signature="baz()" start="28" crap="0" executable="0" executed="0" coverage="0"/>
<coverage>
<line nr="2">
<covered by="FileWithIgnoredLines"/>
</line>
</coverage>
</file>
</phpunit>

0 comments on commit 795fe55

Please sign in to comment.