Skip to content

Commit 14863c2

Browse files
committed
Tests for XML Report
1 parent c84f05b commit 14863c2

File tree

9 files changed

+314
-0
lines changed

9 files changed

+314
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ build/logs
55
build/pdepend
66
cache.properties
77
phpunit.xml
8+
/tests/_files/tmp
89
/vendor
910
/composer.lock
1011
/composer.phar
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/*
3+
* This file is part of the PHP_CodeCoverage package.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
if (!defined('TEST_FILES_PATH')) {
12+
define(
13+
'TEST_FILES_PATH',
14+
dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR .
15+
'_files' . DIRECTORY_SEPARATOR
16+
);
17+
}
18+
19+
require_once TEST_FILES_PATH . '../TestCase.php';
20+
21+
/**
22+
* Tests for the PHP_CodeCoverage_Report_XML class.
23+
*
24+
* @since Class available since Release 3.0.2
25+
*/
26+
class PHP_CodeCoverage_Report_XMLTest extends PHP_CodeCoverage_TestCase
27+
{
28+
static private $TEST_REPORT_PATH_SOURCE;
29+
30+
public static function setUpBeforeClass()
31+
{
32+
parent::setUpBeforeClass();
33+
34+
self::$TEST_REPORT_PATH_SOURCE = TEST_FILES_PATH . 'Report' . DIRECTORY_SEPARATOR . 'XML';
35+
}
36+
37+
protected function tearDown()
38+
{
39+
parent::tearDown();
40+
41+
$tmpFilesIterator = new FilesystemIterator(self::$TEST_TMP_PATH);
42+
foreach ($tmpFilesIterator as $path => $fileInfo) {
43+
/** @var SplFileInfo $fileInfo */
44+
unlink($fileInfo->getPathname());
45+
}
46+
}
47+
48+
/**
49+
* @covers PHP_CodeCoverage_Report_XML
50+
*/
51+
public function testXmlForBankAccountTest()
52+
{
53+
$expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForBankAccount';
54+
55+
$xml = new PHP_CodeCoverage_Report_XML;
56+
$xml->process($this->getCoverageForBankAccount(), self::$TEST_TMP_PATH);
57+
58+
$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
59+
}
60+
61+
/**
62+
* @covers PHP_CodeCoverage_Report_XML
63+
*/
64+
public function testXmlForFileWithIgnoredLines()
65+
{
66+
$expectedFilesPath = self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForFileWithIgnoredLines';
67+
68+
$xml = new PHP_CodeCoverage_Report_XML;
69+
$xml->process($this->getCoverageForFileWithIgnoredLines(), self::$TEST_TMP_PATH);
70+
71+
$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
72+
}
73+
74+
/**
75+
* @covers PHP_CodeCoverage_Report_XML
76+
*/
77+
public function testXmlForClassWithAnonymousFunction()
78+
{
79+
$expectedFilesPath =
80+
self::$TEST_REPORT_PATH_SOURCE . DIRECTORY_SEPARATOR . 'CoverageForClassWithAnonymousFunction';
81+
82+
$xml = new PHP_CodeCoverage_Report_XML;
83+
$xml->process($this->getCoverageForClassWithAnonymousFunction(), self::$TEST_TMP_PATH);
84+
85+
$this->assertFilesEquals($expectedFilesPath, self::$TEST_TMP_PATH);
86+
}
87+
88+
/**
89+
* @param string $expectedFilesPath
90+
* @param string $actualFilesPath
91+
*/
92+
protected function assertFilesEquals($expectedFilesPath, $actualFilesPath)
93+
{
94+
$expectedFilesIterator = new FilesystemIterator($expectedFilesPath);
95+
$actualFilesIterator = new FilesystemIterator($actualFilesPath);
96+
97+
$this->assertEquals(
98+
iterator_count($expectedFilesIterator),
99+
iterator_count($actualFilesIterator),
100+
'Generated files and expected files not match'
101+
);
102+
foreach ($expectedFilesIterator as $path => $fileInfo) {
103+
/** @var SplFileInfo $fileInfo */
104+
$filename = $fileInfo->getFilename();
105+
106+
$actualFile = $actualFilesPath . DIRECTORY_SEPARATOR . $filename;
107+
108+
$this->assertFileExists($actualFile);
109+
$this->assertStringMatchesFormatFile(
110+
$fileInfo->getPathname(),
111+
file_get_contents($actualFile),
112+
"${filename} not match"
113+
);
114+
}
115+
}
116+
}

tests/TestCase.php

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
abstract class PHP_CodeCoverage_TestCase extends PHPUnit_Framework_TestCase
1717
{
18+
static protected $TEST_TMP_PATH;
19+
20+
public static function setUpBeforeClass()
21+
{
22+
self::$TEST_TMP_PATH = TEST_FILES_PATH . 'tmp';
23+
}
24+
1825
protected function getXdebugDataForBankAccount()
1926
{
2027
return [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
3+
<file name="BankAccount.php">
4+
<totals>
5+
<lines total="33" comments="0" code="33" executable="10" executed="5" percent="50.00%"/>
6+
<methods count="4" tested="3" percent="75.00%"/>
7+
<functions count="0" tested="0" percent=""/>
8+
<classes count="1" tested="0" percent="0.00%"/>
9+
<traits count="0" tested="0" percent=""/>
10+
</totals>
11+
<class name="BankAccount" start="2" executable="10" executed="5" crap="8.12">
12+
<package full="" name="" sub="" category=""/>
13+
<namespace name=""/>
14+
<method name="getBalance" signature="getBalance()" start="6" end="9" crap="1" executable="1" executed="1" coverage="100"/>
15+
<method name="setBalance" signature="setBalance($balance)" start="11" end="18" crap="6" executable="5" executed="0" coverage="0"/>
16+
<method name="depositMoney" signature="depositMoney($balance)" start="20" end="25" crap="1" executable="2" executed="2" coverage="100"/>
17+
<method name="withdrawMoney" signature="withdrawMoney($balance)" start="27" end="32" crap="1" executable="2" executed="2" coverage="100"/>
18+
</class>
19+
<coverage>
20+
<line nr="8">
21+
<covered by="BankAccountTest::testBalanceIsInitiallyZero"/>
22+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
23+
</line>
24+
<line nr="22">
25+
<covered by="BankAccountTest::testBalanceCannotBecomeNegative2"/>
26+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
27+
</line>
28+
<line nr="24">
29+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
30+
</line>
31+
<line nr="29">
32+
<covered by="BankAccountTest::testBalanceCannotBecomeNegative"/>
33+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
34+
</line>
35+
<line nr="31">
36+
<covered by="BankAccountTest::testDepositWithdrawMoney"/>
37+
</line>
38+
</coverage>
39+
</file>
40+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
3+
<project name="%s">
4+
<tests>
5+
<test name="BankAccountTest::testBalanceIsInitiallyZero" size="unknown" result="0" status="PASSED"/>
6+
<test name="BankAccountTest::testBalanceCannotBecomeNegative" size="unknown" result="0" status="PASSED"/>
7+
<test name="BankAccountTest::testBalanceCannotBecomeNegative2" size="unknown" result="0" status="PASSED"/>
8+
<test name="BankAccountTest::testDepositWithdrawMoney" size="unknown" result="0" status="PASSED"/>
9+
</tests>
10+
<directory name="%s">
11+
<totals>
12+
<lines total="33" comments="0" code="33" executable="10" executed="5" percent="50.00%"/>
13+
<methods count="4" tested="3" percent="75.00%"/>
14+
<functions count="0" tested="0" percent=""/>
15+
<classes count="1" tested="0" percent="0.00%"/>
16+
<traits count="0" tested="0" percent=""/>
17+
</totals>
18+
<file name="BankAccount.php" href="BankAccount.php.xml">
19+
<totals>
20+
<lines total="33" comments="0" code="33" executable="10" executed="5" percent="50.00%"/>
21+
<methods count="4" tested="3" percent="75.00%"/>
22+
<functions count="0" tested="0" percent=""/>
23+
<classes count="1" tested="0" percent="0.00%"/>
24+
<traits count="0" tested="0" percent=""/>
25+
</totals>
26+
</file>
27+
</directory>
28+
</project>
29+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
3+
<project name="%s">
4+
<tests>
5+
<test name="ClassWithAnonymousFunction" size="unknown" result="0" status="PASSED"/>
6+
</tests>
7+
<directory name="%s">
8+
<totals>
9+
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
10+
<methods count="2" tested="1" percent="50.00%"/>
11+
<functions count="0" tested="0" percent=""/>
12+
<classes count="1" tested="0" percent="0.00%"/>
13+
<traits count="0" tested="0" percent=""/>
14+
</totals>
15+
<file name="source_with_class_and_anonymous_function.php" href="source_with_class_and_anonymous_function.php.xml">
16+
<totals>
17+
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
18+
<methods count="2" tested="1" percent="50.00%"/>
19+
<functions count="0" tested="0" percent=""/>
20+
<classes count="1" tested="0" percent="0.00%"/>
21+
<traits count="0" tested="0" percent=""/>
22+
</totals>
23+
</file>
24+
</directory>
25+
</project>
26+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
3+
<file name="source_with_class_and_anonymous_function.php">
4+
<totals>
5+
<lines total="19" comments="2" code="17" executable="8" executed="7" percent="87.50%"/>
6+
<methods count="2" tested="1" percent="50.00%"/>
7+
<functions count="0" tested="0" percent=""/>
8+
<classes count="1" tested="0" percent="0.00%"/>
9+
<traits count="0" tested="0" percent=""/>
10+
</totals>
11+
<class name="CoveredClassWithAnonymousFunctionInStaticMethod" start="3" executable="8" executed="7" crap="2.01">
12+
<package full="" name="" sub="" category=""/>
13+
<namespace name=""/>
14+
<method name="runAnonymous" signature="runAnonymous()" start="5" end="18" crap="1.04" executable="3" executed="2" coverage="66.666666666667"/>
15+
<method name="anonymous function" signature="anonymous function (&amp;$val, $key)" start="11" end="13" crap="1" executable="2" executed="2" coverage="100"/>
16+
</class>
17+
<coverage>
18+
<line nr="7">
19+
<covered by="ClassWithAnonymousFunction"/>
20+
</line>
21+
<line nr="9">
22+
<covered by="ClassWithAnonymousFunction"/>
23+
</line>
24+
<line nr="12">
25+
<covered by="ClassWithAnonymousFunction"/>
26+
</line>
27+
<line nr="13">
28+
<covered by="ClassWithAnonymousFunction"/>
29+
</line>
30+
<line nr="14">
31+
<covered by="ClassWithAnonymousFunction"/>
32+
</line>
33+
<line nr="17">
34+
<covered by="ClassWithAnonymousFunction"/>
35+
</line>
36+
<line nr="18">
37+
<covered by="ClassWithAnonymousFunction"/>
38+
</line>
39+
</coverage>
40+
</file>
41+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
3+
<project name="%s">
4+
<tests>
5+
<test name="FileWithIgnoredLines" size="unknown" result="0" status="PASSED"/>
6+
</tests>
7+
<directory name="%s">
8+
<totals>
9+
<lines total="37" comments="12" code="25" executable="2" executed="1" percent="50.00%"/>
10+
<methods count="0" tested="0" percent=""/>
11+
<functions count="1" tested="0" percent="0.00%"/>
12+
<classes count="2" tested="2" percent="100.00%"/>
13+
<traits count="0" tested="0" percent=""/>
14+
</totals>
15+
<file name="source_with_ignore.php" href="source_with_ignore.php.xml">
16+
<totals>
17+
<lines total="37" comments="12" code="25" executable="2" executed="1" percent="50.00%"/>
18+
<methods count="0" tested="0" percent=""/>
19+
<functions count="1" tested="0" percent="0.00%"/>
20+
<classes count="2" tested="2" percent="100.00%"/>
21+
<traits count="0" tested="0" percent=""/>
22+
</totals>
23+
</file>
24+
</directory>
25+
</project>
26+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<phpunit xmlns="http://schema.phpunit.de/coverage/1.0">
3+
<file name="source_with_ignore.php">
4+
<totals>
5+
<lines total="37" comments="12" code="25" executable="2" executed="1" percent="50.00%"/>
6+
<methods count="0" tested="0" percent=""/>
7+
<functions count="1" tested="0" percent="0.00%"/>
8+
<classes count="2" tested="2" percent="100.00%"/>
9+
<traits count="0" tested="0" percent=""/>
10+
</totals>
11+
<class name="Foo" start="11" executable="0" executed="0" crap="1">
12+
<package full="" name="" sub="" category=""/>
13+
<namespace name=""/>
14+
<method name="bar" signature="bar()" start="13" end="15" crap="1" executable="0" executed="0" coverage="100"/>
15+
</class>
16+
<class name="Bar" start="18" executable="0" executed="0" crap="1">
17+
<package full="" name="" sub="" category=""/>
18+
<namespace name=""/>
19+
<method name="foo" signature="foo()" start="23" end="25" crap="1" executable="0" executed="0" coverage="100"/>
20+
</class>
21+
<function name="baz" signature="baz()" start="28" crap="0" executable="0" executed="0" coverage="0"/>
22+
<coverage>
23+
<line nr="2">
24+
<covered by="FileWithIgnoredLines"/>
25+
</line>
26+
</coverage>
27+
</file>
28+
</phpunit>

0 commit comments

Comments
 (0)