<?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>Andrew Havens Blog &#187; PHPUnit</title>
	<atom:link href="http://blog.andrewhavens.com/tag/phpunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.andrewhavens.com</link>
	<description></description>
	<lastBuildDate>Mon, 14 Dec 2009 19:41:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting started with Zend_Test &#8211; Step 4: Testing your Zend Framework Controllers</title>
		<link>http://blog.andrewhavens.com/2009/03/13/getting-started-with-zend_test-step-4-testing-your-zend-framework-controllers/</link>
		<comments>http://blog.andrewhavens.com/2009/03/13/getting-started-with-zend_test-step-4-testing-your-zend-framework-controllers/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 18:56:26 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Test]]></category>

		<guid isPermaLink="false">http://blog.andrewhavens.com/?p=100</guid>
		<description><![CDATA[ Finally! We can start testing our Controllers! I will assume that your application has the same directory structure as the Zend Framework Quick Start tutorial. Here&#8217;s an overview of what our directory structure will look like:
/myApp/application/controllers
/myApp/tests/application/controllers
Our tests directory will mirror our application. We might also include a &#8216;library&#8217; directory in the tests directory so [...]]]></description>
			<content:encoded><![CDATA[<!-- Generated by Digg Digg plugin, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/
	--><div style='float:right'><table > <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http%3A%2F%2Fblog.andrewhavens.com%2F2009%2F03%2F13%2Fgetting-started-with-zend_test-step-4-testing-your-zend-framework-controllers%2F&amp;t=Getting+started+with+Zend_Test+-+Step+4%3A+Testing+your+Zend+Framework+Controllers&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td></table></div><p>Finally! We can start testing our Controllers! I will assume that your application has the same directory structure as the <a href="http://framework.zend.com/docs/quickstart">Zend Framework Quick Start tutorial</a>. Here&#8217;s an overview of what our directory structure will look like:</p>
<p>/myApp/application/controllers<br />
/myApp/tests/application/controllers</p>
<p>Our tests directory will mirror our application. We might also include a &#8216;library&#8217; directory in the tests directory so we can test our custom library components. For now, we&#8217;ll just keep it simple and test our controllers.</p>
<p>Now, we&#8217;ll create a file that will take care of auto loading. Create a new file called &#8216;loader.php&#8217; in you tests directory. All of our tests will require this file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APPLICATION_PATH'</span><span style="color: #339933;">,</span> <span style="color: #990000;">realpath</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../application/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span> APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/../library'</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/models'</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
APPLICATION_PATH <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/forms'</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span>
<span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;Zend/Loader.php&quot;</span><span style="color: #339933;">;</span>
Zend_Loader<span style="color: #339933;">::</span><span style="color: #004000;">registerAutoload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now let&#8217;s create a simple controller test. We&#8217;ll call this &#8216;IndexControllerTest.php&#8217; and put it in our tests/application/controllers directory. We just need to set the location of our bootstrap file. When <code>Zend_Test_PHPUnit_ControllerTestCase</code> constructs, it will look for the bootstrap file that we set here.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'../loader.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> IndexControllerTest <span style="color: #000000; font-weight: bold;">extends</span> Zend_Test_PHPUnit_ControllerTestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$bootstrap</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'../../application/bootstrap.php'</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> testIndexAction<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><span style="color: #004000;">dispatch</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/index'</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;">assertController</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</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;">assertAction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'index'</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;">assertResponseCode</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s it! All we have to do is run this file with our PHPUnit command line interface (that we used in the last tutorial).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.andrewhavens.com/2009/03/13/getting-started-with-zend_test-step-4-testing-your-zend-framework-controllers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting started with Zend_Test &#8211; Step 3: Make sure PHPUnit is ready for testing</title>
		<link>http://blog.andrewhavens.com/2009/03/11/getting-started-with-zend_test-step-3-make-sure-phpunit-is-ready-for-testing/</link>
		<comments>http://blog.andrewhavens.com/2009/03/11/getting-started-with-zend_test-step-3-make-sure-phpunit-is-ready-for-testing/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 03:56:24 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[PEAR]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHPUnit]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zend_Test]]></category>

		<guid isPermaLink="false">http://blog.andrewhavens.com/?p=95</guid>
		<description><![CDATA[ Now that we&#8217;ve installed PEAR and PHPUnit, we can verify that PHPUnit is working correctly by writing our first test.
Since we installed PHPUnit through PEAR, and we have properly configured our include_path variable in our php.ini file, we can create our test files anywhere and PHP will know where to look to find PHPUnit. [...]]]></description>
			<content:encoded><![CDATA[<!-- Generated by Digg Digg plugin, 
    Author : Yong Mook Kim
    Website : http://www.mkyong.com/blog/digg-digg-wordpress-plugin/
	--><div style='float:right'><table > <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http%3A%2F%2Fblog.andrewhavens.com%2F2009%2F03%2F11%2Fgetting-started-with-zend_test-step-3-make-sure-phpunit-is-ready-for-testing%2F&amp;t=Getting+started+with+Zend_Test+-+Step+3%3A+Make+sure+PHPUnit+is+ready+for+testing&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td></table></div><p>Now that we&#8217;ve installed PEAR and PHPUnit, we can verify that PHPUnit is working correctly by writing our first test.</p>
<p>Since we installed PHPUnit through PEAR, and we have properly configured our include_path variable in our php.ini file, we can create our test files anywhere and PHP will know where to look to find PHPUnit. So I will create a new file in my /Users/andrew/Sites directory and call it FoobarTest.php with the following contents:</p>
<pre class="brush: php;">

&lt;?php

class FoobarTest extends PHPUnit_Framework_TestCase {

public function testFoobar() {
$this-&gt;fail();
}

}
</pre>
<p>By writing our function name to start with &#8220;test&#8221;, PHPUnit will know to run this function as a test. Typically you will want to name your test functions with names similar to the functions that you are testing, preceded by &#8220;test&#8221;.</p>
<p>Now let&#8217;s run our test!</p>
<pre class="brush: cpp;">

cd /Users/andrew/Sites
phpunit FoobarTest.php
</pre>
<p>You should see something like the following:</p>
<pre class="brush: cpp;">

PHPUnit 3.3.15 by Sebastian Bergmann.

F

Time: 0 seconds

There was 1 failure:

1) testFoobar(FoobarTest)
/Users/andrew/Sites/FoobarTest.php:6

FAILURES!
Tests: 1, Assertions: 0, Failures: 1.
</pre>
<p>Yay! It works! &#8220;But wait,&#8221; you say, &#8220;it says failures. Why is this a good thing?&#8221; Test-driven development is all about writing tests first. Remember this saying: Red, Green, Refactor. Start by writing the test, which will fail, of course, because you haven&#8217;t written any code yet! Then write the code until your test passes. Then STOP. There&#8217;s no need to keep writing. The code is finished. You know it works because you have the test to prove it. Tests are written to fail. Code is written to fix the tests.</p>
<p>However, this is a poor example of a test, so let&#8217;s write a real one.</p>
<p>Let&#8217;s start by thinking about what we want to build. How about we build a class called Foobar (to make things easy since we already have a FoobarTest) that has a function called getMessage(). However, getMessage() doesn&#8217;t return a string like we might expect, it returns an array. A test for that might look like the following. Also note that our function name is a little more verbose. Tests can serve as a sort of documentation. Think of underscores as commas and name your functions to explain what happens in the test.</p>
<pre class="brush: php;">

&lt;?php

class FoobarTest extends PHPUnit_Framework_TestCase {

public function testGetMessage_ReturnsAnArray() {
$foobar = new Foobar();
$message = $foobar-&gt;getMessage();
$this-&gt;assertTrue(is_array($message));
}

}
</pre>
<p>Now if we run our test again, we should see the following:</p>
<pre class="brush: cpp;">

PHPUnit 3.3.15 by Sebastian Bergmann.

PHP Fatal error:  Class 'Foobar' not found in /Users/andrew/Sites/FoobarTest.php on line 6

Fatal error: Class 'Foobar' not found in /Users/andrew/Sites/FoobarTest.php on line 6
</pre>
<p>Now we have an error that says PHPUnit can&#8217;t find our Foobar class. Well that&#8217;s because we haven&#8217;t created it yet! So create a new file in the same directory as our test, name it Foobar.php, and give it the following contents:</p>
<pre class="brush: php;">

&lt;?php

class Foobar {

}
</pre>
<p>Next, let&#8217;s update our test so it knows where to find our new class:</p>
<pre class="brush: php;">

&lt;?php

require_once('Foobar.php');

class FoobarTest extends PHPUnit_Framework_TestCase {

//...
</pre>
<p>That&#8217;s all we need to do. Remember, all we are trying to do is get the test to pass. Right now the test tells us that we need to create a Foobar class so that&#8217;s what we&#8217;ve done. Let&#8217;s run our test again and see what it says.</p>
<pre class="brush: cpp;">

HPUnit 3.3.15 by Sebastian Bergmann.

PHP Fatal error:  Call to undefined method Foobar::getMessage() in /Users/andrew/Sites/FoobarTest.php on line 9

Fatal error: Call to undefined method Foobar::getMessage() in /Users/andrew/Sites/FoobarTest.php on line 9
</pre>
<p>Hooray! We no longer have the same message as before. Now we need to create our getMessage() function in our Foobar class.</p>
<pre class="brush: php;">

&lt;?php

class Foobar {

public function getMessage() {

}

}
</pre>
<p>Run our test again and we should see another change:</p>
<pre class="brush: cpp;">

PHPUnit 3.3.15 by Sebastian Bergmann.

F

Time: 0 seconds

There was 1 failure:

1) testGetMessage_ReturnsAnArray(FoobarTest)
Failed asserting that &lt;boolean:false&gt; is true.
/Users/andrew/Sites/FoobarTest.php:10

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
</pre>
<p>getMessage() doesn&#8217;t return anything, so let&#8217;s change that:</p>
<pre class="brush: php;">

&lt;?php

class Foobar {

public function getMessage() {
return array();
}

}
</pre>
<p>That should be enough for us to get our test to pass. Let&#8217;s see if it works.</p>
<pre class="brush: cpp;">

PHPUnit 3.3.15 by Sebastian Bergmann.

.

Time: 0 seconds

OK (1 test, 1 assertion)
</pre>
<p>It works! We&#8217;re done. I think you get the picture. Let&#8217;s move on to some actual controller tests.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.andrewhavens.com/2009/03/11/getting-started-with-zend_test-step-3-make-sure-phpunit-is-ready-for-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
