<?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; PHPUnit</title>
	<atom:link href="http://matthewturland.com/tag/phpunit/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>PHPUnit and Xdebug on Ubuntu Karmic</title>
		<link>http://matthewturland.com/2010/01/03/phpunit-and-xdebug-on-ubuntu-karmic/</link>
		<comments>http://matthewturland.com/2010/01/03/phpunit-and-xdebug-on-ubuntu-karmic/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 16:00:20 +0000</pubDate>
		<dc:creator>Matthew Turland</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Xdebug]]></category>

		<guid isPermaLink="false">http://matthewturland.com/?p=62</guid>
		<description><![CDATA[This is just a quick post to advise anyone who may be using PHPUnit and Xdebug together on Ubuntu Karmic. If you try to upgrade to PHPUnit 3.4.6 and you&#8217;re using the php5-xdebug Ubuntu package (which is Xdebug 2.0.4), you may get output that looks like this: There are two ways to deal with this [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post to advise anyone who may be using <a title="PHPUnit" href="http://www.phpunit.de/">PHPUnit</a> and <a title="Xdebug - Debugger and Profiler Tool for PHP" href="http://xdebug.org/">Xdebug</a> together on <a title="Ubuntu Home Page | Ubuntu" href="http://www.ubuntu.com/">Ubuntu Karmic</a>. If you try to upgrade to PHPUnit 3.4.6 and you&#8217;re using the <a title="Ubuntu -- Details of package php5-xdebug in karmic" href="http://packages.ubuntu.com/karmic/php5-xdebug">php5-xdebug</a> Ubuntu package (which is Xdebug 2.0.4), you may get output that looks like this:</p>
<pre class="brush: plain; title: ; notranslate">$ sudo pear upgrade phpunit/PHPUnit
Did not download optional dependencies: pear/Image_GraphViz, pear/Log, use --alldeps to download automatically
phpunit/PHPUnit can optionally use package &quot;pear/Image_GraphViz&quot; (version &gt;= 1.2.1)
phpunit/PHPUnit can optionally use package &quot;pear/Log&quot;
phpunit/PHPUnit can optionally use PHP extension &quot;pdo_sqlite&quot;
phpunit/PHPUnit requires PHP extension &quot;xdebug&quot; (version &gt;= 2.0.5), installed version is 2.0.4
No valid packages found
upgrade failed</pre>
<p>There are two ways to deal with this situation. First off, note that the newer Xdebug 2.0.5 version includes <a title="Xdebug: Updates" href="http://xdebug.org/updates.php">several bugfixes</a> including one related to code coverage reporting. That said, if you still want to continue using the php5-xdebug package anyway, you can force the upgrade by having the PEAR installer ignore dependencies like so:</p>
<pre class="brush: bash; title: ; notranslate">sudo pear upgrade -n phpunit/PHPUnit</pre>
<p>The other method involves installing Xdebug 2.0.5. First, if you have the php5-xdebug package, remove it.</p>
<pre class="brush: bash; title: ; notranslate">sudo apt-get remove php5-xdebug</pre>
<p>Next, use the PECL installer to install Xdebug. This requires that you have the php5-dev package installed so that the extension can be compiled locally.</p>
<pre class="brush: bash; title: ; notranslate">sudo apt-get install php5-dev
sudo pecl install xdebug</pre>
<p>At this point, create the file /etc/php5/conf.d/xdebug.ini if it doesn&#8217;t already exist and populate it with these contents:</p>
<pre class="brush: plain; title: ; notranslate">zend_extension=/usr/lib/php5/20060613/xdebug.so</pre>
<p>Then bounce Apache so that the new extension will be loaded.</p>
<pre class="brush: bash; title: ; notranslate">sudo apache2ctl restart</pre>
<p>That&#8217;s it. Hope someone finds this helpful.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewturland.com/2010/01/03/phpunit-and-xdebug-on-ubuntu-karmic/feed/</wfw:commentRss>
		<slash:comments>5</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! -->
