<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.test.phpunit" xmlns:xi="http://www.w3.org/2001/XInclude">
    <title>Zend_Test_PHPUnit</title>

    <para>
        <classname>Zend_Test_PHPUnit</classname> provides a TestCase for <acronym>MVC</acronym> applications
        that contains assertions for testing against a variety of
        responsibilities. Probably the easiest way to understand what it can do
        is to see an example.
    </para>

    <example id="zend.test.phpunit.loginexample">
        <title>Application Login TestCase example</title>

        <para>
            The following is a simple test case for a
            <code>UserController</code> to verify several things:
        </para>

        <itemizedlist>
            <listitem><para>
                The login form should be displayed to non-authenticated users.
            </para></listitem>

            <listitem><para>
                When a user logs in, they should be redirected to their profile
                page, and that profile page should show relevant information.
            </para></listitem>
        </itemizedlist>

        <para>
            This particular example assumes a few things. First, we're moving
            most of our bootstrapping to a plugin. This simplifies setup of the
            test case as it allows us to specify our environment succinctly,
            and also allows us to bootstrap the application in a single line.
            Also, our particular example is assuming that autoloading is setup
            so we do not need to worry about requiring the appropriate classes
            (such as the correct controller, plugin, etc).
        </para>

        <programlisting language="php"><![CDATA[
class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public function setUp()
    {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
    }

    public function appBootstrap()
    {
        $this->frontController
             ->registerPlugin(new Bugapp_Plugin_Initialize('development'));
    }

    public function testCallWithoutActionShouldPullFromIndexAction()
    {
        $this->dispatch('/user');
        $this->assertController('user');
        $this->assertAction('index');
    }

    public function testIndexActionShouldContainLoginForm()
    {
        $this->dispatch('/user');
        $this->assertAction('index');
        $this->assertQueryCount('form#loginForm', 1);
    }

    public function testValidLoginShouldGoToProfilePage()
    {
        $this->request->setMethod('POST')
              ->setPost(array(
                  'username' => 'foobar',
                  'password' => 'foobar'
              ));
        $this->dispatch('/user/login');
        $this->assertRedirectTo('/user/view');

        $this->resetRequest()
             ->resetResponse();

        $this->request->setMethod('GET')
             ->setPost(array());
        $this->dispatch('/user/view');
        $this->assertRoute('default');
        $this->assertModule('default');
        $this->assertController('user');
        $this->assertAction('view');
        $this->assertNotRedirect();
        $this->assertQuery('dl');
        $this->assertQueryContentContains('h2', 'User: foobar');
    }
}
]]></programlisting>

        <para>
            This example could be written somewhat simpler -- not all the
            assertions shown are necessary, and are provided for illustration
            purposes only. Hopefully, it shows how simple it can be to test
            your applications.
        </para>
    </example>

    <xi:include href="Zend_Test-PHPUnit-Bootstrapping.xml" />
    <xi:include href="Zend_Test-PHPUnit-Testing.xml" />
    <xi:include href="Zend_Test-PHPUnit-Assertions.xml" />
    <xi:include href="Zend_Test-PHPUnit-Examples.xml" />
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
