<?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>code-diesel &#187; testing</title>
	<atom:link href="http://www.codediesel.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Test dependencies in PHPUnit 3.4</title>
		<link>http://www.codediesel.com/testing/test-dependencies-in-phpunit/</link>
		<comments>http://www.codediesel.com/testing/test-dependencies-in-phpunit/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 08:54:33 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[testing]]></category>
		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/sql/test-dependencies-in-phpunit/</guid>
		<description><![CDATA[writing dependent tests in phpunit 3.4]]></description>
			<content:encoded><![CDATA[<p><a target="_blank" href="http://www.phpunit.de/">PHPUnit</a> has become the de-facto standard for unit testing PHP code. Now in version 3.4, it has added many new and interesting features to its repertoire.</p>
<h4>Dependencies in PHPUnit tests</h4>
<p>PHPUnit 3.4 now supports dependencies between different test methods. It allows you to execute a particular test <em>ONLY IF</em> the test that it depends on executes successfully. Take the following example (Listing 1.) where we test a <a href="http://www.codediesel.com/php/linked-list-in-php/">linked-list class</a> I developed earlier.<br />
<span id="more-2217"></span></p>
<h4>A simple unit-test with PHPUnit</h4>
<p>The test basically creates a link-list with 100 nodes, and tests a few class methods &#8211; reverseList, deleteFirstNode, deleteLastNode. As you can see the linklist creation part and method tests are combined in the single test method <em>testLinkList()</em>. </p>
<p>The primary focus here is on testing the three class functions. There is nothing wrong with this. But what if the linklist creation part itself fails before the other tests are conducted? Of course we can add another test to check that the links are being created correctly, but that mixes up the two parts.</p>
<p><b>Listing 1.</b></p>

<div class="wp_codebox"><table><tr id="p22171"><td class="code" id="p2217code1"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'linklist.class.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkListTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_nNodes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</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> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #009933; font-style: italic;">/* Linklist creation block */</span>
        <span style="color: #000088;">$theList</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>  
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertLast</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #009933; font-style: italic;">/* 
            Linklist methods testing block :
&nbsp;
            $theList-&gt;totalNodes() is a public function 
            in the 'linklist' class that returns the total
            number of nodes in the link-list.
        */</span>
&nbsp;
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reverseList</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</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: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteFirstNode</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</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: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteLastNode</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Splitting up tests</h4>
<p>A better way is to split the linklist creation part and class method testing part into two separate test functions as below (Listing 2). Here the linklist is created in the &#8216;testCreateList()&#8217; method and the testing of the class methods is done in the &#8216;testLinkList()&#8217; method. This allows us to keep the two testing blocks separate.</p>
<p><b>Listing 2.</b></p>

<div class="wp_codebox"><table><tr id="p22172"><td class="code" id="p2217code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'linklist.class.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkListTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_nNodes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</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> testCreateList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertLast</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testCreateList
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reverseList</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</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: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteFirstNode</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</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: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteLastNode</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The important point to note here is that we have specified that the &#8216;<em>testLinkList</em>&#8216; method depends on the &#8216;<em>testCreateList</em>&#8216; method, with a @depends annotation. This tells PHPUnit that if the &#8216;<em>testCreateList</em>&#8216;  test fails then the test dependent on it, &#8216;<em>testLinkList</em>&#8216;, will have to be skipped, which is a logical way to go.</p>

<div class="wp_codebox"><table><tr id="p22173"><td class="code" id="p2217code3"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @depends testCreateList
 */</span></pre></td></tr></table></div>

<h4>Producers &#038; Consumers in a Test</h4>
<p>First some definitions:</p>
<blockquote><p>
a. A producer is a test method that yields its unit under test as return value.<br />
b. A consumer is a test method that depends on one or more producers and their return values.
</p></blockquote>
<p>What it means is that a &#8216;Producer&#8217; is a test method that returns some data that will be used by a &#8216;Consumer&#8217; test method. In our example the &#8216;<em>testCreateList</em>&#8216; is a Producer and the &#8216;<em>testLinkList</em>&#8216; is the Consumer.  The &#8216;<em>testCreateList</em>&#8216; method returns a linklist object which is then received by the dependent method &#8216;<em>testLinkList</em>&#8216;. So if for some reason the &#8216;<em>testCreateList</em>&#8216; fails to return some data then the test that depends on it,  &#8216;<em>testLinkList</em>&#8216;, will have to be skipped, as it has nothing to work with.</p>
<p>Bear in mind that it does not mean that every test method that depends on some other method has to accept some arguments from the method it is dependent on. Lets breakup the above example further (Listing 3). Here we have added one more test method, &#8216;<em>testNodesDefined</em>&#8216;, which makes sure that the &#8216;$_nNodes&#8217; variable is not null. The &#8216;<em>testCreateList</em>&#8216; method is dependent on this method, so that if the &#8216;$_nNodes&#8217; is null then the &#8216;<em>testCreateList</em>&#8216; is skipped, which causes the test &#8216;<em>testLinkList</em>&#8216; to be skipped.</p>
<p><b>Listing 3.</b></p>

<div class="wp_codebox"><table><tr id="p22174"><td class="code" id="p2217code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'linklist.class.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> LinkListTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/* How many nodes to create */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_nNodes</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</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> testNodesDefined<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;">assertNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testNodesDefined
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertLast</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testCreateList
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reverseList</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</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: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteFirstNode</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</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: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">deleteLastNode</span><span style="color: #009900;">&#40;</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;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$theList</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">totalNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p> A output from a failed test, when we set  &#8216;$_nNodes&#8217; to null, is shown below.<br />
<b>Listing 4.</b></p>

<div class="wp_codebox"><table><tr id="p22175"><td class="code" id="p2217code5"><pre class="text" style="font-family:monospace;">D:\localhost\datastructures&gt;phpunit --verbose LinkListTest
PHPUnit 3.4.2 by Sebastian Bergmann.
&nbsp;
LinkListTest
FSS
&nbsp;
Time: 0 seconds
&nbsp;
There was 1 failure:
&nbsp;
1) LinkListTest::testNodesDefined
Failed asserting that &lt;null&gt; is not null.
&nbsp;
D:\localhost\test\datastructures\LinkListTest.php:13
&nbsp;
There were 2 skipped tests:
&nbsp;
1) LinkListTest::testCreateList
This test depends on &quot;LinkListTest::testNodesDefined&quot; to pass.
&nbsp;
&nbsp;
2) LinkListTest::testLinkList
This test depends on &quot;LinkListTest::testCreateList&quot; to pass.
&nbsp;
&nbsp;
FAILURES!
Tests: 1, Assertions: 2, Failures: 1, Skipped: 2.
&nbsp;
D:\localhost\test\datastructures&gt;</pre></td></tr></table></div>

<blockquote><p>
Note: The tests are executed in the sequence in which you wrote them. So take care while writing dependent tests. For example, sequencing test methods as below will not work.
</p></blockquote>

<div class="wp_codebox"><table><tr id="p22176"><td class="code" id="p2217code6"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testNodesDefined<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;">assertNotNull</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_nNodes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testCreateList
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testLinkList<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theList</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">..</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @depends testNodesDefined
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testCreateList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #339933;">..</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>What all of this has brought us</h4>
<p>Testing a complex class in a single humongous test method is not wrong, but can create a confusing mess of &#8216;asserts&#8217;. Splitting a single test method into multiple dependent tests can help you segregate your test methods by functionality which in turn will get you to quickly localize defects in your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/testing/test-dependencies-in-phpunit/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Detecting duplicate code in PHP files</title>
		<link>http://www.codediesel.com/tools/detecting-duplicate-code-in-php-files/</link>
		<comments>http://www.codediesel.com/tools/detecting-duplicate-code-in-php-files/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 04:33:25 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[testing]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=625</guid>
		<description><![CDATA[Duplicated code in projects is a frequent thing and also the one ripe for factoring out in a new class or function. Cut/Paste coding is a common development practice among programmers, a lot of which can lead to code size increase and maintenance nightmares. PHPCPD (php copy paste detector) is a PEAR tool that makes [...]]]></description>
			<content:encoded><![CDATA[<p>Duplicated code in projects is a frequent thing and also the one ripe for factoring out in a new class or function. Cut/Paste coding is a common development practice among programmers, a lot of which can lead to code size increase and maintenance nightmares. <a target="_blank" href="http://github.com/sebastianbergmann/phpcpd/tree/master">PHPCPD</a> (php copy paste detector) is a PEAR tool that makes it easier to detect duplicate code in php projects. Below is a short tutorial on the PHPCPD package.<br />
<span id="more-625"></span></p>
<p><strong>1. Installing phpcpd</strong><br />
We will be using the PEAR installer for this purpose. First the PEAR channel that is used to distribute phpcpd needs to be registered with the local PEAR environment. This tells PEAR from where the install files should be downloaded.</p>

<div class="wp_codebox"><table><tr id="p62513"><td class="code" id="p625code13"><pre class="dos" style="font-family:monospace;">c:\&gt; pear channel-discover pear.phpunit.de
Adding Channel &quot;pear.phpunit.de&quot; succeeded
Discovery of channel &quot;pear.phpunit.de&quot; succeeded</pre></td></tr></table></div>

<p>After this is done the PEAR installer is ready to install phpcpd.</p>

<div class="wp_codebox"><table><tr id="p62514"><td class="code" id="p625code14"><pre class="dos" style="font-family:monospace;">c:\&gt; pear install phpunit/phpcpd
downloading phpcpd-1.1.1.tgz ...
Starting to download phpcpd-1.1.1.tgz <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span>,078 bytes<span style="color: #66cc66;">&#41;</span>
.....done: <span style="color: #cc66cc;">8</span>,078 bytes
install ok: channel://pear.phpunit.de/phpcpd-1.1.1</pre></td></tr></table></div>

<p>Post installation you will find the PHPCPD source files in the PEAR directory.</p>
<p><strong>2. Running your first check.</strong><br />
Here is our first check on the <i>admin</i> sub directory of a project with the results. You can check for duplicate code in a individual file or a directory.</p>

<div class="wp_codebox"><table><tr id="p62515"><td class="code" id="p625code15"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd ./admin
phpcpd 1.1.1 by Sebastian Bergmann.
&nbsp;
Found <span style="color: #cc66cc;">3</span> exact clones with <span style="color: #cc66cc;">50</span> duplicated lines <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #cc66cc;">6</span> files:
&nbsp;
  - .messages.php:<span style="color: #cc66cc;">95</span>-<span style="color: #cc66cc;">105</span>
    .messagesgroup.php:<span style="color: #cc66cc;">112</span>-<span style="color: #cc66cc;">122</span>
&nbsp;
  - .viewschedules.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">23</span>
    .tutorbookings.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">23</span>
&nbsp;
  - .ampieexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">35</span>
    .amcolumnexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">35</span>
&nbsp;
<span style="color: #cc66cc;">0.35</span><span style="color: #33cc33;">%</span> duplicated lines out of <span style="color: #cc66cc;">14456</span> total lines of code.</pre></td></tr></table></div>

<p><strong>3. Options</strong><br />
By default phpcpd will search for a minimum of 5 identical lines and 70 identical tokens. So if there are less than 5 duplicate lines in the code or less than 70 identical tokens they will be ignored. To override this you can use the <em>&#8211;min-lines</em> and <em>&#8211;min-tokens</em> switch as below.</p>

<div class="wp_codebox"><table><tr id="p62516"><td class="code" id="p625code16"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd --min-lines <span style="color: #cc66cc;">4</span> --min-tokens <span style="color: #cc66cc;">40</span> ./admin
phpcpd 1.1.1 by Sebastian Bergmann.
&nbsp;
Found <span style="color: #cc66cc;">9</span> exact clones with <span style="color: #cc66cc;">187</span> duplicated lines <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #cc66cc;">14</span> files:
&nbsp;
  - .actionaction.updatestudent.php:<span style="color: #cc66cc;">15</span>-<span style="color: #cc66cc;">27</span>
    .actionaction.updatetutor.php:<span style="color: #cc66cc;">15</span>-<span style="color: #cc66cc;">27</span>
&nbsp;
  - .adminFunctions.php:<span style="color: #cc66cc;">91</span>-<span style="color: #cc66cc;">98</span>
    .adminFunctions.php:<span style="color: #cc66cc;">124</span>-<span style="color: #cc66cc;">131</span>
&nbsp;
  - .messages.php:<span style="color: #cc66cc;">95</span>-<span style="color: #cc66cc;">118</span>
    .messagesgroup.php:<span style="color: #cc66cc;">112</span>-<span style="color: #cc66cc;">135</span>
&nbsp;
  - .viewschedules.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">84</span>
    .tutorbookings.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">84</span>
&nbsp;
  - .viewschedules.php:<span style="color: #cc66cc;">167</span>-<span style="color: #cc66cc;">185</span>
    .tutorbookings.php:<span style="color: #cc66cc;">145</span>-<span style="color: #cc66cc;">163</span>
&nbsp;
  - .tutors.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
    .dimdim.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
&nbsp;
  - .ampieexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">44</span>
    .amcolumnexport.php:<span style="color: #cc66cc;">4</span>-<span style="color: #cc66cc;">44</span>
&nbsp;
  - .geoipgeoip.inc.php:<span style="color: #cc66cc;">236</span>-<span style="color: #cc66cc;">241</span>
    .geoipgeoip.inc.php:<span style="color: #cc66cc;">272</span>-<span style="color: #cc66cc;">277</span>
&nbsp;
  - .studentschedule.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
    .<span style="color: #b1b100; font-weight: bold;">Copy</span> of onetomanyschedule.php:<span style="color: #cc66cc;">14</span>-<span style="color: #cc66cc;">20</span>
&nbsp;
<span style="color: #cc66cc;">1.29</span><span style="color: #33cc33;">%</span> duplicated lines out of <span style="color: #cc66cc;">14457</span> total lines of code.</pre></td></tr></table></div>

<p>The report generated by phpcpd can also be exported to a <a target="_blank" href="http://pmd.sourceforge.net/">PMD-CPD</a> xml format. The following scans the <em>admin</em> directory and saves the report in the <em>projectPhpcpd.xml</em> file.</p>

<div class="wp_codebox"><table><tr id="p62517"><td class="code" id="p625code17"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd --log-pmd projectPhpcpd.xml ./admin</pre></td></tr></table></div>

<p>Most of the php source files have the .php extension and phpcpd uses this by default when comparing files. To add other extensions to the list you can use the <i>&#8211;suffixes</i> option, which takes a comma separated list of extension names.</p>

<div class="wp_codebox"><table><tr id="p62518"><td class="code" id="p625code18"><pre class="dos" style="font-family:monospace;">c:\localhost\project&gt; phpcpd --suffixes php,php5 ./admin</pre></td></tr></table></div>

<p><strong>Concluding thoughts</strong><br />
There is also a Java program called <a target="_blank" href="http://pmd.sourceforge.net/cpd.html">PMD</a> which can detect duplicate code. But the main advantage of a PEAR package is that you can integrate it in your project itself or use it with <a target="_blank" href="http://phpundercontrol.org/about.html">phpUnderControl</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/tools/detecting-duplicate-code-in-php-files/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Generating test data for MySQL</title>
		<link>http://www.codediesel.com/mysql/generating-test-data-for-mysql/</link>
		<comments>http://www.codediesel.com/mysql/generating-test-data-for-mysql/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 04:34:08 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=572</guid>
		<description><![CDATA[The most common type of web application a developer encounters are database related. But rarely is the case when you have the required data in your database during testing, making it a frequent source of frustration for programmers, who than have to manually populate the database. Having a dummy set of data is always a [...]]]></description>
			<content:encoded><![CDATA[<p>The most common type of web application a developer encounters are database related. But rarely is the case when you have the required data in your database during testing, making it a frequent source of frustration for programmers, who than have to manually populate the database. Having a dummy set of data is always a good thing.</p>
<p><span id="more-572"></span></p>
<p><a title="dummy data generator" href="http://www.generatedata.com/#generator" target="_blank">generatedata.com</a> is one such tool. Generatedata lets you specify the database schema details and generates the dummy sql data for you, with table creation and complete inserts. The data can further be exported to html, excel, xml or csv formats. The tool is available online and there is also a downloadable version of the tool if you like to modify the code or need to install it on your server or development machine.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2009/03/testdata.gif"><img class="aligncenter size-medium wp-image-576" title="testdata" src="http://www.codediesel.com/wp-content/uploads/2009/03/testdata-300x168.gif" alt="testdata" width="300" height="168" /></a></p>
<p>The tool also has an option to generate country specific data; currently limited to Canada,Netherlands,UK and the US. If you need to add other countries to the list, you have the option of modifying the code to suit your specific purpose. Even though there are many free tools available online with the same purpose, I find Generatedata to be simple to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/mysql/generating-test-data-for-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Selenium IDE Tutorial &#8211; Part 2</title>
		<link>http://www.codediesel.com/php/selenium-ide-tutorial-part-2/</link>
		<comments>http://www.codediesel.com/php/selenium-ide-tutorial-part-2/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 05:49:01 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=114</guid>
		<description><![CDATA[In part 1 of this tutorial you have seen how to write a simple web test using Selenium. In this part we will see how to use the test generated using the IDE with Selenium RC and PHPUnit. In this part we will use Delicious as our test target. I&#8217;ve already created a small Selenium [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-3476693800710463";
/* 468x60, created 6/14/10 */
google_ad_slot = "3005910052";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>In <a title="selenium tutorial part 1" href="http://www.codediesel.com/php/selenium-ide-tutorial-part-1/" target="_self">part 1</a> of this tutorial you have seen how to write a simple web test using Selenium. In this part we will see how to use the test generated using the IDE with Selenium RC and PHPUnit.<br />
<span id="more-114"></span><br />
In this part we will use Delicious as our test target. I&#8217;ve already created a small Selenium test that you can use. You can download the HTML source from <a href="http://www.codediesel.com/data/scripts/delicious.html.txt">here</a>.</p>
<p><strong>Starting up.</strong><br />
Restart the Selenium IDE. In the source tab replace the default HTML with the one from the above file. Set the Base URL to http://delicious.com/. After doing this your IDE should like below.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium221.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium221.gif" alt="selenium ide tutorial 2" title="selenium221" width="408" height="556" class="aligncenter size-full wp-image-132" /></a></p>
<p>If you are on a slow internet connection than it may help to slow the test speed.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium241.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium241.gif"  style="border: 1px solid #c0c0c0;" alt="" title="selenium241" width="295" height="114" class="aligncenter size-full wp-image-135" /></a></p>
<p>So what exactly does this test do? Simple: </p>
<p>1. First it opens the delicious home page<br />
2. Then it searches by the keyword &#8216;selenium&#8217;<br />
3. Reads the total number of bookmarks from the results page as shown below</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium23.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium23.gif" style="border: 1px solid #c0c0c0;" alt="" title="selenium tutorial" width="500" height="255" class="aligncenter size-full wp-image-133" /></a></p>
<p>Now this may not look like a test for you , but it is just an exercise in using Selenium, you will surely apply it to a good test case. Onwards&#8230;</p>
<p>Before running this test make sure that you are logged out of Delicious.<br />
Now run the selenium IDE test. After the test is successfully completed this is what you should see in the log pane.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium25.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium25.gif" alt="" title="selenium25" width="468" height="186" class="aligncenter size-full wp-image-136" /></a></p>
<p><strong>Is that all?</strong><br />
If this was all there was I wouldn&#8217;t be really writing this post. The interesting part comes now &#8211; running the same test from PHP. From the file menu select &#8216;Export Test Case As&#8230;&#8217; and as we are using PHP, select &#8216;PHP &#8211; Selenium RC&#8217;. Save the file by the name &#8216;Example.php&#8217;.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium26.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium26.gif"  style="border: 1px solid #c0c0c0;" alt="" title="selenium26" width="469" height="363" class="aligncenter size-full wp-image-137" /></a></p>
<p>This is what we will get in the &#8216;Example.php&#8217; file.</p>

<div class="wp_codebox"><table><tr id="p11422"><td class="code" id="p114code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Extensions/SeleniumTestCase.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Example <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Extensions_SeleniumTestCase
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> setUp<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;">setBrowser</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;*chrome&quot;</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;">setBrowserUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://delicious.com/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> testMyTestCase<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;">open</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</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;">type</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;homepage-searchinput&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;selenium&quot;</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;">click</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;homepage-searchsubmit&quot;</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;">waitForPageToLoad</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;30000&quot;</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;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xpath=/html/body[@id='index']/div
                         [@id='doc3']/div[@id='bd']/div
                         [@id='yui-main']/div[@id='content']/h3/div/em&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><strong>A short notice.</strong><br />
Before we proceed a few points to consider. Make sure that the file name and the class name are the same, here &#8216;Example&#8217;. Say if you change the class name to &#8216;Delicious&#8217; make sure that you also change the file name to &#8216;Delicious.php&#8217;.</p>
<p>What the code basically does is it initializes the browser to &#8216;chrome&#8217; (you can set the browser to your preference by changing to &#8216;*iexplore&#8217; or &#8216;*firefox&#8217;);  sets the baseurl to &#8216;http://delicious.com/&#8217; and runs the test.</p>
<p><strong>Whats the complex looking line!?</strong><br />
Everything may look fine to you except maybe the last line &#8211; &#8216;$this->getText&#8230;&#8217;.  The function &#8216;getText&#8217; returns the text found at the element specified in the expression. The complex looking line is an xpath expression to the results element:</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium27.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium27.gif" style="border: 1px solid #c0c0c0;" alt="" title="selenium27" width="248" height="91" class="aligncenter size-full wp-image-138" /></a></p>
<p>You don&#8217;t have to rake your brains to find an xpath to an element. An easy way is to use the <a target="_blank" href="https://addons.mozilla.org/en-US/firefox/addon/1192">XPather </a>Firefox addon to get the xpath of any element on a page. After you download and install the addon, restart Firefox, right-click on any element and from the context menu select &#8216;Show in XPather&#8217;. XPather retrieves the xpath to the specified element, which you can than use in your test code.</p>
<p>Note: This site uses &#8216;delicious&#8217; as an example, so even some minor changes made by them to their site will render the xpath invalid, throwing an &#8216;element not found&#8217; error in selenium. If this happens please notify me or you can find the new xpath using the Xpather plugin.</p>
<p><strong>Downloading and installing Selenium RC</strong><br />
Selenium RC is a Java based command line server that starts browsers and runs commands you pass from your tests.</p>
<p>1. First make sure you have a Java runtime installed on your machine.<br />
2. Download Selenium RC from <a target="_blank" href="http://selenium-rc.seleniumhq.org/download.html">here</a>.<br />
3. After extracting the files from the archive copy the &#8216;selenium-server.jar&#8217; file to any directory you feel appropriate. I copied it to my PHP installations bin directory.<br />
4. Start the Selenium RC server from the command-line by issuing the following command:</p>
<p><em>java -jar selenium-server.jar</em></p>
<p>This will start the server on port 4444.<br />
5. Now the server is ready to accept test commands from your PHP script. Make sure you keep this server running till you finish testing.</p>
<p><strong>Installing PHPUnit</strong><br />
1. An easy way to install PHPUnit is to use the PEAR installer. The PEAR channel (pear.phpunit.de) is used to distribute PHPUnit so make sure that it is registered with your local PEAR environment: </p>
<p><em>pear channel-discover pear.phpunit.de</em></p>
<p>After the channel is registered install PHPUnit:</p>
<p><em>pear install phpunit/PHPUnit</em></p>
<p><strong>Actual testing</strong><br />
Now that PHPUnit is installed and the Selenium RC server is up and running, its time to run our test we saved before in our &#8216;Example.php&#8217; file. Type the following on your command-line:</p>
<p><em>phpunit Example</em></p>
<p>This will start the test. The PHPUnit Selenium driver will execute each test command from your file and send it to the Selenium server, which does the job of launching the appropriate browser, opening web pages, and performing various specified actions; and closing the browser after the test completes.</p>
<p>Now the above test does basically nothing important. So we will add some simple conditional statements to the code.</p>

<div class="wp_codebox"><table><tr id="p11423"><td class="code" id="p114code23"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
    <span style="color: #000088;">$bookmarks</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xpath=/html/body[@id='index']/div
                       [@id='doc3']/div[@id='bd']/div
                       [@id='yui-main']/div[@id='content']/h3/div/em&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$bookmarks</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3000</span><span style="color: #009900;">&#41;</span>? <span style="color: #0000ff;">&quot;On the top&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Still not there&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This is what PHPUnit has to say about the above test.</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium28.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium28.gif" style="border: 1px solid #c0c0c0;"  alt="" title="selenium28" width="477" height="244" class="aligncenter size-full wp-image-139" /></a></p>
<p>Or we can use an assertion as below, deliberately rigged to fail the test:</p>

<div class="wp_codebox"><table><tr id="p11424"><td class="code" id="p114code24"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
    <span style="color: #000088;">$bookmarks</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xpath=/html/body[@id='index']/div
                       [@id='doc3']/div[@id='bd']/div
                       [@id='yui-main']/div[@id='content']/h3/div/em&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertGreaterThan</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">33000</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bookmarks</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Now PHPUnit will shout failure:</p>
<p><a href='http://www.codediesel.com/wp-content/uploads/2008/11/selenium29.gif'><img src="http://www.codediesel.com/wp-content/uploads/2008/11/selenium29.gif" alt="" title="selenium29" width="500" height="269" class="aligncenter size-full wp-image-140" /></a></p>
<p>You can look at more functions to implement in your tests <a target="_blank" href="http://www.phpunit.de/wiki/Documentation">here</a> and <a target="_blank" href="http://selenium-core.seleniumhq.org/reference.html">here</a>. </p>
<p>This concludes the second part of the tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/selenium-ide-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>61</slash:comments>
		</item>
	</channel>
</rss>

