<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Blog entries tagged testing :: mwop.net</title>
  <updated>2014-08-21T14:30:00-05:00</updated>
  <generator uri="https://getlaminas.org" version="2">Laminas_Feed_Writer</generator>
  <link rel="alternate" type="text/html" href="https://mwop.net/blog/tag/testing"/>
  <link rel="self" type="application/atom+xml" href="https://mwop.net/blog/tag/testing/atom.xml"/>
  <id>https://mwop.net/blog/tag/testing</id>
  <entry xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <title type="html"><![CDATA[Testing Code That Emits Output]]></title>
    <published>2014-08-21T14:30:00-05:00</published>
    <updated>2014-08-21T14:30:00-05:00</updated>
    <link rel="alternate" type="text/html" href="https://mwop.net/blog/2014-08-11-testing-output-generating-code.html"/>
    <id>https://mwop.net/blog/2014-08-11-testing-output-generating-code.html</id>
    <author>
      <name>Matthew Weier O'Phinney</name>
      <email>contact@mwop.net</email>
      <uri>https://mwop.net</uri>
    </author>
    <content xmlns:xhtml="http://www.w3.org/1999/xhtml" type="xhtml">
      <xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"><xhtml:p>Here's the scenario: you have code that will emit headers and
content, for instance, a front controller. How do you test
this?</xhtml:p>
<xhtml:p>The answer is remarkably simple, but non-obvious:
namespaces.</xhtml:p>
<xhtml:h2>Prerequisites</xhtml:h2>
<xhtml:p>For this approach to work, the assumptions are:</xhtml:p>
<xhtml:ul>
<xhtml:li>Your code emitting headers and output lives in a namespace
other than the global namespace.</xhtml:li>
</xhtml:ul>
<xhtml:p>That's it. Considering that most PHP code you grab anymore does
this, and most coding standards you run across will require this,
it's a safe bet that you're already ready. If you're not, go
refactor your code now, before continuing; you'll thank me
later.</xhtml:p>
<xhtml:h2>The technique</xhtml:h2>
<xhtml:p>PHP introduced namespaces in PHP 5.3. Namespaces cover classes,
as most of us are well aware, but they also cover constants and
functions — a fact often overlooked, as before 5.6 (releasing next
week!), you cannot import them via use statements!</xhtml:p>
<xhtml:p>That does not mean they cannot be defined and used, however — it
just means that you need to manually import them, typically via a
<xhtml:code>require</xhtml:code> or <xhtml:code>require_once</xhtml:code> statement. These
are usually anathema in libraries, but for testing, they work just
fine.</xhtml:p>
<xhtml:p>Here's an approach I took recently. I created a file that lives
— this is the important bit, so pay attention — <xhtml:em>in the same
namespace as the code emitting headers and output</xhtml:em>. This file
defines several functions that live in the global (aka PHP's
built-in) namespace, and an accumulator static object I can then
use in my tests for assertions. Here's what it looks like:</xhtml:p>
<xhtml:pre><xhtml:code class="language-php hljs php" data-lang="php"><xhtml:span class="hljs-keyword">namespace</xhtml:span> <xhtml:span class="hljs-title">Some</xhtml:span>\<xhtml:span class="hljs-title">Project</xhtml:span>;

<xhtml:span class="hljs-keyword">abstract</xhtml:span> <xhtml:span class="hljs-class"><xhtml:span class="hljs-keyword">class</xhtml:span> <xhtml:span class="hljs-title">Output</xhtml:span>
</xhtml:span>{
    <xhtml:span class="hljs-keyword">public</xhtml:span> <xhtml:span class="hljs-keyword">static</xhtml:span> $headers = <xhtml:span class="hljs-keyword">array</xhtml:span>();
    <xhtml:span class="hljs-keyword">public</xhtml:span> <xhtml:span class="hljs-keyword">static</xhtml:span> $body;

    <xhtml:span class="hljs-keyword">public</xhtml:span> <xhtml:span class="hljs-keyword">static</xhtml:span> <xhtml:span class="hljs-function"><xhtml:span class="hljs-keyword">function</xhtml:span> <xhtml:span class="hljs-title">reset</xhtml:span><xhtml:span class="hljs-params">()</xhtml:span>
    </xhtml:span>{
        <xhtml:span class="hljs-keyword">self</xhtml:span>::$headers = <xhtml:span class="hljs-keyword">array</xhtml:span>();
        <xhtml:span class="hljs-keyword">self</xhtml:span>::$body = <xhtml:span class="hljs-keyword">null</xhtml:span>;
    }
}

<xhtml:span class="hljs-function"><xhtml:span class="hljs-keyword">function</xhtml:span> <xhtml:span class="hljs-title">headers_sent</xhtml:span><xhtml:span class="hljs-params">()</xhtml:span>
</xhtml:span>{
    <xhtml:span class="hljs-keyword">return</xhtml:span> <xhtml:span class="hljs-keyword">false</xhtml:span>;
}

<xhtml:span class="hljs-function"><xhtml:span class="hljs-keyword">function</xhtml:span> <xhtml:span class="hljs-title">header</xhtml:span><xhtml:span class="hljs-params">($value)</xhtml:span>
</xhtml:span>{
    Output::$headers[] = $value;
}

<xhtml:span class="hljs-function"><xhtml:span class="hljs-keyword">function</xhtml:span> <xhtml:span class="hljs-title">printf</xhtml:span><xhtml:span class="hljs-params">($text)</xhtml:span>
</xhtml:span>{
    Output::$body .= $text;
}
</xhtml:code></xhtml:pre>
<xhtml:p>A few notes:</xhtml:p>
<xhtml:ul>
<xhtml:li><xhtml:code>headers_sent()</xhtml:code> always returns false here, as most
emitters test for a boolean true value and bail early when that
occurs.</xhtml:li>
<xhtml:li>I used <xhtml:code>printf()</xhtml:code> here, as echo cannot be overridden
due to being a PHP language construct and not an actual function.
As such, if you use this technique, you will have to likely alter
your emitter to call <xhtml:code>printf()</xhtml:code> instead of echo. The
benefits, however, are worth it.</xhtml:li>
<xhtml:li>I marked Output abstract, to prevent instantiation; it should
only be used statically.</xhtml:li>
</xhtml:ul>
<xhtml:p>I place the above file within my test suite, usually under a
<xhtml:code>TestAsset</xhtml:code> directory adjacent to the test itself; since
it contains functions, I'll name the file
<xhtml:code>Functions.php</xhtml:code> as well. This combination typically will
prevent it from being autoloaded in any way, as the test directory
will often not have autoloading defined, or will be under a
separate namespace.</xhtml:p>
<xhtml:p>Inside your PHPUnit test suite, then, you would do the
following:</xhtml:p>
<xhtml:pre><xhtml:code class="language-php hljs php" data-lang="php"><xhtml:span class="hljs-keyword">namespace</xhtml:span> <xhtml:span class="hljs-title">SomeTest</xhtml:span>\<xhtml:span class="hljs-title">Project</xhtml:span>;

<xhtml:span class="hljs-keyword">use</xhtml:span> <xhtml:span class="hljs-title">PHPUnit_Framework_TestCase</xhtml:span> <xhtml:span class="hljs-title">as</xhtml:span> <xhtml:span class="hljs-title">TestCase</xhtml:span>;
<xhtml:span class="hljs-keyword">use</xhtml:span> <xhtml:span class="hljs-title">Some</xhtml:span>\<xhtml:span class="hljs-title">Project</xhtml:span>\<xhtml:span class="hljs-title">FrontController</xhtml:span>;
<xhtml:span class="hljs-keyword">use</xhtml:span> <xhtml:span class="hljs-title">Some</xhtml:span>\<xhtml:span class="hljs-title">Project</xhtml:span>\<xhtml:span class="hljs-title">Output</xhtml:span>;                 <xhtml:span class="hljs-comment">// &lt;-- our Output class from above</xhtml:span>
<xhtml:span class="hljs-keyword">require_once</xhtml:span> <xhtml:span class="hljs-keyword">__DIR__</xhtml:span> . <xhtml:span class="hljs-string">'/TestAsset/Functions.php'</xhtml:span>; <xhtml:span class="hljs-comment">// &lt;-- get our functions</xhtml:span>

<xhtml:span class="hljs-class"><xhtml:span class="hljs-keyword">class</xhtml:span> <xhtml:span class="hljs-title">FrontControllerTest</xhtml:span> <xhtml:span class="hljs-keyword">extends</xhtml:span> <xhtml:span class="hljs-title">TestCase</xhtml:span>
</xhtml:span>{
    <xhtml:span class="hljs-keyword">public</xhtml:span> <xhtml:span class="hljs-function"><xhtml:span class="hljs-keyword">function</xhtml:span> <xhtml:span class="hljs-title">setUp</xhtml:span><xhtml:span class="hljs-params">()</xhtml:span>
    </xhtml:span>{
        Output::reset();
        <xhtml:span class="hljs-comment">/* ... */</xhtml:span>
    }

    <xhtml:span class="hljs-keyword">public</xhtml:span> <xhtml:span class="hljs-function"><xhtml:span class="hljs-keyword">function</xhtml:span> <xhtml:span class="hljs-title">tearDown</xhtml:span><xhtml:span class="hljs-params">()</xhtml:span>
    </xhtml:span>{
        Output::reset();
        <xhtml:span class="hljs-comment">/* ... */</xhtml:span>
    }
}
</xhtml:code></xhtml:pre>
<xhtml:p>From here, you test as normal — but when you invoke methods that
will cause headers or content to emit, you can now test to see what
those contain:</xhtml:p>
<xhtml:pre><xhtml:code class="language-php hljs php" data-lang="php">    <xhtml:span class="hljs-keyword">public</xhtml:span> <xhtml:span class="hljs-function"><xhtml:span class="hljs-keyword">function</xhtml:span> <xhtml:span class="hljs-title">testEmitsExpectedHeadersAndContent</xhtml:span><xhtml:span class="hljs-params">()</xhtml:span>
    </xhtml:span>{
        <xhtml:span class="hljs-comment">/* ... */</xhtml:span>

        <xhtml:span class="hljs-keyword">$this</xhtml:span>-&gt;assertContains(<xhtml:span class="hljs-string">'Content-Type: application/json'</xhtml:span>, Output::$headers);
        $json = Output::$body;
        $data = json_decode($json, <xhtml:span class="hljs-keyword">true</xhtml:span>);
        <xhtml:span class="hljs-keyword">$this</xhtml:span>-&gt;assertArrayHasKey(<xhtml:span class="hljs-string">'foo'</xhtml:span>, $data);
        <xhtml:span class="hljs-keyword">$this</xhtml:span>-&gt;assertEquals(<xhtml:span class="hljs-string">'bar'</xhtml:span>, $data[<xhtml:span class="hljs-string">'foo'</xhtml:span>]);
    }
</xhtml:code></xhtml:pre>
<xhtml:h2>How it works</xhtml:h2>
<xhtml:p>Why does this work?</xhtml:p>
<xhtml:p>PHP performs some magic when it resolves functions. With
classes, it looks for a matching class either in the current
namespace, or one that was imported (and potentially aliased); if a
match is not found, it stops, and raises an error. With functions,
however, it looks first in the current namespace, and if it isn't
found, then looks in the global namespace. This last part is key —
it means that if you redefine a function in the current namespace,
it will be used in lieu of the original function defined by PHP.
This also means that any code operating in the same namespace as
the function — even if defined in another file — will use that
function.</xhtml:p>
<xhtml:p>This technique just leverages this fact.</xhtml:p>
<xhtml:div class="h-entry"><xhtml:img class="u-photo photo" width="50" src="https://avatars0.githubusercontent.com/u/25943?v=3&amp;u=79dd2ea1d4d8855944715d09ee4c86215027fa80&amp;s=140" alt="matthew"/> <xhtml:a class="u-url u-uid p-name" href="https://mwop.net/blog/2014-08-11-testing-output-generating-code.html">
Testing Code That Emits Output</xhtml:a> was originally published
<xhtml:time class="dt-published" datetime="2014-08-21T14:30:00-05:00">21
August 2014</xhtml:time> on <xhtml:a href="https://mwop.net">https://mwop.net</xhtml:a> by <xhtml:a rel="author" class="p-author" href="https://mwop.net">Matthew Weier
O'Phinney</xhtml:a>.</xhtml:div>
</xhtml:div>
    </content>
  </entry>
  <entry xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <title type="html"><![CDATA[Setting up your Zend_Test test suites]]></title>
    <published>2008-09-11T15:00:00-05:00</published>
    <updated>2008-09-13T09:37:40-05:00</updated>
    <link rel="alternate" type="text/html" href="https://mwop.net/blog/190-Setting-up-your-Zend_Test-test-suites.html"/>
    <id>https://mwop.net/blog/190-Setting-up-your-Zend_Test-test-suites.html</id>
    <author>
      <name>Matthew Weier O'Phinney</name>
      <email>contact@mwop.net</email>
      <uri>https://mwop.net</uri>
    </author>
    <content xmlns:xhtml="http://www.w3.org/1999/xhtml" type="xhtml">
      <xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"><xhtml:p>Now that <xhtml:a href="http://framework.zend.com/manual/en/zend.test.html">Zend_Test</xhtml:a>
has shipped, developers are of course asking, "How do I setup my
test suite?" Fortunately, after some discussion with my colleagues
and a little experimenting on my one, I can answer that now.</xhtml:p>
<xhtml:p><xhtml:a href="http://phpunit.de">PHPUnit</xhtml:a> offers a variety of
methods for setting up test suites, some trivial and some complex.
The Zend Framework test suite, for instance, goes for a more
complex route, adding component-level suites that require a fair
amount of initial setup, but which allow us fairly fine-grained
control.</xhtml:p>
<xhtml:p>However, testing and test automation should be easy and the
complex approach is overkill for most of our applications.
Fortunately, PHPUnit offers some other methods that make doing so
relatively simple. The easiest method is to use an <xhtml:a href="http://www.phpunit.de/pocket_guide/3.2/en/appendixes.configuration.html">
XML configuration file</xhtml:a>.</xhtml:p>
<xhtml:p>As an example, consider the following:</xhtml:p>
<xhtml:pre><xhtml:code class="language-xml hljs xml" data-lang="xml"><xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">phpunit</xhtml:span>&gt;</xhtml:span>
    <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">testsuite</xhtml:span> <xhtml:span class="hljs-attr">name</xhtml:span>=<xhtml:span class="hljs-string">"My Test Suite"</xhtml:span>&gt;</xhtml:span>
        <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">directory</xhtml:span>&gt;</xhtml:span>./<xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">directory</xhtml:span>&gt;</xhtml:span>
    <xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">testsuite</xhtml:span>&gt;</xhtml:span>

    <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">filter</xhtml:span>&gt;</xhtml:span>
        <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">whitelist</xhtml:span>&gt;</xhtml:span>
            <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">directory</xhtml:span> <xhtml:span class="hljs-attr">suffix</xhtml:span>=<xhtml:span class="hljs-string">".php"</xhtml:span>&gt;</xhtml:span>../library/<xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">directory</xhtml:span>&gt;</xhtml:span>
            <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">directory</xhtml:span> <xhtml:span class="hljs-attr">suffix</xhtml:span>=<xhtml:span class="hljs-string">".php"</xhtml:span>&gt;</xhtml:span>../application/<xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">directory</xhtml:span>&gt;</xhtml:span>
            <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">exclude</xhtml:span>&gt;</xhtml:span>
                <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">directory</xhtml:span> <xhtml:span class="hljs-attr">suffix</xhtml:span>=<xhtml:span class="hljs-string">".phtml"</xhtml:span>&gt;</xhtml:span>../application/<xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">directory</xhtml:span>&gt;</xhtml:span>
            <xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">exclude</xhtml:span>&gt;</xhtml:span>
        <xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">whitelist</xhtml:span>&gt;</xhtml:span>
    <xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">filter</xhtml:span>&gt;</xhtml:span>

    <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">logging</xhtml:span>&gt;</xhtml:span>
        <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">log</xhtml:span> <xhtml:span class="hljs-attr">type</xhtml:span>=<xhtml:span class="hljs-string">"coverage-html"</xhtml:span> <xhtml:span class="hljs-attr">target</xhtml:span>=<xhtml:span class="hljs-string">"./log/report"</xhtml:span> <xhtml:span class="hljs-attr">charset</xhtml:span>=<xhtml:span class="hljs-string">"UTF-8"</xhtml:span>
            <xhtml:span class="hljs-attr">yui</xhtml:span>=<xhtml:span class="hljs-string">"true"</xhtml:span> <xhtml:span class="hljs-attr">highlight</xhtml:span>=<xhtml:span class="hljs-string">"true"</xhtml:span>
            <xhtml:span class="hljs-attr">lowUpperBound</xhtml:span>=<xhtml:span class="hljs-string">"50"</xhtml:span> <xhtml:span class="hljs-attr">highLowerBound</xhtml:span>=<xhtml:span class="hljs-string">"80"</xhtml:span>/&gt;</xhtml:span>
        <xhtml:span class="hljs-tag">&lt;<xhtml:span class="hljs-name">log</xhtml:span> <xhtml:span class="hljs-attr">type</xhtml:span>=<xhtml:span class="hljs-string">"testdox-html"</xhtml:span> <xhtml:span class="hljs-attr">target</xhtml:span>=<xhtml:span class="hljs-string">"./log/testdox.html"</xhtml:span> /&gt;</xhtml:span>
    <xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">logging</xhtml:span>&gt;</xhtml:span>
<xhtml:span class="hljs-tag">&lt;/<xhtml:span class="hljs-name">phpunit</xhtml:span>&gt;</xhtml:span>
</xhtml:code></xhtml:pre>
<xhtml:p>First thing to note, relative paths are relative to the
configuration file. This allows you to run your tests from anywhere
in your tests tree. Second, providing a <xhtml:code>directory</xhtml:code>
directive to the <xhtml:code>testsuite</xhtml:code> directive scans for all
files ending in <xhtml:code>Test.php</xhtml:code> in that directory, meaning
you don't have to keep a list of your test cases manually. It's a
great way to automate the suite. Third, the filter directive allows
us to determine what classes to include and/or exclude from
coverage reports. Finally, the <xhtml:code>logging</xhtml:code> directive lets
us specify what kinds of logs to create and where.</xhtml:p>
<xhtml:p>Drop the above into <xhtml:code>tests/phpunit.xml</xhtml:code> in your
application, and you can start writing test cases and running the
suite immediately, using the following command:</xhtml:p>
<xhtml:pre><xhtml:code class="language-bash hljs bash" data-lang="bash">$ phpunit --configuration phpunit.xml
</xhtml:code></xhtml:pre>
<xhtml:p>I like to group my test cases by type. I have controllers,
models, and often library code, and need to keep the tests
organized both on the filesystem as well as for running the actual
tests. There are two things I do to facilitate this.</xhtml:p>
<xhtml:p>First, I create directories. For instance, I have the following
hierarchy in my test suite:</xhtml:p>
<xhtml:pre><xhtml:code class="language- hljs" data-lang="">tests/
    phpunit.xml
    TestHelper.php
    controllers/
        IndexControllerTest.php (contains IndexControllerTest)
        ErrorControllerTest.php (contains ErrorControllerTest)
        ...
    models/
        PasteTest.php           (contains PasteTest)
        DbTable/
            PasteTest.php       (contains DbTable_PasteTest)
        ...
    My/
        Form/
            Element/
                SimpleTextareaTest.php
</xhtml:code></xhtml:pre>
<xhtml:p><xhtml:code>controllers/</xhtml:code> contains my controllers,
<xhtml:code>models/</xhtml:code> contains my models. If I were developing a
modular application, I'd have something like
<xhtml:code>blog/controllers/</xhtml:code> instead. Library code is given the
same hierarchy as is found in my <xhtml:code>library/</xhtml:code>
directory.</xhtml:p>
<xhtml:p>Second, I use docblock annotations to group my tests. I add the
following to my class-level docblock in my controller test
cases:</xhtml:p>
<xhtml:pre><xhtml:code class="language-php hljs php" data-lang="php"><xhtml:span class="hljs-comment">/**
 * <xhtml:span class="hljs-doctag">@group</xhtml:span> Controllers
 */</xhtml:span>
</xhtml:code></xhtml:pre>
<xhtml:p>Models get the annotation <xhtml:code>@group Models</xhtml:code>, etc. This
allows me to run individual sets of tests on demand:</xhtml:p>
<xhtml:pre><xhtml:code class="language-bash hljs bash" data-lang="bash">$ phpunit --configuration phpunit.xml --group=Controllers
</xhtml:code></xhtml:pre>
<xhtml:p>You can specify multiple <xhtml:code>@group</xhtml:code> annotations, which
means you can separate tests into modules, issue report
identifiers, etc; additionally, you can add the annotations to
individual test methods themselves to have really fine-grained test
running capabilities.</xhtml:p>
<xhtml:p>Astute readers will have noticed the <xhtml:code>TestHelper.php</xhtml:code>
file in that directory listing earlier, and will be wondering what
that's all about.</xhtml:p>
<xhtml:p>A test suite needs some environmental information, just like
your application does. It may need a default database adapter,
altered <xhtml:code>include_path</xhtml:code>s, autoloading set up, and more.
Here's what my <xhtml:code>TestHelper.php</xhtml:code> looks like:</xhtml:p>
<xhtml:pre><xhtml:code class="language-php hljs php" data-lang="php"><xhtml:span class="hljs-meta">&lt;?php</xhtml:span>
<xhtml:span class="hljs-comment">/*
 * Start output buffering
 */</xhtml:span>
ob_start();

<xhtml:span class="hljs-comment">/*
 * Set error reporting to the level to which code must comply.
 */</xhtml:span>
error_reporting( E_ALL | E_STRICT );

<xhtml:span class="hljs-comment">/*
 * Set default timezone
 */</xhtml:span>
date_default_timezone_set(<xhtml:span class="hljs-string">'GMT'</xhtml:span>);

<xhtml:span class="hljs-comment">/*
 * Testing environment
 */</xhtml:span>
define(<xhtml:span class="hljs-string">'APPLICATION_ENV'</xhtml:span>, <xhtml:span class="hljs-string">'testing'</xhtml:span>);

<xhtml:span class="hljs-comment">/*
 * Determine the root, library, tests, and models directories
 */</xhtml:span>
$root        = realpath(dirname(<xhtml:span class="hljs-keyword">__FILE__</xhtml:span>) . <xhtml:span class="hljs-string">'/../'</xhtml:span>);
$library     = $root . <xhtml:span class="hljs-string">'/library'</xhtml:span>;
$tests       = $root . <xhtml:span class="hljs-string">'/tests'</xhtml:span>;
$models      = $root . <xhtml:span class="hljs-string">'/application/models'</xhtml:span>;
$controllers = $root . <xhtml:span class="hljs-string">'/application/controllers'</xhtml:span>;

<xhtml:span class="hljs-comment">/*
 * Prepend the library/, tests/, and models/ directories to the
 * include_path. This allows the tests to run out of the box.
 */</xhtml:span>
$path = <xhtml:span class="hljs-keyword">array</xhtml:span>(
    $models,
    $library,
    $tests,
    get_include_path()
);
set_include_path(implode(PATH_SEPARATOR, $path));

<xhtml:span class="hljs-comment">/**
 * Register autoloader
 */</xhtml:span>
<xhtml:span class="hljs-keyword">require_once</xhtml:span> <xhtml:span class="hljs-string">'Zend/Loader.php'</xhtml:span>;
Zend_Loader::registerAutoload();

<xhtml:span class="hljs-comment">/**
 * Store application root in registry
 */</xhtml:span>
Zend_Registry::set(<xhtml:span class="hljs-string">'testRoot'</xhtml:span>, $root);
Zend_Registry::set(<xhtml:span class="hljs-string">'testBootstrap'</xhtml:span>, $root . <xhtml:span class="hljs-string">'/application/bootstrap.php'</xhtml:span>);

<xhtml:span class="hljs-comment">/*
 * Unset global variables that are no longer needed.
 */</xhtml:span>
<xhtml:span class="hljs-keyword">unset</xhtml:span>($root, $library, $models, $controllers, $tests, $path);
</xhtml:code></xhtml:pre>
<xhtml:p>The above ensures that my <xhtml:code>APPLICATION_ENV</xhtml:code> constant
is set appropriately, that error reporting is appropriate for tests
(i.e., I want to see <xhtml:em>all</xhtml:em> errors), and that autoloading is
enabled. Additionally, I place a couple items in my registry — the
bootstrap and test root directory.</xhtml:p>
<xhtml:p>In each test case file, I then do a <xhtml:code>require_once</xhtml:code> on
this file. In future versions of PHPUnit, you'll be able to specify
a bootstrap file in your configuration XML that gets pulled in for
each test case, and you'll be able to even further automate your
testing environment setup.</xhtml:p>
<xhtml:p>Hopefully this will get you started with your application
testing; what are you waiting for?</xhtml:p>
<xhtml:div class="h-entry"><xhtml:img class="u-photo photo" width="50" src="https://avatars0.githubusercontent.com/u/25943?v=3&amp;u=79dd2ea1d4d8855944715d09ee4c86215027fa80&amp;s=140" alt="matthew"/> <xhtml:a class="u-url u-uid p-name" href="https://mwop.net/blog/190-Setting-up-your-Zend_Test-test-suites.html">
Setting up your Zend_Test test suites</xhtml:a> was originally published
<xhtml:time class="dt-published" datetime="2008-09-11T15:00:00-05:00">11
September 2008</xhtml:time> on <xhtml:a href="https://mwop.net">https://mwop.net</xhtml:a> by <xhtml:a rel="author" class="p-author" href="https://mwop.net">Matthew Weier
O'Phinney</xhtml:a>.</xhtml:div>
</xhtml:div>
    </content>
  </entry>
</feed>
