<?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>A Path Less Taken &#187; phpunit</title>
	<atom:link href="http://www.legendrefamily.org/blog/tag/phpunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.legendrefamily.org/blog</link>
	<description>Breaking with convention in a very conventional fashion.</description>
	<lastBuildDate>Tue, 11 Oct 2011 03:19:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unit Testing FTW</title>
		<link>http://www.legendrefamily.org/blog/2010/07/unit-testing-ftw/</link>
		<comments>http://www.legendrefamily.org/blog/2010/07/unit-testing-ftw/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 22:51:26 +0000</pubDate>
		<dc:creator>JJ</dc:creator>
				<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://www.legendrefamily.org/blog/?p=1313</guid>
		<description><![CDATA[I was recently working on a generic container class in PHP that implemented allowed, required and group behaviors related to assigned properties. While coding the class I built a test class using PHPUnit that had 7 tests and 33 assertions. After using the class on a limited basis I determined that the internal implementation left [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I was recently working on a generic container class in PHP that implemented allowed, required and group behaviors related to assigned properties.  While coding the class I built a test class using PHPUnit that had 7 tests and 33 assertions.  After using the class on a limited basis I determined that the internal implementation left something to be desired.  Unswayed I dug in and re-implemented the bulk of the internal methods without changing much of the interface of the class.  I ran the unit test again and found that only 8 of the assertions related to the modified behavior needed to be updated.  After I changed those assertions the class passed all the tests.</p>
<p>I know that many you are asking &#8220;what&#8217;s the big deal about that?&#8221;  The big deal to me is that I&#8217;m often reluctant to write tests because I&#8217;m a hobbyist developer and I&#8217;m always anxious to get straight to the code.  I guess you could say that this simple experience has changed my point of view.  It is very powerful to be able to change a class as fundamentally as I did and still be able to validate that it behaves as desired with such ease.  The benefit of all the upfront work in building unit tests was clear to see in this case.</p>
<p>If you have a similar experience or just a point of view one way or another on unit testing please post a comment below and share it with me.</p>
<div class="shr-publisher-1313"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.legendrefamily.org%2Fblog%2F2010%2F07%2Funit-testing-ftw%2F' data-shr_title='Unit+Testing+FTW'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.legendrefamily.org%2Fblog%2F2010%2F07%2Funit-testing-ftw%2F' data-shr_title='Unit+Testing+FTW'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.legendrefamily.org/blog/2010/07/unit-testing-ftw/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing PHPUnit with Kohana 3</title>
		<link>http://www.legendrefamily.org/blog/2010/07/installing-phpunit-with-kohana-3/</link>
		<comments>http://www.legendrefamily.org/blog/2010/07/installing-phpunit-with-kohana-3/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 04:17:54 +0000</pubDate>
		<dc:creator>JJ</dc:creator>
				<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[kohana]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php framework]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://www.legendrefamily.org/blog/?p=1290</guid>
		<description><![CDATA[Before you post a comment that says something like &#8220;there&#8217;s a Kohana 3 module that does that&#8221; let me just say I know and I&#8217;m grateful for it. Some of what I did was inspired by that module. The issue was when I tried getting the Kohana 3 unittest module to work to test my [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Before you post a comment that says something like &#8220;there&#8217;s a Kohana 3 module that does that&#8221; let me just say I know and I&#8217;m grateful for it.  Some of what I did was inspired by that module. The issue was when I tried getting the Kohana 3 <a href="http://github.com/kohana/unittest" title="kohana / unittest">unittest</a> module to work to test my module it was giving me problems.  I read the Kohana forum posts and the unittest documentation and could not find an answer.  Finally I decided to start at the beginning and set up PHPUnit manually to work with Kohana 3 and my module to debug what I was doing wrong with unittest.  In the process I discovered that getting PHPUnit to work with Kohana 3 wasn&#8217;t that hard after all.  I thought I would share my solution in the event you are having similar issues.</p>
<p><span id="more-1290"></span></p>
<p>The system requirements for me were the following:</p>
<ul>
<li>LightTPD HTTP Server</li>
<li>PHP 5.3.2</li>
<li>Kohana 3.0.6</li>
<li>PHPUnit 3.4</li>
</ul>
<p>First I set up my Kohana 3 app normally which included the module that I wanted to test.  I edited the default controller and created a class instance of an object from my module to verify there were no syntax errors.  When that worked as expected I removed the new code from the default controller restoring it to it&#8217;s previous state.</p>
<p>Next I downloaded a copy of <a href="http://www.phpunit.de/" title="PHPUnit">PHPUnit</a> from <a href="http://pear.phpunit.de/get/">here</a>.  I chose to manually install PHPUnit rather than use the PEAR installer so I could learn more about how it worked.  I opened the .tgz and .tar files and copied everything inside the PHPUnit-3.4.9 folder into my php\pear folder.  Then I moved the phpunit.php and phpunit.bat files into the top level folder of my Kohana 3 module (as in myapp/modules/mymodule).  We have to edit both of these files.</p>
<p>With the files in place I opened phpunit.bat in my editor.  There are only two lines that execute at the bottom of the file.  I commented out the first line and changed the second on by replacing the @php_bin@ inside of each string with the following:</p>
<ol>
<li>Replace the first @php_bin@ with the explicit path and file name of the command line PHP executable (in my case something like c:\php\php.exe).</li>
<li>Replace the second @php_bin@ with the path to the phpunit file (our phpunit.php file in the same directory as this file that we will rename next to just phpunit).</li>
</ol>
<p>I then saved the changes and closed the file.
</p>
<p>Next I renamed the phpunit.php file to just phpunit with no file extension.  Then I edited the file and made the following changes:</p>
<ol>
<li>Insert the following line below the Copyright notice (thanks to unittest&#8217;s author for this idea):

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SUPRESS_REQUEST'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'SUPRESS_REQUEST'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

</li>
<li>Insert the following line after the if block that includes a call to set_include_path:

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.;c:\php\pear;c:\lighttpd\htdocs\aa\modules\mymodule\tests'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The first path statement is to the php\pear directory where PHPUnit is found and the second is to the tests folder (you need to create this folder) where the unit tests for my module will be created.  I created mine in the root directory of my module.</li>
</ol>
<p>After making those changes I saved and closed this file.
</p>
<p>Next I edited the bootstrap.php file found at myapp\application\bootstrap.php.  I modified the last statement as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span> <span style="color: #990000;">defined</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SUPRESS_REQUEST'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
	 * If no source is specified, the URI will be automatically detected.
	 */</span>
	<span style="color: #b1b100;">echo</span> Request<span style="color: #339933;">::</span><span style="color: #004000;">instance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #339933;">-&gt;</span><span style="color: #004000;">send_headers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #339933;">-&gt;</span><span style="color: #004000;">response</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is part of what I copied from unittest.  It allows PHPUnit to bootstrap the Kohana framework using index.php which in turn calls bootstrap.php without actually taking the final step which is sending the response back to the browser with this statement.  When I was finished with this I saved and closed the file.  I also went back to the browser and confirmed that the site was still functioning normally.</p>
<p>All that was left was to write a unit test and try it out.  I followed the guidelines found <a href="http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html">here</a> for writing a unit test.  Then I saved the test file, opened a command window, navigated to the root directory of my module and ran the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">phpunit --bootstrap=c:\lighttpd\htdocs\myapp\index.php MyTestFileName</pre></div></div>

<p>PHPUnit ran, bootstrapped Kohana 3, executed my test and returned the result.
</p>
<p>That was all there was to it.  I&#8217;m not willing to suggest that I&#8217;ve solved all the issues.  I did not deal with any database connections nor did I try to test the behavior of any response code, but I still think this was a pretty simple way to get PHPUnit unit tests enabled for a Kohana 3 module.  If you wanted to do this for the application I think you just move the tests folder to under the application folder (myapp\application\tests) and update all the other steps to reflect that location.  If you are also having problems getting your Kohana 3 and PHPUnit unit tests to work and want to learn more about PHPUnit then give this approach a try.  If you have any questions leave a comment and I&#8217;ll be happy to help if I can.</p>
<div class="shr-publisher-1290"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.legendrefamily.org%2Fblog%2F2010%2F07%2Finstalling-phpunit-with-kohana-3%2F' data-shr_title='Installing+PHPUnit+with+Kohana+3'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.legendrefamily.org%2Fblog%2F2010%2F07%2Finstalling-phpunit-with-kohana-3%2F' data-shr_title='Installing+PHPUnit+with+Kohana+3'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.legendrefamily.org/blog/2010/07/installing-phpunit-with-kohana-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

