<?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>Straylight Run &#187; unit testing</title>
	<atom:link href="http://blog.straylightrun.net/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.straylightrun.net</link>
	<description>Software, Technology, PHP</description>
	<lastBuildDate>Tue, 11 May 2010 03:53:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Database Unit Testing</title>
		<link>http://blog.straylightrun.net/2009/08/27/database-unit-testing/</link>
		<comments>http://blog.straylightrun.net/2009/08/27/database-unit-testing/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 17:04:52 +0000</pubDate>
		<dc:creator>gerard</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.straylightrun.net/?p=233</guid>
		<description><![CDATA[When I first looked into unit testing, the concept seemed simple enough.  The canonical example seemed to be the sum function.

public function sum&#40;$x, $y&#41;
&#123;
    return $x + $y;
&#125;

And the test.

public function testSum&#40;&#41;
&#123;
    $summer = new Summer&#40;&#41;;
&#160;
    $this-&#62;assertEquals&#40;7, $summer-&#62;sum&#40;3, 4&#41;&#41;;
&#160;
    // and after awhile, [...]]]></description>
			<content:encoded><![CDATA[<p>When I first looked into unit testing, the concept seemed simple enough.  The canonical example seemed to be the sum function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sum<span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$y</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the test.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testSum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$summer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Summer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #000088;">$summer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sum</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</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;">// and after awhile, trying to break the </span>
    <span style="color: #666666; font-style: italic;">// function turned out to be part of the fun.</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #000088;">$summer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sum</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #000088;">$summer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sum</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In any case, this was nice and all for arithmetic, but like in any typical web app, I soon ran into the big problem: how do I test the database?</p>
<p>Now, there is a <a href="http://www.phpunit.de/manual/3.4/en/database.html">chapter on database testing in the PHPUnit manual</a> with <a href="http://www.ds-o.com/archives/63-PHPUnit-Database-Extension-DBUnit-Port.html">DBUnit for PHP</a>.  But as I read about those, I saw a lot of documentation about syntax and handling of null values and empty tables. I wondered why I couldn&#8217;t just create a <em>real</em> database and test it with <em>real</em> queries?</p>
<p><strong><span style="text-decoration: underline;">Enter SQLite</span></strong></p>
<p><a href="http://www.sqlite.org/">SQLite</a> is a self-contained, serverless, zero-configuration, ACID-compliant RDBMS.  Since SQLite requires no setup, and runs completely in memory, it makes the perfect database to run self-contained, serverless, automated unit tests against.</p>
<p><strong><span style="text-decoration: underline;">Database Access Abstraction To The Rescue</span></strong></p>
<p>Being able to test seamlessly with a SQLite database in my unit tests and run against a MySQL database in live code required some careful thought about how I went about setting up my classes.  First of all, I created a database access interface with some standard functions like <code>query(), getRows(), startTransaction(), commit()</code>, etc.  (You <em>are</em> using a <a href="http://blog.straylightrun.net/2009/04/30/who-needs-a-database-abstraction-layer-anyway/">database access class</a>, aren&#8217;t you?)  Then, I implemented the interface with a MySQL implementation, and an SQLite implementation.</p>
<p>Since there are places in the code that takes advantage of MySQL specific features, I had to emulate these features in my SQLite driver.  For example, there was no <a href="http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_lpad"><code>lpad()</code></a> function in SQLite.  Fortunately, thanks to the magic of <a href="http://us3.php.net/manual/en/function.sqlite-create-function.php"><code>createFunction()</code></a>, I could create a SQLite User-Defined Function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> lpad<span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$numchars</span><span style="color: #339933;">,</span> <span style="color: #000088;">$char</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">str_pad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$numchars</span><span style="color: #339933;">,</span> <span style="color: #000088;">$char</span><span style="color: #339933;">,</span> STR_PAD_LEFT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$sqlite</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createFunction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lpad'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'lpad'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Some of the MySQL bells and whistles are of course missing, such as <a href="http://blog.straylightrun.net/2009/02/09/tip-of-the-day-on-duplicate-key-update/"><code>ON DUPLICATE KEY UPDATE</code></a>. With some fancy query re-writing, I got those MySQL queries working with SQLite.  Here is my current <a href="http://blog.straylightrun.net/wp-content/uploads/2009/08/Database.phps">SQLite database access class in its entirety</a>.</p>
<p><strong><span style="text-decoration: underline;">Dependency Injection FTW!</span></strong></p>
<p>Now, that I had a MySQL database driver, and an equivalent SQLite database driver, I needed a way to get my business objects to use the SQLite version in a test, and the MySQL version during real execution.  If only there was a pattern that let me &#8220;inject&#8221; the proper database dependency into the object.  Well, this is exactly what <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependency Injection</a> is!</p>
<p>Instead of instantiating a database driver within the class, I passed the database object in the constructor.  There are other ways to pass in the object, such as with a setter method, or with a global registry, but I like constructor the best.  So the class might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Customer
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_db</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</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>_db <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong><span style="text-decoration: underline;">Writing The Test</span></strong></p>
<p>With the database access classes in place set to be injected into my classes, I could test functions that persisted data to the database, and then <em>actually</em> inspect the database.  There was no XML data files.  There was no database testing DSL.  There was only plain ol&#8217; SQL.</p>
<p>Here is a simplified function that saves fictional customer data, and a test for it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> save<span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lastName</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>_db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO customers (first_name, last_name) VALUES (?, ?)&quot;</span><span style="color: #339933;">,</span> 
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lastName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">lastInsertId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the test.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> CustomerTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_db</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUp<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>_db <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Test_Database<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CREATE TABLE customers (first_name, last_name_);&quot;</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: #000000; font-weight: bold;">function</span> tearDown<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>_db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DROP TABLE customers&quot;</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: #000000; font-weight: bold;">function</span> testSave<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$customer</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Customer<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_db<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$customer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mickey&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Mouse&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// test that customer is saved to DB</span>
        <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_db<span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRows</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM customers&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mickey&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mouse&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$results</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'last_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</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><span style="text-decoration: underline;"><strong>One Last Thing</strong></span></p>
<p>This method could get to be overkill in certain situations as you write tons of SQL to set up your tests.  But even then I find it preferable to setting up XML or CSV data.  As far as mocking out a database with some sort of Mock Object, I still maintain, why use a mock and a DSL when you can use the real thing?  This has worked well for me so far.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.straylightrun.net/2009/08/27/database-unit-testing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Slides From OINK-PUG, My Road To Test-Driven Development</title>
		<link>http://blog.straylightrun.net/2009/08/21/slides-from-oink-pug-my-road-to-test-driven-development/</link>
		<comments>http://blog.straylightrun.net/2009/08/21/slides-from-oink-pug-my-road-to-test-driven-development/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 22:58:08 +0000</pubDate>
		<dc:creator>gerard</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[talks]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[test first]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://blog.straylightrun.net/2009/08/21/slides-from-oink-pug-my-road-to-test-driven-development/</guid>
		<description><![CDATA[This past Thursday I gave a short talk at the monthly OINK-PUG meeting about how I discovered the Test-Driven Development Kool-Aid®.&#160; I knew about TDD years before I actually started doing it.&#160; I tried to start several times, and never kept it going.&#160; About a year ago, while developing a web service with no screens, [...]]]></description>
			<content:encoded><![CDATA[<p>This past Thursday I gave a short talk at the monthly <a href="http://www.oink-pug.org">OINK-PUG</a> meeting about how I discovered the Test-Driven Development Kool-Aid®.&nbsp; I knew about TDD years before I actually started doing it.&nbsp; I tried to start several times, and never kept it going.&nbsp; About a year ago, while developing a web service with no screens, the light bulb went on and gave me the idea for this talk.</p>
<blockquote><p>In which I give a short introduction to unit testing, and then outline the story that finally turned on the light bulb about real test-driven development and test first for me. The third part of the talk gives an introduction to Dependency Injection (DI), an integral part to unit testing. </p>
</blockquote>
<div style="text-align: left; width: 425px" id="__ss_1890999"><a style="margin: 12px 0px 3px; display: block; font: 14px helvetica,arial,sans-serif; text-decoration: underline" title="My Road To Test Driven Development" href="http://www.slideshare.net/gerrys0/my-road-to-test-driven-development">My Road To Test Driven Development</a><embed height="355" type="application/x-shockwave-flash" width="425" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=test-driven-development-090821123047-phpapp01&amp;stripped_title=my-road-to-test-driven-development" allowfullscreen="true" allowscriptaccess="always"></embed>
<div style="font-family: tahoma,arial; height: 26px; font-size: 11px; padding-top: 2px">View more <a style="text-decoration: underline" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration: underline" href="http://www.slideshare.net/gerrys0">Gerard Sychay</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.straylightrun.net/2009/08/21/slides-from-oink-pug-my-road-to-test-driven-development/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->