<?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>Matthew Turland &#187; Testing</title>
	<atom:link href="http://matthewturland.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewturland.com</link>
	<description></description>
	<lastBuildDate>Tue, 24 Jan 2012 04:03:47 +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>Process Isolation in PHPUnit</title>
		<link>http://matthewturland.com/2010/08/19/process-isolation-in-phpunit/</link>
		<comments>http://matthewturland.com/2010/08/19/process-isolation-in-phpunit/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 02:40:28 +0000</pubDate>
		<dc:creator>Matthew Turland</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://matthewturland.com/?p=688</guid>
		<description><![CDATA[I was recently writing a unit test for an autoloader when I came across a somewhat unintuitive behavior in PHPUnit. One requirement of the test suite was that some test methods had to be run in a separate process since class declarations reside in the global scope and persist until the process terminates. So, I [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently writing a unit test for an <a title="PHP: Autoloading Classes - Manual" href="http://php.net/manual/en/language.oop5.autoload.php">autoloader</a> when I came across a somewhat unintuitive behavior in <a title="PHPUnit" href="http://www.phpunit.de">PHPUnit</a>.</p>
<p>One requirement of the test suite was that some test methods had to be run in a separate process since class declarations reside in the global scope and persist until the process terminates. So, I slapped a <code><a title="Appendix B. Annotations" href="http://www.phpunit.de/manual/3.4/en/appendixes.annotations.html#appendixes.annotations.runInSeparateProcess">@runInSeparateProcess</a></code> annotation in the docblock of a test method with that requirement, ran the test suite&#8230; and watched that test method fail because the class was still being declared.</p>
<p>It took some head-scratching and tracing through the source of PHPUnit itself to figure out what was going on. When you run the <code>phpunit</code> executable, it&#8217;s actually instantiating <code>PHPUnit_TextUI_TestRunner</code>. The eventual result of this is that the <code>run()</code> method inherited by your subclass of <code>PHPUnit_Framework_TestCase</code> is called.</p>
<p>Depending on the value of the also-inherited <code>$preserveGlobalState</code> instance property, which can be set via the <code>setPreserveGlobalState()</code> method, multiple measures are undertaken to preserve the state of the current process. One such measure is including files for all the classes currently defined in that process, which is what was tripping me up because <code>$preserveGlobalState</code> has a default value of <code>true</code>.</p>
<p><code>$preserveGlobalState</code> must contain its intended value <em>before</em> the <code>run()</code> method is called. The easiest way that I&#8217;ve found to facilitate this is to override the <code>run()</code> method in your subclass, call <code>setPreserveGlobalState()</code> there, then call the parent class implementation of <code>run()</code>. I&#8217;ve included a code sample below to illustrate this.</p>
<pre class="brush: php; title: ; notranslate">class MyTestCase extends PHPUnit_Framework_TestCase
{
    public function run(PHPUnit_Framework_TestResult $result = NULL)
    {
        $this-&gt;setPreserveGlobalState(false);
        return parent::run($result);
    }
}</pre>
<p>So, if you try to use the <code>@runInSeparateProcess</code> or <code>@runTestsInSeparateProcesses</code> annotations that PHPUnit offers, be aware that the global state will be preserved by default. You will need to explicitly set it to not be so if running tests in separate processes is to have the effect that you are probably intending.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewturland.com/2010/08/19/process-isolation-in-phpunit/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Database Testing with PHPUnit and MySQL</title>
		<link>http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/</link>
		<comments>http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 16:00:47 +0000</pubDate>
		<dc:creator>Matthew Turland</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://matthewturland.com/?p=49</guid>
		<description><![CDATA[Update 2012/01/15: I finally got around to submitting a patch to document this feature in the PHP manual. Sebastian has merged it, so it will hopefully be available in the online manual soon. Update #2 2012/01/23: I got around to checking the online version of the manual and the current build includes my patch. Enjoy. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update</strong> 2012/01/15: I finally got around to submitting a patch to document this feature in the PHP manual. Sebastian has <a title="#45: Added a section on MySQL XML datasets to the Database Testing chapter by elazar for sebastianbergmann/phpunit-documentation - Pull Request - GitHub" href="https://github.com/sebastianbergmann/phpunit-documentation/pull/45">merged it</a>, so it will hopefully be available in the online manual soon.</p>
<p><strong>Update #2</strong> 2012/01/23: I got around to checking the online version of the manual and the current build <a title="Chapter 8. Database Testing" href="http://www.phpunit.de/manual/current/en/database.html#mysql-xml-dataset">includes my patch</a>. Enjoy.</p>
<p>I recently made a contribution to the <a title="PHPUnit" href="http://www.phpunit.de/">PHPUnit</a> project that I thought I&#8217;d take a blog post to discuss. One of the extensions bundled with PHPUnit adds support for <a title="Chapter 9. Database Testing" href="http://www.phpunit.de/manual/3.4/en/database.html">database testing</a>. This extension was contributed by <a title="Digital Sandwich" href="http://www.ds-o.com/">Mike Lively</a> and is a port of the <a title="DbUnit - Core Components" href="http://www.dbunit.org/components.html">DbUnit</a> extension for the <a title="Welcome to JUnit.org! | JUnit.org" href="http://www.junit.org/">JUnit</a> Java unit testing framework. If you&#8217;re interested in learning more about database unit testing, check out <a title="Testing PHP/MySQL Applications with PHPUnit/DbUnit  - Sebastian Bergmann" href="http://sebastian-bergmann.de/archives/773-Testing-PHPMySQL-Applications-with-PHPUnitDbUnit.html">this presentation</a> by <a title="Sebastian Bergmann  - Sebastian Bergmann" href="http://sebastian-bergmann.de/">Sebastian Bergmann</a> on the subject.</p>
<p>One of the major components of both extensions is the data set. Database unit tests involve loading a seed data set into a database, executing code that performs an operation on that data set such as deleting a record, and then checking the state of the data set to confirm that the operation had the desired effect. DbUnit supports <a title="DbUnit - Core Components" href="http://www.dbunit.org/components.html">multiple formats</a> for seed data sets. The PHPUnit Database extension includes <a title="Chapter 9. Database Testing" href="http://www.phpunit.de/manual/3.4/en/database.html#database.datasets">support</a> for DbUnit&#8217;s XML and flat XML formats plus <acronym title="Comma Separated Values">CSV</acronym> format as well.</p>
<p>If you&#8217;re using <a title="MySQL ::  The world's most popular open source database" href="http://www.mysql.com/">MySQL</a> as your database, CSV has been the only format supported by both the <a title="MySQL ::   MySQL 5.0 Reference Manual :: 4.5.4 mysqldump — A Database Backup Program" href="http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html#option_mysqldump_fields">mysqldump</a> utility and the PHPUnit Database extension up to this point. My contribution adds support for its <a title="MySQL ::   MySQL 5.0 Reference Manual :: 4.5.4 mysqldump — A Database Backup Program" href="http://dev.mysql.com/doc/refman/5.0/en/mysqldump.html#option_mysqldump_xml">XML format</a> to the extension. While this support was developed to work in the PHPUnit 3.4.x branch, it won&#8217;t be available in a stable release until 3.5.0. In the meantime, this is how you can use it now.</p>
<ol>
<li>Go to the <a title="Commit fad913fd84720f889e1d3415e775f68304e76f52 to elazar's phpunit - GitHub" href="http://github.com/sebastianbergmann/phpunit/commit/fad913fd84720f889e1d3415e775f68304e76f52">commit</a> on Github and apply the additions and modifications included in it to your PHPUnit installation.</li>
<li>From a shell, get your XML seed data set and store it in a location accessible to your unit test cases.
<pre class="brush: bash; title: ; notranslate">mysqldump --xml -t -u username -p database &gt; seed.xml</pre>
</li>
<li>Create a test case class that extends PHPUnit_Extensions_Database_TestCase. Implement getConnection() and getDataSet() as per the documentation where the latter will include a method call to create the data set from the XML file as shown below.
<pre class="brush: php; title: ; notranslate">$dataSet = $this-&gt;createMySQLXMLDataSet('/path/to/seed.xml');</pre>
</li>
<li>At this point, you can execute operations on the database to get it to its expected state following a test, produce an XML dump of the database in that state, and then compare that dump to the actual database contents in a test method to confirm that the two are equal.
<pre class="brush: php; title: ; notranslate">$expected = $this-&gt;createMySQLXMLDataSet('/path/to/expected.xml');
$actual = new PHPUnit_Extension_Database_DataSet_QueryDataSet($this-&gt;getConnection());
// Specify a SELECT query as the 2nd parameter here to limit the data set, else the entire table is used
$actual-&gt;addTable('tablename');
$this-&gt;assertDataSetsEqual($expected, $actual);</pre>
</li>
</ol>
<p>That&#8217;s it! Hopefully this proves useful to <a title="Twitter / Trevor Morse: @elazar OMG, yes! I've bee ..." href="http://twitter.com/trevor_morse/status/7239323093">someone else</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewturland.com/2010/01/04/database-testing-with-phpunit-and-mysql/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Unit Tests and Code Coverage with PHPT</title>
		<link>http://matthewturland.com/2008/05/23/unit-tests-and-code-coverage-with-phpt/</link>
		<comments>http://matthewturland.com/2008/05/23/unit-tests-and-code-coverage-with-phpt/#comments</comments>
		<pubDate>Fri, 23 May 2008 18:24:03 +0000</pubDate>
		<dc:creator>Matthew Turland</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Kubuntu]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[My initial experiences with unit testing and PHP were with PHPUnit. While it&#8217;s a great tool and I have to give kudos to Sebastian for contributing so much to its development, I&#8217;ve come to appreciate the simplicity of PHPT tests. Recently, I wrote some for a project and realized that I wasn&#8217;t aware of how [...]]]></description>
			<content:encoded><![CDATA[<p>My initial experiences with unit testing and PHP were with <a title="PHPUnit" href="http://www.phpunit.de/">PHPUnit</a>. While it&#8217;s a great tool and I have to give kudos to <a title="Sebastian Bergmann" href="http://sebastian-bergmann.de">Sebastian</a> for contributing so much to its development, I&#8217;ve come to appreciate the simplicity of <a title="Writing Tests [PHP-QAT: Quality Assurance Team" href="http://qa.php.net/write-test.php">PHPT</a> tests. Recently, I wrote some for a project and realized that I wasn&#8217;t aware of how to generate code coverage reports. Many thanks to the very helpful patrons in the <a title="PEAR :: Support" href="http://pear.php.net/support/">#pear</a> channel on EFNet for helping me to get this working.</p>
<p>First, the stock Kubuntu installation of PEAR is version 1.6.1. Their first recommendation was to upgrade to 1.7.2, which was easy enough:</p>
<pre>sudo apt-get install php-pearpear update-channelspear upgrade-all</pre>
<p>Next, <a title="PEAR :: Bug #13958 :: When a phpt tests exit() or die() xdebug coverage is not generated" href="http://pear.php.net/bugs/bug.php?id=13958">PEAR bug 13958</a> can prevent code coverage reports from being generated properly. This bug has an available patch, which you can apply by doing the following:</p>
<pre>cd /usr/local/share/PEARwget -c <a href="http://pear.php.net/bugs/bug.php?id=13958&amp;edit=12&amp;patch=phpt_coverage_bug&amp;revision=1211459864">http://pear.php.net/bugs/...</a>patch PEAR/RunTest.php phpt_coverage_bug.patch.txt</pre>
<p>Now to install PHPUnit. I ran into an odd issue here where following the <a title="Chapter 3. Installing PHPUnit" href="http://www.phpunit.de/manual/3.2/en/installation.html">installation documentation</a> resulted in PHPUnit 1.3.3 stable being installed, which is obviously a fairly old version. Even pear clear-cache didn&#8217;t seem to solve the issue. I checked to confirm that PEAR was using PHP 5. The only way I was able to get around this was to explicitly specify the latest version.</p>
<pre>sudo pear install phpunit/phpunit-3.2.19</pre>
<p>The current version of PHPUnit, 3.2.19, also has bugs <a title="#482 (phpt coverage broken) - PHPUnit - Trac" href="http://www.phpunit.de/ticket/482" class="broken_link">482</a>, <a title="#483 (warnings when phpt generates no coverage file) - PHPUnit - Trac" href="http://www.phpunit.de/ticket/483" class="broken_link">483</a>, and <a title="#484 (PhptTestCase does not implement SelfDescribing) - PHPUnit - Trac" href="http://www.phpunit.de/ticket/484" class="broken_link">484</a> related to generating PHPT code coverage reports. I had to apply the patch for bug 482 to fix issues I was having, but the others may also be necessary depending on the code being tested. These issues should be fixed when PHPUnit 3.2.20 is released. In the meantime, if you want to avoid manually applying patches and you&#8217;re feeling adventurous, you can use an <a title="/phpunit/trunk - PHPUnit - Trac" href="http://github.com/sebastianbergmann/phpunit">git checkout</a>.</p>
<p>As far as I can tell, PHPUnit itself does not contain a runner for its PHPT suite extension. However, there is <a title="[cvs] View of /pear/Console_CommandLine/tests/AllTests.php" href="http://svn.php.net/viewvc/pear/packages/Console_CommandLine/trunk/tests/AllTests.php?revision=260092&amp;view=markup">a runner script</a> to do this in PEAR CVS. Download the script to the directory containing your PHPT test files and execute it with a command similar to this.</p>
<pre>phpunit --coverage-html ../Tests-Results \    Console_CommandLine_AllTests AllTests.php</pre>
<p>In the case of this particular command, the Tests-Results directory parallel to the directory containing your PHPT files should now contain an index.html file with your test results. There you have it. Happy testing folks!</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewturland.com/2008/05/23/unit-tests-and-code-coverage-with-phpt/feed/</wfw:commentRss>
		<slash:comments>4</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! -->
