<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pro-web.at &#187; php</title>
	<atom:link href="http://pro-web.at/archives/tag/php/feed" rel="self" type="application/rss+xml" />
	<link>http://pro-web.at</link>
	<description></description>
	<lastBuildDate>Sat, 06 Mar 2010 13:40:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Detailed report for PHPUnit test results</title>
		<link>http://pro-web.at/archives/detailed-report-for-phpunit-test-results</link>
		<comments>http://pro-web.at/archives/detailed-report-for-phpunit-test-results#comments</comments>
		<pubDate>Sun, 07 Jun 2009 18:15:37 +0000</pubDate>
		<dc:creator>Daniel Prieler</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://pro-web.at/?p=128</guid>
		<description><![CDATA[For my recent university php project we had to create a detailed test-report.
First we had the description of our test-cases in the code and also in a seperate overview-table in some wordfile.
Also the output of the PHPUnit test-results was by far not &#8220;enterprise ready&#8221;, so i started searching for a better solution.
I stumbeld upon a [...]


Related posts:<ol><li><a href='http://pro-web.at/archives/css-counter-test-auto-numbering-your-google-docs-headlines-with-css' rel='bookmark' title='Permanent Link: CSS Counter Test / Auto numbering your google docs headlines with CSS'>CSS Counter Test / Auto numbering your google docs headlines with CSS</a></li></ol>]]></description>
			<content:encoded><![CDATA[<p>For my recent university php project we had to create a detailed test-report.<br />
First we had the description of our test-cases in the code and also in a seperate overview-table in some wordfile.<br />
Also the output of the PHPUnit test-results was by far not &#8220;enterprise ready&#8221;, so i started searching for a better solution.</p>
<p>I stumbeld upon a post from <a href="http://parthpatil.com/2008/05/14/formatting-phpunit-test-results-as-html-table/">Parth Patil</a>, whose solution was to create an xml-report from PHPUnit and then use this xml to create your own report.</p>
<p>I used his solution, made it PHPUnit 3.4 compatible and also added some Reflection to see my testcase doc-comments in the report. (Note: For the refelection i use the Zend_Framework reflection class)<br />
<span id="more-128"></span><br />
<strong>The Result is:</strong><br />
<a href="http://pro-web.at/wp-content/uploads/2009/06/demo_result.jpg" class="lightbox"><img src="http://pro-web.at/wp-content/uploads/2009/06/demo_result.jpg" width="100%" alt="test result" /></a></p>
<p>For my TestCase:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> My_Test <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * This is the first demo test, which should succeed
	 * @return void
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * This is the second demo test, which should fail
	 * @return void
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testBar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And here&#8217;s the programming of the report:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AllTests
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$reportPath</span> <span style="color: #339933;">=</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// init a PHPUnit test-suite</span>
		<span style="color: #000088;">$suite</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPUnit_Framework_TestSuite<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Testsuite Name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$suite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addTestSuite</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My_Test'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// switch xml output on</span>
		PHPUnit_TextUI_TestRunner<span style="color: #339933;">::</span><span style="color: #004000;">run</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$suite</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'xmlLogfile'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$reportPath</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/log.xml'</span>
		<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// generate html-report</span>
		<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">generateHtmlReport</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reportPath</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #990000;">static</span> <span style="color: #000000; font-weight: bold;">function</span> generateHtmlReport<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$simple</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/log.xml'</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// loop through each testsuite</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$simple</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xpath</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'//testsuite'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$testsuite</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$testsuite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testcase</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// has no testcases</span>
				<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;h1&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$testsuite</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/h1&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;table cellspacing=&quot;0&quot; class=&quot;test_results&quot;&gt;
			  &lt;thead&gt;
			    &lt;tr&gt;&lt;th&gt;Test Name&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Result&lt;/th&gt;&lt;th&gt;Message&lt;/th&gt;&lt;/tr&gt;
			  &lt;/thead&gt;
			  &lt;tbody&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$testsuite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">testcase</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$testcase</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$reflection</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Reflection_Method<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$testcase</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'class'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$testcase</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;tr&gt;&lt;td class=&quot;name&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$testcase</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;td class=&quot;description&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #990000;">nl2br</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reflection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDocblock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getShortDescription</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$testcase</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">failure</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;td class=&quot;test_fail&quot;/&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;td&gt;&lt;pre&gt;'</span><span style="color: #339933;">.</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$testcase</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">failure</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/td&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;td class=&quot;test_pass&quot;/&gt;'</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;td&gt;ok&lt;/td&gt;'</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/tr&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000088;">$output</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/tbody&gt;&lt;/table&gt;'</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// stylesheet</span>
		<span style="color: #000088;">$output</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;style&gt; ... see attached zip-file ...&lt;/style&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// save report</span>
		<span style="color: #666666; font-style: italic;">// file_put_contents($path.'/report.html', $output);</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// or output it</span>
		<span style="color: #990000;">echo</span> <span style="color: #000088;">$output</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong><a href="http://pro-web.at/wp-content/uploads/2009/06/report_test.zip">Download the php-class for my demo report</a></strong></p>
<p>In some future i also will add these features:</p>
<ul>
<li>Print which functions each test covers (read the @covers annotation)</li>
<li>Show the test-class docBlock-description in the html-report</li>
<li>&#8230;</li>
</ul>


<p>Related posts:<ol><li><a href='http://pro-web.at/archives/css-counter-test-auto-numbering-your-google-docs-headlines-with-css' rel='bookmark' title='Permanent Link: CSS Counter Test / Auto numbering your google docs headlines with CSS'>CSS Counter Test / Auto numbering your google docs headlines with CSS</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://pro-web.at/archives/detailed-report-for-phpunit-test-results/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
