<?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>Slice of Lime &#187; Developer&#8217;s Corner</title>
	<atom:link href="http://www.sliceoflime.com/blog/category/developers-corner/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sliceoflime.com/blog</link>
	<description>Strategy. Design. Development</description>
	<lastBuildDate>Thu, 29 Jul 2010 16:39:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Routing Subdomains to Modules in Zend Framework</title>
		<link>http://www.sliceoflime.com/blog/2009/03/29/routing-subdomains-to-modules-in-zend-framework/</link>
		<comments>http://www.sliceoflime.com/blog/2009/03/29/routing-subdomains-to-modules-in-zend-framework/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 07:18:52 +0000</pubDate>
		<dc:creator>rajones</dc:creator>
				<category><![CDATA[Developer's Corner]]></category>

		<guid isPermaLink="false">http://www.sliceoflime.com/blog/?p=698</guid>
		<description><![CDATA[While working on a recent in-house project, we saw a need to take a step back to look at the big picture. We asked ourselves, &#8220;How can we fully leverage the flexibility of the Zend Framework in order to keep our related in house projects under one code-base?&#8221; This would allow for such things as [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a recent in-house project, we saw a need to take a step back to look at the big picture. We asked ourselves, &#8220;How can we fully leverage the flexibility of the Zend Framework in order to keep our related in house projects under one code-base?&#8221; This would allow for such things as a common login,  easy access to common code, and site-wide variables.</p>
<p>The answer? Define each project as its own module that could access, for example, a common &#8220;Auth&#8221; session object and a common layout. So we started with the idea that we would have urls like http://mydomain.com/module/controller/action/. However, we preferred to have each project on its own subdomain. Thus we had a new question, &#8220;How do we route a subdomain to a module?&#8221;</p>
<p>The answer to that turned out to be incredibly simple. Only three things need to be in place:</p>
<ul>
<li>Properly configured name-based Apache hosts</li>
<li>Properly written .htaccess</li>
<li>Specific code within your Zend Framework bootstrap file</li>
</ul>
<h2>Apache Configuration</h2>
<p>Assuming that you&#8217;re on a LAMP server, and Apache is configured for Virtual Hosts, your httpd.conf or httpd.include might look something like this:<br />
<small>Note that this has been simplified for the point of explanation here, and you should use the real public IP of your server not the localhost address</small></p>
<div style="font-size:.9em; border:solid 1px #CCC; background:#f5f5f5; padding:20px;">
&lt;VirtualHost 127.0.0.1:80&gt;<br />
	ServerName   www.mydomain.com:80<br />
	UseCanonicalName Off<br />
	DocumentRoot /path/to/www/document-root<br />
&lt;/VirtualHost&gt;<br />
<br />
&lt;VirtualHost 127.0.0.1:80&gt;<br />
	ServerName   mydomain.com:80<br />
	UseCanonicalName Off<br />
	DocumentRoot /path/to/application/document-root<br />
&lt;/VirtualHost&gt;
</div>
<p>This ensures that your www host has its own document root, isolated from your application. Of course, you could just as easily include your www to be a module within your application by leaving out that first VirtualHost declaration. </p>
<p>Notice that in the second VirtualHost definition the ServerName does not contain a subdomain; it simply states the root level of your domain. This will act as a sort of catch-all for all subdomains that <em>have not been explicitly declared within their own VirtualHost definitions</em>. Any subdomain that you do not want included as a module within your application should be explicitly defined via its own VirtualHost definition.</p>
<h2>.htaccess and mod_rewrite</h2>
<p>I&#8217;m of the opinion that examples from Zend Quickstart and other resources over-complicate the mod_rewrite rules necessary for putting Zend Framework into action. Here is the mod_rewrite rules that we use without a hitch:</p>
<div style="border:solid 1px #CCC; background:#f5f5f5; padding:20px; font-size:.9em; ">
RewriteCond %{REQUEST_FILENAME} !-d<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteRule .* index.php [QSA,L]
</div>
<p><small>Note that that is just a sample of our .htaccess file that has numerous features to enhance security and performance. Something we&#8217;ll be writing about soon.</small></p>
<h2>Magic in the Bootstrap</h2>
<p>Firstly, the standard router in Zend Framework expects the URI to follow a format like /module/controller/action/, and references the $_SERVER['REQUEST_URI'] to find it. The problem is that our subdomain-per-module approach moves the module from the $_SERVER['REQUEST_URI'] to $_SERVER['SERVER_NAME']. How then would Zend properly route controllers and actions to the appropriate module? Take a look at the following code:</p>
<div style="border:solid 1px #CCC; background:#f5f5f5; padding:20px; font-size:.9em; ">
// Define the module<br />
define(&#8216;MODULE&#8217;,strtolower(array_shift (explode(&#8216;.&#8217;,$_SERVER['SERVER_NAME']))));</p>
<p>// Declare existing modules<br />
$modules = array(&#8216;blog&#8217;=>APPLICATION_PATH.&#8217;modules/blog/controllers&#8217;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8217;store&#8217;=>APPLICATION_PATH.&#8217;modules/store/controllers&#8217;);</p>
<p>// Redirect if user enters subdomain that has not been declared<br />
if (!array_key_exists(MODULE,$modules))<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;header (&#8216;Location: http://www.sliceoflime.com&#8217;);<br />
&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />
}</p>
<p>// Prepare $Request object and set the URI<br />
$Request = new Zend_Controller_Request_Http;<br />
$Request->setRequestUri(&#8216;/&#8217;.MODULE.$_SERVER['REQUEST_URI']);</p>
<p>// Prepare the front controller and dispatch<br />
$frontController = Zend_Controller_Front::getInstance();<br />
$frontController->setControllerDirectory($modules);<br />
$frontController->setDefaultModule(&#8217;store&#8217;);<br />
$frontController->setRequest($Request);<br />
$frontController->dispatch();
</p></div>
<p>That first line does three things at once: extracts the subdomain part of the url, converts it to lowercase, and creates a MODULE constant we can use later in our code. Now we need to head off the front controller at the pass so to speak. By default, if no request object was given to the front controller, it will create one itself, and use the default properties the request object generates. In such a case, our module would not be included in the $_SERVER['REQUEST_URI'] and therefore not properly routed. In this case we explicitly create a request object, and set its $_requestUri property via setRequestUri() by prepending the MODULE constant to the existing $_SERVER['REQUEST_URI'].</p>
<p>From there, we continue on as normal. We create the front controller object and set its controller directories by passing it an array that defines each of our modules and their respective controller directories. That second to the last line, where we call the setRequest method, is vitally important. This is where the front controller is set to use the request object we&#8217;ve created and modified.</p>
<p>I hope this will be useful to other developers out there. Please do leave your comments, we&#8217;d love to hear how this worked or did not work for you, and other issues relating to routing subdomains to modules in Zend Framework.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sliceoflime.com/blog/2009/03/29/routing-subdomains-to-modules-in-zend-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SimpleTest with the Zend Framework, part 1</title>
		<link>http://www.sliceoflime.com/blog/2008/12/02/using-simpletest-with-the-zend-framework-part-1/</link>
		<comments>http://www.sliceoflime.com/blog/2008/12/02/using-simpletest-with-the-zend-framework-part-1/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 00:32:39 +0000</pubDate>
		<dc:creator>brice</dc:creator>
				<category><![CDATA[Developer's Corner]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SimpleTest]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Test-driven Development]]></category>
		<category><![CDATA[Zend]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.sliceoflime.com/blog/?p=518</guid>
		<description><![CDATA[
Recognizing the need to focus on certain specific technologies, we at Slice have spent time researching various development frameworks used to create dynamic websites.  While there are a number of different development frameworks out there, each with their various advantages and disadvantages, for various reasons we have chosen to examine the Zend Framework (ZF) for [...]]]></description>
			<content:encoded><![CDATA[<p><!--StartFragment--></p>
<p class="MsoNormal">Recognizing the need to focus on certain specific technologies, we at Slice have spent time researching various development frameworks used to create dynamic websites.<span>  </span>While there are a number of different development frameworks out there, each with their various advantages and disadvantages, for various reasons we have chosen to examine the Zend Framework (ZF) for developing dynamic PHP websites.</p>
<p class="MsoNormal">My most recent experience prior to joining Slice of Lime was in using .Net in a relatively Agile methodology.<span>  </span>From that experience and from listening to the experience of others who have implemented an Agile methodology, I am becoming convinced that Test-Driven Development (TDD) is an indispensible practice for developing web applications.</p>
<p class="MsoNormal">While I am satisfied that our decision to focus on ZF will pay dividends to us and our clients, it is still a relatively new technology, and one must cast a particularly wide net to garner documentation on problems in developing with it.<span>  </span>My particular problem at this point is “How do I implement the TDD procedures I am familiar with while using ZF?” I was excited to see that ZF includes the Zend_Test class group, which extends PHPUnit’s classes.<span>  </span>Familiar with SimpleTest, a tool similar to PHPUnit, I thought, how difficult can it be to implement Zend_Test in my project?<span>  </span>The answer for those adequately familiar with Zend is probably “Incredibly Easy!”<span>  </span>For those of us just beginning to use Zend, with no familiarity with PHPUnit, and an eagerness to dive into getting code written the answer is “Difficult enough that I don’t care to investigate beyond a few internet searches.”</p>
<p class="MsoNormal">For a number of reasons I, like other exasperated bulletin board posters out there,<span>   </span>found the Zend documentation for Zend_Test insufficient to get me started.<span>  </span>I was introduced to SimpleTest by the authors of PHP In Action and their straight-forward example that demonstrates incrementally building a test-suite – an example the Zend documenter would do well to check out when they revisit the documentation for Zend_Test.<span>  </span>I decided I would forgo using Zend_Test and PHPUnit for now in favor of the familiar SimpleTest.</p>
<p class="MsoNormal">I began by sticking the simpletest directory (downloaded from simpletest.org) into my project, and decided to get one test to pass.<span>  </span>For the moment being, I put the simpletest directory into the webroot directory at the same level as my application and library directories in what is the standard directory configuration for a ZF project.<span>  </span>I’m not convinced I’ll leave it there, but I thought for simplicity’s sake, I’d try having it there.<span>  </span>I may also see if it makes more sense to have it up a level (at the same level as httpdocs) or in the library (with Zend).<span>  </span>I also created a tests directory at the same level that will include all my test files.<span>  </span>The structure looks like this then:</p>
<p class="MsoNormal">httpdocs<br />
    application<br />
        controllers<br />
        models<br />
            dao<br />
            domain<br />
            manager<br />
        views<br />
    library<br />
    Zend<br />
    simpletest<br />
    tests</p>
<p class="MsoNormal">The first test file I created in the tests directory is testOne.php:</p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php<br />
require_once(&#8216;../simpletest/unit_tester.php&#8217;);<br />
require_once(&#8216;../simpletest/reporter.php&#8217;);</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">class TestOne extends UnitTestCase {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">function TestPass() {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">$this-&gt;pass(&#8220;Test Passed&#8221;);</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">$test = new TestOne();<br />
$test-&gt;run(new HtmlReporter());</span></span></p>
<p class="MsoNormal">I open my browser to <a href="http://localhost/tests/TestOne.php"><span>http://localhost/tests/TestOne.php</span></a>.<span>  </span>My first test passed no problem!!!<span>  </span>Ok, not so exciting since I explicitly told it to pass.</p>
<p class="MsoNormal">Next I decided to test a simple business object.<span>  </span>I created a domain object called Company.<span>  </span>(The model for this application extracts the business objects from the common Table Module pattern that ZF employs.<span>  </span>Instead of the domain objects directly extending or wrapping Zend_DB_Table, a manager class uses data access objects(dao), which extend Zend_DB_Table, to create concrete domain objects.)<span>   </span>I created the file testCompany.php in tests:</p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php<br />
require_once(dirname(__FILE__) . &#8216;/../simpletest/autorun.php&#8217;);</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;"> class TestCompany extends UnitTestCase {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">function TestCreate() {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">$company = new Company();</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">$this-&gt;assertIsA($company, &#8216;Company&#8217;);</span><span><span style="color: #000080;">     </span></span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">    }</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal">Note: while poking around with SimpleTest I found that I could include autorun.php instead of having to include the unit_tester and reporter files and having to explicitly run the test, a development that must be relatively recent to the publishing of PHP In Action and a nice feature.</p>
<p class="MsoNormal">I know that I am becoming more of a TDD disciple when I take delight in seeing a red bar upon first writing a test.<span>  </span>Without a Company class my test failed.<span>  </span>Now I added the class file, Company.php,<span>  </span>into /application/models/domain.</p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php </span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">class Company</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">{</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal"><span>Of course I also need to include the class file in my test file:</span></p>
<p class="MsoNormal"><span><span style="color: #000080;">require_once(dirname(__FILE__) .<span style="color: #000000;"><span style="color: #000080;">&#8216;/../application/models/domain/Company.php&#8217;</span>);</span></span></span></p>
<p class="MsoNormal"><span>My test, http://localhost/tests/testCompany.php passes.<span>  </span>I utter, “Red-Green-Refactor,” under my breath.<span>  </span>I am going to want to do some testing of the manager, but I first decide to give my Company object at least one attribute.<span>  </span>I add the following line to TestCompany-&gt;TestCreate() to get my red bar back:</span></p>
<p class="MsoNormal"><span><span style="color: #000080;">$this-&gt;assertEqual($company-&gt;getID(),0);</span></span></p>
<p class="MsoNormal">To return to green I add to my Company class:</p>
<p class="MsoNormal"><span><span style="color: #000080;">protected $id = 0;</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">function getID() { return $this-&gt;id; }</span></span></p>
<p class="MsoNormal">The manager needs a data access object to get the instance of my Company from the database.<span>  </span>I decide my next step is to test and create my CompanyDAO class.<span>  </span>I am thinking now that I want the structure of my tests directory to mirror the structure of my models directory.<span>  </span>I make a mental note that I need to move my TestCompany file into a domain directory and create a testCompanyDAO.php in tests/dao.</p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">require_once(dirname(__FILE__) . &#8217;/../../simpletest/autorun.php&#8217;);<br />
require_once(dirname(__FILE__) .&#8217;/../../application/models/dao/CompanyDAO.php&#8217;);</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">class TestCompanyDAO extends UnitTestCase {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">function TestCreate() {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">$dao = new CompanyDAO();</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">$this-&gt;assertIsA($dao, &#8216;CompanyDAO&#8217;);</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal">My test predictably fails.<span>  </span>I add CompanyDAO.php to /application/models/dao:</p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">class CompanyDAO extends Zend_Db_Table_Abstract</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">{<br />
    protected $_name</span><span><span style="color: #000080;">    </span></span><span style="color: #000080;">= &#8216;company&#8217;;<br />
    protected $_primary = &#8216;id&#8217;;<br />
}</span></span></p>
<p class="MsoNormal">Things suddenly got interesting when my test failed again.<span>  </span>This time my test reported that it could not find Zend_Db_Table_Abstract.<span>  </span>Of course!<span>  </span>One of the first things to do when bootstrapping a Zend Framework app is to register Zend_Loader to autoload the necessary Zend objects.<span>  </span>One thing that I didn’t like about the example that the Zend_Test document provided was that it was using a plugin to the controller to bootstrap the Zend app for every test.<span>  </span>I didn’t want to have to do that for such simple tests as checking to see if I created an instance of Company or not.<span>  </span>I decided to create a simple file that mirrored whatever initialization I needed from the Zend Framework for my test files.<span>  </span>I can then include that file in only the specific test files where I need it.<span>  </span>I added bootstrapTestsForZend.php to my tests directory:</p>
<p class="MsoNormal"><span><span style="color: #000080;">// Copy application bootstrapping</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">define(&#8216;APPLICATION_PATH&#8217;, $_SERVER['DOCUMENT_ROOT'] .&#8217;/application/&#8217;);<br />
define(&#8216;APPLICATION_ENVIRONMENT&#8217;, &#8216;development&#8217;);</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">set_include_path( $_SERVER['DOCUMENT_ROOT'] . &#8216;/library&#8217; .PATH_SEPARATOR . get_include_path() );<br />
require_once &#8220;Zend/Loader.php&#8221;;<br />
Zend_Loader::registerAutoload();</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">$configuration = new Zend_Config_Ini(<br />
    APPLICATION_PATH . &#8216;/config/app.ini&#8217;,<br />
    APPLICATION_ENVIRONMENT<br />
);</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">$dbAdapter = Zend_Db::factory($configuration-&gt;database);<br />
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);</span></span></p>
<p class="MsoNormal">(For more on how/why this configuration is like it is, see the <a href="http://framework.zend.com/docs/quickstart" target="_blank">Quickstart</a>.)</p>
<p class="MsoNormal">While initially my APPLICATION_ENVIRONMENT is set to ‘development’, I will probably add a ‘testing’ environment to my configuration so that I can create and drop a lightweight data file when running tests without affecting the data that other developers are using to do functional testing with.</p>
<p class="MsoNormal">I added the following line as my first line in testCompanyDAO.php to get back to green:</p>
<p class="MsoNormal"><span><span style="color: #000080;">require_once($_SERVER['DOCUMENT_ROOT'] . &#8217;/tests/bootstrapTestsForZend.php&#8217;)</span>;</span></p>
<p class="MsoNormal">To test my manager class, which is really the workhorse of my domain model, I added /tests/manager/testCompanyMgr.php.</p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">require_once($_SERVER['DOCUMENT_ROOT'] . &#8217;/tests/bootstrapTestsForZend.php&#8217;);<br />
require_once(dirname(__FILE__) . &#8217;/../../simpletest/autorun.php&#8217;);<br />
require_once(dirname(__FILE__) . &#8217;/../../application/models/manager/CompanyMgr.php&#8217;);</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">class TestCompanyMgr extends UnitTestCase {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">function TestCreate() {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">$mgr = new CompanyMgr();</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">$this-&gt;assertIsA($mgr, &#8216;CompanyMgr&#8217;);</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">}<span style="color: #000000;"> </span></span></span></p>
<p class="MsoNormal"><span>The manager class:</span></p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php<br />
require_once APPLICATION_PATH . &#8216;/models/domain/Company.php&#8217;;</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">class CompanyMgr {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">protected $_dao;</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal"><span>One of the common methods for my manager classes is to return all instances of the business object it handles.<span>  </span>Normally I should probably test<span>  </span>the following functionality in a number of smaller increments, but for demonstration purposes I am skipping ahead here a bit.</span></p>
<p class="MsoNormal"><span>There are currently three entries in the Company table in my development database.<span>  </span>I write my test to assert that when I ask the manager to return all entries that I get three back:</span></p>
<p class="MsoNormal"><span><span style="color: #000080;">function TestGetAll() {</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">    $mgr = new CompanyMgr();<br />
    $entries = $mgr-&gt;getAll();<br />
    $this-&gt;assertEqual(count($entries),3);<br />
}</span></span></p>
<p class="MsoNormal">With my tests truly driving my development I work back and forth between code and running my tests until my resulting manager class looks like such:</p>
<p class="MsoNormal"><span><span style="color: #000080;">&lt;?php<br />
require_once APPLICATION_PATH . &#8216;/models/domain/Company.php&#8217;;</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">class CompanyMgr {<br />
    protected $_dao;<br />
    <br />
    function getAll()<br />
</span><span><span style="color: #000080;">    </span></span><span style="color: #000080;">{<br />
        $dao = $this-&gt;getDao();<br />
        $rows = $dao-&gt;fetchAll()-&gt;toArray();<br />
        $entries = array();</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">        foreach($rows as $row)<br />
        {<br />
            $entries[] = $this-&gt;createFromRow($row);<br />
</span><span><span style="color: #000080;">        </span></span><span style="color: #000080;">}<br />
        return $entries;</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">}<br />
</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">    private function getDao()<br />
    {</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span><span style="color: #000080;">   </span></span><span style="color: #000080;">if (null === $this-&gt;_dao) {<br />
            require_once APPLICATION_PATH . &#8217;/models/dao/CompanyDAO.php&#8217;;<br />
            $this-&gt;_dao = new CompanyDAO();</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">        </span></span><span style="color: #000080;">}<br />
        return $this-&gt;_dao;</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal"><span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">private function createFromRow($row)<br />
</span><span><span style="color: #000080;">     </span></span><span style="color: #000080;">{<br />
        $company = new Company();<br />
        if (isset($row["id"])) $company-&gt;setID($row["id"]);<br />
        return $company;<br />
    }</span></span></p>
<p class="MsoNormal"><span><span style="color: #000080;">}</span></span></p>
<p class="MsoNormal">As I said before, I will eventually take the step to create and tear down my data source (whether a simple file or actual db instance) each time I run the tests.<span>  </span>While developing, I’ve only needed to run one test file at a time.<span>  </span>As I move along, I will add these test files into a test suite.<span>  </span>I will probably add the creation of the data source<span>  </span>at that level.<span>  </span>SimpleTest also includes functionality to use Mock Objects, which I would also like to explore in Zend.<span>  </span>I’ll try to include those concepts as well as a few more specific to Zend Framework like testing a Zend_Form and Zend_Validate.</p>
<p><!--EndFragment--></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sliceoflime.com/blog/2008/12/02/using-simpletest-with-the-zend-framework-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
