<?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>PHP in Action &#187; Frameworks</title>
	<atom:link href="http://blog.agilephp.com/category/frameworks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agilephp.com</link>
	<description>Dagfinn Reiersøl on PHP, agile development, Ruby and other addictive substances</description>
	<lastBuildDate>Mon, 28 Sep 2009 14:35:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The one-line web framework</title>
		<link>http://blog.agilephp.com/2008/12/14/the-one-line-web-framework/</link>
		<comments>http://blog.agilephp.com/2008/12/14/the-one-line-web-framework/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 14:53:49 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1386</guid>
		<description><![CDATA[ The core of your average web framework is a Front Controller. Front Controllers are commonly considered complex and esoteric.  That&#8217;s a myth. I sometimes brag that I can construct a Front Controller in 15 minutes. Actually, it&#8217;s doesn&#8217;t take quite that long. In PHP, a Front Controller can be simplified to just one [...]]]></description>
			<content:encoded><![CDATA[<p> The core of your average web framework is a Front Controller. Front Controllers are commonly considered complex and esoteric.  That&#8217;s a myth. I sometimes brag that I can construct a Front Controller in 15 minutes. Actually, it&#8217;s doesn&#8217;t take quite that long. In PHP, a Front Controller can be simplified to just one line of code: </p>
<pre class="brush: php">
call_user_func($_REQUEST[&#039;action&#039;]);
</pre>
<p> <b>Warning: Do not use this in a real application.</b> </p>
<p> There is one and only one good reason why you shouldn&#8217;t: it&#8217;s totally insecure. Except for that, it&#8217;s simple but perfectly viable. Your actions will be just plain functions, which is pretty simplistic and will be hard to handle if you build many of them. But it&#8217;s a huge improvement over the average PHP script with no systematic handling of the PHP request. </p>
<p> If you want to do something more like the frameworks such as Zend Framework, you want your actions as methods grouped into classes (called Controllers in ZF). </p>
<p> Now you need two lines. The following script demonstrates this: </p>
<pre class="brush: php">
// Pretend we have an HTTP request
$_REQUEST = array(&#039;controller&#039; =&gt; &#039;Contacts&#039;, &#039;action&#039; =&gt; &#039;edit&#039;);

// Front controller
$controller = new $_REQUEST[&#039;controller&#039;];
call_user_func(array($controller,$_REQUEST[&#039;action&#039;]));

// The action controller class
class Contacts {
    function edit() {
        echo \&quot;ok\n\&quot;;
    }
}
</pre>
<p> You can start with something like it (assuming that security has been taken care of) and add features as you need them. </p>
<p> In case you&#8217;re wondering how to what exactly the security issues are, the most obvious problem is the fact that any user can run any method in any class that is available to this script. The way to avoid that append or prepend some fixed string to the class and the method name. For instance, in Zend Framework, the action name edit will activate the method editAction(). </p>
<p> Beyond that, the security problems are the usual ones. You need to <a href="http://shiflett.org/articles/input-filtering">filter input</a>, And in general you should beware of $_REQUEST, since it can also contain cookie data. </p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=The%20one-line%20web%20framework&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F12%2F14%2Fthe-one-line-web-framework%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/12/14/the-one-line-web-framework/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Zend_Test</title>
		<link>http://blog.agilephp.com/2008/11/18/zend_test/</link>
		<comments>http://blog.agilephp.com/2008/11/18/zend_test/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 23:45:54 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1392</guid>
		<description><![CDATA[ I admit I don&#8217;t follow Zend Framework very closely, since I haven&#8217;t been using it for any serious work. But I did write a piece about testing a Zend Framework action controller with View Helpers. 
 This might need updating, since the testing capabilities of the Zend Framework have grown substantially since then. In [...]]]></description>
			<content:encoded><![CDATA[<p> I admit I don&#8217;t follow Zend Framework very closely, since I haven&#8217;t been using it for any serious work. But I did write a piece about <a href="http://www.reiersol.com/blog/1_php_in_action/archive/70_testing_a_zend_framework_action_controller_with_view_helpers.html">testing a Zend Framework action controller with View Helpers</a>. </p>
<p> This might need updating, since the testing capabilities of the Zend Framework have grown substantially since then. In particular, there is now a component called <a href="http://framework.zend.com/manual/en/zend.test.html">Zend_Test</a>. I haven&#8217;t had time to study it closely yet, but I hope to do so soon. </p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Zend_Test&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F11%2F18%2Fzend_test%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/11/18/zend_test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing a Zend Framework action controller with View Helpers</title>
		<link>http://blog.agilephp.com/2008/06/10/testing-a-zend-framework-action-controller-with-view-helpers/</link>
		<comments>http://blog.agilephp.com/2008/06/10/testing-a-zend-framework-action-controller-with-view-helpers/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 22:40:23 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1372</guid>
		<description><![CDATA[ I came across a Zend Framework (ZF) example I wanted to refactor. You really have to have unit test coverage to refactor effectively, and since there were no tests, I started trying to find out how to test it. There didn&#8217;t seem to be a wealth of information available on the web, so I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p> I came across a Zend Framework (ZF) example I wanted to refactor. You really have to have unit test coverage to refactor effectively, and since there were no tests, I started trying to find out how to test it. There didn&#8217;t seem to be a wealth of information available on the web, so I&#8217;ve tried to figure it out by myself. </p>
<p> Several challenges presented themselves. It might seem as if the ZF controller system is not optimally designed for testing action controllers, but there are always ways to get around obstacles. </p>
<p> A ZF action controller (which is really just a group of actions collected in a class) always extends Zend_Controller_Action. (For an introduction, see <a href="http://framework.zend.com/wiki/display/ZFDEV/Official+ZF+QuickStart">the official QuickStart</a>). </p>
<p> The one I was playing with was an authentication controller: </p>
<pre class="brush: php">
class AuthController extends Zend_Controller_Action...
</pre>
<p> That&#8217;s all we need to know about the specific class under test for now. </p>
<p> I always like to set up the basic objects for the test as instance variables in the setUp() method, so that I can write many small test methods exercising them in various ways. To even get started testing an action controller, we need to instantiate it outside its usual environment, the Zend Front Controller. It requires a  request object and a response object: </p>
<pre class="brush: php">
class AuthControllerTest extends UnitTestCase {

  function setUp() {
    $this-&gt;request = new Zend_Controller_Request_Http();
    $this-&gt;response = new Zend_Controller_Response_Cli();
    $this-&gt;controller = new AuthController($this-&gt;request,$this-&gt;response);
  }
</pre>
<p> (I&#8217;m using SimpleTest here, but the same techniques should apply to PHPUnit with different syntax.) </p>
<p> The challenge increases increase when we want to use View Helpers, such as the flash messenger. When programming the action controller, it looks like this: </p>
<pre class="brush: php">
$flashMessenger = $this-&gt;_helper-&gt;FlashMessenger;
</pre>
<p> To test this meaningfully, we need to replace the flash messenger with a mock object. That means also having to replace $this->_helper, which is a &#8220;helper broker&#8221; object. </p>
<p> First, we generate the mock classes: </p>
<pre class="brush: php">
Mock::generate(&#039;Zend_Controller_Action_HelperBroker&#039;,&#039;MockHelperBroker&#039;);
Mock::generate(&#039;Zend_Controller_Action_Helper_FlashMessenger&#039;,&#039;MockFlashMessenger&#039;);
</pre>
<p> Unfortunately, the helper broker is a protected instance variable, with no apparent way to change it. But it&#8217;s easy to fix by adding a setter method to the action controller class: </p>
<pre class="brush: php">
class AuthController extends Zend_Controller_Action...

  public function setHelperBroker($helperBroker) {
    $this-&gt;_helper = $helperBroker;
  }
</pre>
<p> This allows us to set up the mock helper broker and let it return the flash messenger: </p>
<pre class="brush: php">
$this-&gt;helperBroker = new MockHelperBroker;
$this-&gt;controller-&gt;setHelperBroker($this-&gt;helperBroker);

$this-&gt;flashMessenger = new MockFlashMessenger;
$this-&gt;helperBroker-&gt;setReturnValue(
  &#039;__get&#039;,$this-&gt;flashMessenger,array(&#039;FlashMessenger&#039;));
</pre>
<p> Now we can use the mock objects to test an action which is supposed to send a flash message:
<pre class="brush: php">
function testIdentifyActionSendsFlashMessage() {
    $this-&gt;flashMessenger-&gt;expectOnce(
        &#039;addMessage&#039;,
        array(&#039;Please provide a username and password.&#039;));
    $this-&gt;controller-&gt;identifyAction();
}
</pre>
<p> That&#8217;s as far as I&#8217;m going with this now. There may be more later. </p>
<p> </body></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Testing%20a%20Zend%20Framework%20action%20controller%20with%20View%20Helpers&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F06%2F10%2Ftesting-a-zend-framework-action-controller-with-view-helpers%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/06/10/testing-a-zend-framework-action-controller-with-view-helpers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
