\documentclass{article}
\usepackage{ulem}
\pagestyle{headings}
\begin{document}
\part{Writing PHP Tests}
.phpt stands for "php test" and is a specialized file format that consists of 4 standard sections, and two optional sections (marked by *).  They are as follows:

\renewcommand{\labelenumi}{\arabic{enumi}}
\begin{enumerate}
\item{TEST - the name of the test file (informational)}
\item{SKIPIF - PHP code that determines whether this test should be run, and echoes "skip reason", more at (a)}
\item{ARGS - pass in command-line arguments to php pear run-tests does not yet implement GET, POST, or INI}
\item{FILE - the actual test file, this is standard PHP}
\item{EXPECT/EXPECTF/EXPECTREGEX - plain test, the expected output of the file.  EXPECTF and EXPECTREGEX are used to allow looser validation.}
\end{enumerate}
a simple test is:

\begin{verbatim}

--TEST--
simple test
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == "WIN") {
    echo "skip cannot run on windows";
}
echo "warn you might have left the iron plugged in";
?>
--FILE--
<?php
echo "tests done";
?>
--EXPECT--
tests done
\end{verbatim}

If you run this command on windows

\begin{verbatim}

C:\> pear run-tests simpletest.phpt
SKIP (reason: cannot run on windows)
\end{verbatim}

if you run it on any other OS

\begin{verbatim}

~ pear run-tests simpletest.phpt
PASS simple test [simpletest.phpt] (warn: you might have left the iron plugged in)
\end{verbatim}

This test fails:

\begin{verbatim}

--TEST--
fail test
--SKIPIF--
--FILE--
<?php
echo "wrong";
?>
--EXPECT--
tests done
\end{verbatim}

\begin{verbatim}

C:\> pear run-tests failtest.phpt
FAIL fail test [failtest.phpt]
\end{verbatim}

and creates 3 files:

\begin{verbatim}

failtest.php:
<?php
echo "wrong";
?>

failtest.exp:
tests done

failtest.out:
wrong

failtest.diff:
001- tests done
001+ wrong
\end{verbatim}

Most tests rely upon var\_dump(), echo, and other display methods to validate the test like so:

\begin{verbatim}

--TEST--
old way
--SKIPIF--
--FILE--
<?php
var_dump(array(2, 'hi'));
?>
--EXPECT--
array(2) {
  [0]=>
  int(3)
  [1]=>
  string(2) "hi"
}
\end{verbatim}

The method I have been using for testing PEAR is more familiar to users of phpunit.

\begin{verbatim}

--TEST--
new way
--SKIPIF--
--FILE--
<?php
require_once dirname(__FILE__) . '/phpt_test.php.inc';
$phpunit = new PEAR_PHPTest;
$a = array(2, 'hi');
$phpunit->assertEquals(array(2, 'hi'), $a, 'first array');
echo 'tests done';
?>
--EXPECT--
tests done
\end{verbatim}

When a test succeeds, there is no output, but in this test:

\begin{verbatim}

--TEST--
new way
--SKIPIF--
--FILE--
<?php
require_once dirname(__FILE__) . '/phpt_test.php.inc';
$phpunit = new PEAR_PHPTest;
$a = array(3, 'hi');
$phpunit->assertEquals(array(2, 'hi'), $a, 'first array');
echo 'tests done';
?>
--EXPECT--
tests done
\end{verbatim}

if you have Text\_Diff installed, the .out file would contain:

\begin{verbatim}

Test Failure: "first array"
 in C:\devel\newtest.php line 4
Diff of expecting/received:
@@ -1,4 +1,4 @@
 array (
-  0 => 2,

+   0 => 3,

   1 => 'hi',
 )
\end{verbatim}

otherwise, it would contain:

\begin{verbatim}

Test Failure: "first array"
 in C:\devel\newtest.php line 4
Expecting:
array (
  0 => 2,
  1 => 'hi',
)
Received:
array (
  0 => 3,
  1 => 'hi',
)
\end{verbatim}

This makes it really easy to both debug and create new tests/modify old ones.  If array(3, 'hi') is correct, all you have to do is cut and paste the output into your test. An extreme example of where I have done this is in pear-core/tests/PEAR\_DependencyDB/test\_rebuildDB.phpt

An example of how this flexibility is better than a simple EXPECTF is in pear-core/tests/PEAR\_Downloader\_Package/test\_initialize\_abstractpackage.phpt in this section:

\begin{verbatim}

$phpunit->assertEquals(array (
  0 =>
  array (
    0 => 3,
    1 => '+ tmp dir created at ' . $dp->_downloader->getDownloadDir(),
  ),
), $fakelog->getLog(), 'log messages');
\end{verbatim}

here, I wanted to make sure that not only was this + tmp dir message logged, but that the directory created was in fact the one from our instance of PEAR\_Downloader.

\end{document}
