<?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>Mats Lindh &#187; unittesting</title>
	<atom:link href="http://e-mats.org/category/unittesting/feed/" rel="self" type="application/rss+xml" />
	<link>http://e-mats.org</link>
	<description>Where desperate is just another word for a regular day.</description>
	<lastBuildDate>Fri, 23 Jul 2010 14:23:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Unit-Testing Code Which Uses a Database</title>
		<link>http://e-mats.org/2008/08/unit-testing-code-which-uses-a-database/</link>
		<comments>http://e-mats.org/2008/08/unit-testing-code-which-uses-a-database/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 09:07:53 +0000</pubDate>
		<dc:creator>Mats</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[unittesting]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://e-mats.org/?p=224</guid>
		<description><![CDATA[How to unit-test code that interacts with a database appeared on the blog of Baron Schwartz, and to be really boring, I agree with what he&#8217;s writing. Unit-testing database connectivity and storage is not hard. If it is, it might be a good time to redo that architecture you&#8217;ve been talking about. An important point [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.xaprb.com/blog/2008/08/19/how-to-unit-test-code-that-interacts-with-a-database/'>How to unit-test code that interacts with a database</a> appeared on <a href='http://www.xaprb.com/blog/'>the blog of Baron Schwartz</a>, and to be really boring, I agree with what he&#8217;s writing. Unit-testing database connectivity and storage is not hard. If it is, it might be a good time to redo that architecture you&#8217;ve been talking about.</p>
<p>An important point that Baron mentions is that you _NEVER_ _EVER_ run your tests on your production servers. That will of course be disastrous, as your tests needs a predefined state of the database to be valid for testing. The solution I&#8217;ve been using to handle this, is to always set up my environment to use another database when doing the tests. This way, you&#8217;ll never end up with running the tests on a live database by accident. I handle this in my AllTests.php file, where the test suites and shared fixtures are set up. We dump the contents of the developer database (<strong>databasename</strong>), create a new database (<strong>databasename_test</strong>) and insert all the current table structures and indexes. This way we get an accurate copy of the table definitions currently defined by the developer (so that we don&#8217;t run the tests against an old set of tables), and we test that the code works as it should with the active definitions.</p>
<p>The simplest way to do this, is to use mysqldump and mysql through a call to exec. If you&#8217;re not in a trusted environment, please, please, please <a href='http://no.php.net/manual/en/function.escapeshellarg.php'>add the appropriate shell argument escape commands</a>. It can however be argued that if you&#8217;re allowing random people to change your database login information, you probably have bigger problems than doing unit testing..</p>
<div class="geshi no php">
<ol>
<li class="li1">
<div class="de1"><span class="kw3">exec</span><span class="br0">&#40;</span><span class="st0">&#39;mysqldump -u &#39;</span> <span class="sy0">.</span> <span class="re1">$username</span> <span class="sy0">.</span> <span class="st0">&#39; -p&#39;</span> <span class="sy0">.</span> <span class="re1">$password</span> <span class="sy0">.</span> <span class="st0">&#39; &#39;</span> <span class="sy0">.</span> <span class="re1">$dbname</span> <span class="sy0">.</span> <span class="st0">&#39; | mysql -u &#39;</span> <span class="sy0">.</span> <span class="re1">$username</span> <span class="sy0">.</span> <span class="st0">&#39; -p&#39;</span> <span class="sy0">.</span> <span class="re1">$password</span> <span class="sy0">.</span> <span class="st0">&#39; &#39;</span> <span class="sy0">.</span> <span class="re1">$database</span> <span class="sy0">.</span> <span class="st0">&#39;_test&#39;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</li>
</ol>
</div>
<p>It would be very interesting to get more information about which measures Baron advocates for detecting a production system. We have configuration settings for our applications which also defines if this is a development or production system, in addition to the fact that our testing code only touches databases which end in _test.</p>
]]></content:encoded>
			<wfw:commentRss>http://e-mats.org/2008/08/unit-testing-code-which-uses-a-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
