\documentclass{article}
\usepackage{ulem}
\pagestyle{headings}
\begin{document}
\tableofcontents

\part{ Anatomy of Horde Unit Tests}
\section{ Filesystem structure of a test suite}
The following tree shows a hypothetical arrangement of all element types that you might find in a Horde package:

\begin{verbatim}

Xyz/
`-- test
    `-- Horde
        `-- Xyz
            |-- AllTests.php
            |-- Autoload.php
            |-- bootstrap.php
            |-- fixtures
            |   `-- data.xml
            |-- Integration
            |   `-- IntegrationTest.php
            |-- phpunit.xml
            |-- Server
            |   |-- conf.php
            |   |-- conf.php.dist
            |   `-- ServerTest.php
            |-- SomeTest.php
            |-- Stub
            |   `-- Object.php
            |-- TestCase.php
            `-- Unit
                `-- UnitTest.php
\end{verbatim}

We will go through the different elements below but lets first look at the hierarchy from the top level directory to the level of the \texttt{Raw: AllTests.php} file.

The top level directory \texttt{Xyz} represents a horde git repository\footnote{https://github.com/horde}.

The majority of the test suites can be found in the framework libraries but several applications have their own test suites as well. Whether a framework package or an application: In both cases the \texttt{test} directory in the top-level directory of the component will contain the tests.

Below the \texttt{test} directory the code hierarchy of the component will usually be mirrored.

For a hypothetical framework package with name \texttt{Horde\_Xyz} this means that where will be a directory \texttt{Horde} with a subdirectory \texttt{Xyz}. The latter one contains the tests. This matches the hierarchy in the \texttt{lib} directory where you will also find a \texttt{Horde} directory that usually contains a \texttt{Xyz} subdirectory. If not there will at least be an \texttt{Xyz.php} file.

For an application named \texttt{xyz} the \texttt{test} directory will only contain a directory \texttt{Xyz}.

This directory structure below \texttt{test} is just a convention and no functional requirement for the tests. At least at the moment. At some point the idea was that you would be able to unpack all PEAR package archives into one location without the file paths in the components conflicting with each other. But when we rely on PEAR for the installation now then the different test suites of package \texttt{Horde\_One} and \texttt{Horde\_Two} end up in \texttt{tests/Horde\_One} and \texttt{tests/Horde\_Two} respectively. So there is no chance for a conflict anyway. This might change at some point though. So we stick to the convention and ask anyone writing new test suites to do it as well.

\subsection{ Raw: AllTests.php}
This file is a \textbf{mandatory} requirement for a Horde test suite.  In almost every case you should use the boilerplate template found at \texttt{Test/doc/Horde/Test/template/packagetest/Raw: AllTests.php}.

\subsection{ bootstrap.php}
This file is a \textbf{mandatory} requirement for a Horde test suite.  In almost every case you should use the boilerplate template found at \texttt{Test/doc/Horde/Test/template/packagetest/bootstrap.php}.  This file contains the code necessary to integrate the tests with the rest of the Horde suite (i.e. it sets up autoloading).

\subsection{ phpunit.xml}
This file is a \textbf{mandatory} requirement for a Horde test suite.  In almost every case you should use the boilerplate template found at \texttt{Test/doc/Horde/Test/template/packagetest/phpunit.xml}.

\subsection{ Autoload.php}
This file is not required . It's purpose is to setup additional PHP autoloading so that all tests in the test suite automatically have access to all the classes required for executing the tests. The default \texttt{Horde\_Test\_AllTests} already loads a basic autoloading definition that works for most framework components; this file should only be used if additinal autoloading is needed.

This file will be needed if you desire to autoload any helper class contained in the Test framework, since the default autoloader does not account for these files.

\subsubsection{ The content of \texttt{Autoload.php}}
If you derive your test cases from a central test case definition you should load this one in \texttt{Autoload.php} as well:

\begin{verbatim}

/** Load the basic test definition */
require_once dirname(__FILE__) . '/TestCase.php';
\end{verbatim}

Sometimes it makes sense to pull in the definition of test helpers that may be used throughout the test suite. They are usually not available via autoloading and need to be pulled in explicitly:

\begin{verbatim}

/** Load stub definitions */
require_once dirname(__FILE__) . '/Stub/ListQuery.php';
require_once dirname(__FILE__) . '/Stub/DataQuery.php';
\end{verbatim}

\subsubsection{ Autoload mappings for applications}
In case you test a horde application that may have special class name mappings when autoloading, you can define this via the \texttt{Horde\_Test\_Autoload::addPrefix()} call:

\begin{verbatim}

Horde_Test_Autoload::addPrefix('IMP', dirname(__FILE__) . '/../../lib/');
\end{verbatim}

The class \texttt{IMP\_Imap} would be loaded from \texttt{dirname(Raw: \_\_FILE\_\_) . '/../../lib/Imap.php'} in the example above.

\section{ Running the test suite}
The Horde\_Test module must be installed on your system.

From the command line, navigate to the base of an application/package test directory (should be located in the \texttt{tests/} directory at the base of the application - navigate to the level of the directory containing the Raw: AllTests.php file).

To run all tests, issue the command:\newline
\begin{verbatim}

php AllTests.php
\end{verbatim}

You can also run individual tests by issuing (replace /usr/share/php with the path to PEAR):

\begin{verbatim}

/usr/share/php/Horde/Test/vendor/phpunit/phpunit/phpunit [SubDirectory1/SubDirectory2/]UnitTest.php
\end{verbatim}

\subsection{ Attention}
Using phpunit Raw: AllTests.php will cause autoloading features to \textbf{fail}.

Remember to run Raw: AllTests.php with \textbf{php} and not \textbf{phpunit}.

For single or individual tests use phpunit.

In a developer checkout, unit tests can also be run by calling <code>../components/bin/horde-components qc</code> from the app's root dir.

\section{ Unit testing a simple library }
I call a library simple to test which does not do IO (db, downloads, interact with devices), does not apply side effects to its input arguments and does not depend on global state.

This is quite straight forward.\newline
For example, look at [Horde\_Argv library's test suite\footnote{https://github.com/horde/Argv/tree/master/test/Horde/Argv|the}].

The logic is easy to test as all the classes get their complete state from input to methods or the constructor. You can just insert what you want to test and look at the results. Some very minimal test setup is done in the base class Horde\_Argv\_TestCase from which the actual test cases are derived. Each test case may have a setUp method before the individual tests are run. This method sets up the environment with the unit under test. Look at the ParserTest\footnote{http://example.com/index.php?page=ParserTest}. In setUp, the parser is configured with commandline switches it should recognize. Then, the individual tests interact with the parser. Individual assertions which are non-trivial and reused can be factored out into helper methods like assertRemoved.

\section{ Not so simple libraries}
It gets more complicated the more libraries either mix logic with data retrieval, IO, side effects or if they are essentially adapters to backends. \newline
For example, a lot of code in Horde\_Core is essentially glue between standalone libraries to provide easy to use wrappers and derived classes for horde applications.

\subsection{ database access}
\subsection{ File IO, streams, internet}
\section{ Testing horde applications}
\subsection{ Unit testing well definded subsystems of applications}
\subsection{ Integration tests / end to end tests}
\end{document}
