<?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>Jeff Carouth&#039;s blog&#187; Data Sources</title>
	<atom:link href="http://carouth.com/tag/data-sources/feed/" rel="self" type="application/rss+xml" />
	<link>http://carouth.com</link>
	<description>Ramblings of a Web Application Developer</description>
	<lastBuildDate>Sat, 06 Feb 2010 21:36:18 +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>Stubbing an Interface</title>
		<link>http://carouth.com/2009/11/18/stubbing-an-interface/</link>
		<comments>http://carouth.com/2009/11/18/stubbing-an-interface/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 17:41:33 +0000</pubDate>
		<dc:creator>jcarouth</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Data Sources]]></category>
		<category><![CDATA[Interfaces]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://carouth.com/?p=141</guid>
		<description><![CDATA[One of the topics I talk about frequently is coding to an interface. There is an abundance of evidence why one might choose to do so but today I'd like to talk about one benefit in terms of unit testing—another topic I love to talk about. To illustrate the benefit I will use an example [...]]]></description>
			<content:encoded><![CDATA[<p>One of the topics I talk about frequently is coding to an interface. There is an abundance of evidence why one might choose to do so but today I'd like to talk about one benefit in terms of unit testing—another topic I love to talk about. To illustrate the benefit I will use an example of having the data source coded against the interface thereby allowing the consumer of the data source to ignore the specifics behind how data is stored, e.g., in a relational database or in a key-value store, and concentrate solely on requesting the appropriate data. Before I get to the code, however, I think it's important that I define some of the scope of this post.</p>
<h2>Goals of a Unit Test Suite</h2>
<p>Other than exercising a substantial portion of your code a unit test suite must be capable of running quickly lest it be a burden on development. Having a burdensome, slow-running test suite will only encourage developers to ignore it and only run it when absolutely necessary. This, in turn, will make maintenance of the test suite more difficult and probably result in fragile or poor tests which ultimately will leave a bad taste in the developer's mouth about unit testing, and we wouldn't want that, now would we?</p>
<p>One sure-fire way to decrease the speed of a test suite is to make it rely on real-world data sources such as your relational database or even a web service. Imagine a test suite for an application that relies on Twitter's availability. I can assure you that I would not want to run that test suite. The fail whale is annoying enough on the web. But I digress. Being able to emulate a data storage source is paramount to the speed of a unit test. Stubbing is one technique employed to do such a thing.</p>
<h2>A Data Source Interface</h2>
<p>A basic data source needs the implement the standard <abbr title="Create, read, update, and delete">CRUD</abbr> methods and can be defined as follows:</p>
<pre class="brush: php;">interface ICrudDataSource
{
    public function create(array $data);

    public function read($id);

    public function update($id, array $data);

    public function delete($id);
}
</pre>
<p>If this were a real interface there would be PHPDoc blocks to indicate the behavior of the methods and parameter types, but for the sake of the example I omit them. Now we can implement this interface in our database table class and define the behavior for each of the methods in terms of how our chosen rdbms will understand them, e.g., create() is an INSERT statement, read() is a SELECT statement, so on and so forth.</p>
<p>We can then use this class in our unit tests and it will actually test that the database vendor correctly implemented the INSERT, SELECT, UPDATE, and DELETE statements and that we are calling them appropriately. While I do think the latter should be tested when we look at the database abstraction implementation, the former is not necessary and will only slow down tests that <em>use</em> the data source code but <em>don't directly depend on it</em> being a database, for example. In such a case a simple array-based storage mechanism will work.</p>
<h2>Array-based Stub for Unit Testing</h2>
<p>Thus, we implement the ICrudDataSource interface in a stub that is used for our unit tests that need to interact with the data storage but necessarily need to interact with the specific data storage used in production.</p>
<pre class="brush: php;">class UserDataStorageStub implements ICrudDataSource
{
    protected $_store;

    public function __construct()
    {
        $this-&gt;_store = array();
    }

    public function create(array $data)
    {
        if (!$this-&gt;exists($data['id'])) {
            $this-&gt;_store[$data['id']] = $data;
            return true;
        }

        return false;
    }

    public function read($id)
    {
        if ($this-&gt;exists($id)) {
            return $this-&gt;_store[$id];
        }

        return false;
    }

    public function update($id, array $data)
    {
        if (!$this-&gt;exists($id)) {
            return false;
        }

        $this-&gt;_store[$id] = $data;
    }

    public function delete($id)
    {
        unset($this-&gt;_store[$id]);
    }

    private function exists($id)
    {
        return array_key_exists($id, $this-&gt;_store);
    }
}</pre>
<p>With this array-based implementation the unit tests will be fast regardless of the availability of the actual data source or the load on that data source. This stub, or a stub that extends this one, can also be easily pre-populated with test data to allow for testing with fixed data (although this can lead to very brittle tests if done lackadaisically).</p>
<div class="acc_license"><a href="http://creativecommons.org/licenses/by/3.0/"><img src="http://i.creativecommons.org/l/by/3.0/88x31.png" alt="by" /></a></div><!--<rdf:RDF xmlns="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><Work rdf:about=""><license rdf:resource="http://creativecommons.org/licenses/by/3.0/" /></Work><License rdf:about="http://creativecommons.org/licenses/by/3.0/"><requires rdf:resource="http://creativecommons.org/ns#Attribution" /><permits rdf:resource="http://creativecommons.org/ns#Reproduction" /><permits rdf:resource="http://creativecommons.org/ns#Distribution" /><permits rdf:resource="http://creativecommons.org/ns#DerivativeWorks" /><requires rdf:resource="http://creativecommons.org/ns#Notice" /></License></rdf:RDF>-->


Share:


	<a rel="nofollow"  target="_blank" href="http://www.printfriendly.com/print?url=http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F&amp;partner=sociable" title="Print"><img src="http://carouth.com/wp-content/plugins/sociable/images/printfriendly.png" title="Print" alt="Print" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F&amp;title=Stubbing%20an%20Interface&amp;bodytext=One%20of%20the%20topics%20I%20talk%20about%20frequently%20is%20coding%20to%20an%20interface.%20There%20is%20an%20abundance%20of%20evidence%20why%20one%20might%20choose%20to%20do%20so%20but%20today%20I%27d%20like%20to%20talk%20about%20one%20benefit%20in%20terms%20of%20unit%20testing%E2%80%94another%20topic%20I%20love%20to%20talk%20about.%20To%20illust" title="Digg"><img src="http://carouth.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F&amp;title=Stubbing%20an%20Interface&amp;notes=One%20of%20the%20topics%20I%20talk%20about%20frequently%20is%20coding%20to%20an%20interface.%20There%20is%20an%20abundance%20of%20evidence%20why%20one%20might%20choose%20to%20do%20so%20but%20today%20I%27d%20like%20to%20talk%20about%20one%20benefit%20in%20terms%20of%20unit%20testing%E2%80%94another%20topic%20I%20love%20to%20talk%20about.%20To%20illust" title="del.icio.us"><img src="http://carouth.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F&amp;t=Stubbing%20an%20Interface" title="Facebook"><img src="http://carouth.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F&amp;title=Stubbing%20an%20Interface&amp;annotation=One%20of%20the%20topics%20I%20talk%20about%20frequently%20is%20coding%20to%20an%20interface.%20There%20is%20an%20abundance%20of%20evidence%20why%20one%20might%20choose%20to%20do%20so%20but%20today%20I%27d%20like%20to%20talk%20about%20one%20benefit%20in%20terms%20of%20unit%20testing%E2%80%94another%20topic%20I%20love%20to%20talk%20about.%20To%20illust" title="Google Bookmarks"><img src="http://carouth.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F&amp;title=Stubbing%20an%20Interface" title="Reddit"><img src="http://carouth.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F&amp;title=Stubbing%20an%20Interface" title="StumbleUpon"><img src="http://carouth.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Stubbing%20an%20Interface%20-%20http%3A%2F%2Fcarouth.com%2F2009%2F11%2F18%2Fstubbing-an-interface%2F" title="Twitter"><img src="http://carouth.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://carouth.com/2009/11/18/stubbing-an-interface/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
