Source for file test_case.php
Documentation is available at test_case.php
* Base include file for SimpleTest
* @version $Id: test_case.php 1726 2008-04-08 01:20:10Z lastcraft $
* Includes SimpleTest files and defined the root constant
* for dependent libraries.
require_once(dirname(__FILE__
) .
'/invoker.php');
require_once(dirname(__FILE__
) .
'/errors.php');
require_once(dirname(__FILE__
) .
'/compatibility.php');
require_once(dirname(__FILE__
) .
'/scorer.php');
require_once(dirname(__FILE__
) .
'/expectation.php');
require_once(dirname(__FILE__
) .
'/dumper.php');
require_once(dirname(__FILE__
) .
'/simpletest.php');
require_once(dirname(__FILE__
) .
'/exceptions.php');
require_once(dirname(__FILE__
) .
'/reflection_php5.php');
require_once(dirname(__FILE__
) .
'/reflection_php4.php');
define('SIMPLE_TEST', dirname(__FILE__
) .
DIRECTORY_SEPARATOR);
* Basic test case. This is the smallest unit of a test
* all methods that start with the the string "test" and
* runs them. Working test cases extend this class.
* Sets up the test with no display.
* @param string $label If no test name is given then
* the class name is used.
* Accessor for the test name for subclasses.
* @return string Name of the test.
* This is a placeholder for skipping tests. In this
* method you place skipIf() and skipUnless() calls to
* set the skipping state.
* Will issue a message to the reporter and tell the test
* case to skip if the incoming flag is true.
* @param string $should_skip Condition causing the tests to be skipped.
* @param string $message Text of skip condition.
function skipIf($should_skip, $message =
'%s') {
* Will issue a message to the reporter and tell the test
* case to skip if the incoming flag is false.
* @param string $shouldnt_skip Condition causing the tests to be run.
* @param string $message Text of skip condition.
function skipUnless($shouldnt_skip, $message =
false) {
$this->skipIf(! $shouldnt_skip, $message);
* Used to invoke the single tests.
* @return SimpleInvoker Individual test runner.
* Uses reflection to run every method within itself
* starting with the string "test" unless a method
* @param SimpleReporter $reporter Current test reporter.
* @return boolean True if all tests passed.
function run(&$reporter) {
$context->setTest($this);
$context->setReporter($reporter);
foreach ($this->getTests() as $method) {
if ($reporter->shouldInvoke($this->getLabel(), $method)) {
$reporter->paintCaseStart($this->getLabel());
$invoker->before($method);
$invoker->invoke($method);
$invoker->after($method);
$reporter->paintCaseEnd($this->getLabel());
return $reporter->getStatus();
* Gets a list of test names. Normally that will
* be all internal methods that start with the
* name "test". This method should be overridden
* if you want a different rule.
* @return array List of test names.
* Tests to see if the method is a test that should
* be run. Currently any method that starts with 'test'
* is a candidate unless it is the constructor.
* @param string $method Method name to try.
* @return boolean True if test method.
* Announces the start of the test.
* @param string $method Test method just started.
* Sets up unit test wide variables at the start
* of each test method. To be overridden in
* actual user test cases.
* Clears the data set in the setUp() method call.
* To be overridden by the user in actual user test cases.
* Announces the end of the test. Includes private clean up.
* @param string $method Test method just finished.
function after($method) {
* Sets up an observer for the test end.
* @param object $observer Must have atTestEnd()
function tell(&$observer) {
function pass($message =
"Pass") {
* Sends a fail event with a message.
* @param string $message Message to send.
function fail($message =
"Fail") {
* Formats a PHP error and dispatches it to the
* @param integer $severity PHP error code.
* @param string $message Text of error.
* @param string $file File error occoured in.
* @param integer $line Line number of error.
function error($severity, $message, $file, $line) {
"Unexpected PHP error [$message] severity [$severity] in [$file line $line]");
* Formats an exception and dispatches it to the
* @param Exception $exception Object thrown.
$this->_reporter->paintException($exception);
function signal($type, &$payload) {
$this->_reporter->paintSignal($type, $payload);
* Runs an expectation directly, for extending the
* tests with new expectation classes.
* @param SimpleExpectation $expectation Expectation subclass.
* @param mixed $compare Value to compare.
* @param string $message Message to display.
* @return boolean True on pass
function assert(&$expectation, $compare, $message =
'%s') {
if ($expectation->test($compare)) {
$expectation->overlayMessage($compare, $this->_reporter->getDumper())));
$expectation->overlayMessage($compare, $this->_reporter->getDumper())));
return $this->assert($expectation, $compare, $message);
* Uses a stack trace to find the line of an assertion.
* @return string Line number of first assert*
* method embedded in format string.
$trace =
new SimpleStackTrace(array('assert', 'expect', 'pass', 'fail', 'skip'));
return $trace->traceMethod();
* Sends a formatted dump of a variable to the
* test suite for those emergency debugging
* @param mixed $variable Variable to display.
* @param string $message Message to display.
* @return mixed The original variable.
function dump($variable, $message =
false) {
$formatted =
$dumper->dump($variable);
$formatted =
$message .
"\n" .
$formatted;
$this->_reporter->paintFormattedMessage($formatted);
* Accessor for the number of subtests including myelf.
* @return integer Number of test cases.
* Helps to extract test cases automatically from a file.
* Builds a test suite from a library of test cases.
* The new suite is composed into this one.
* @param string $test_file File name of library with
* @return TestSuite The new test suite.
function &load($test_file) {
include_once($test_file);
$this->_makeFileVariablesGlobal($existing_globals, $new_globals);
if (empty($new_classes)) {
$new_classes =
$this->_scrapeClassesFromFile($test_file);
* Imports new variables into the global namespace.
* @param hash $existing Variables before the file was loaded.
* @param hash $new Variables after the file was loaded.
function _makeFileVariablesGlobal($existing, $new) {
foreach ($globals as $global) {
$_GLOBALS[$global] =
$new[$global];
* Lookup classnames from file contents, in case the
* file may have been included before.
* Note: This is probably too clever by half. Figuring this
* out after a failed test case is going to be tricky for us,
* never mind the user. A test case should not be included
* @param string $test_file File name with classes.
function _scrapeClassesFromFile($test_file) {
preg_match_all('~^\s*class\s+(\w+)(\s+(extends|implements)\s+\w+)*\s*\{~mi',
* Calculates the incoming test cases. Skips abstract
* @param array $candidates Candidate classes.
* @return array New classes which are test
* cases that shouldn't be ignored.
foreach ($candidates as $class) {
if ($reflection->isAbstract()) {
* Builds a test suite from a class list.
* @param string $title Title of new group.
* @param array $classes Test classes.
* @return TestSuite Group loaded with the new
if (count($classes) ==
0) {
$suite =
&new BadTestSuite($title, "No runnable test cases in [$title]");
foreach ($classes as $class) {
$suite->addTestClass($class);
* This is a composite test class for combining
* test cases and other RunnableTest classes into
* Sets the name of the test suite.
* @param string $label Name sent at the start and end
* Accessor for the test name for subclasses. If the suite
* wraps a single test case the label defaults to the name of that test.
* @return string Name of the test.
* Adds a test into the suite by instance or class. The class will
* be instantiated if it's a test suite.
* @param SimpleTestCase $test_case Suite or individual test
* runnable test interface.
function add(&$test_case) {
* Builds a test suite from a library of test cases.
* The new suite is composed into this one.
* @param string $test_file File name of library with
$this->add($extractor->load($test_file));
* Delegates to a visiting collector to add test
* @param string $path Path to scan from.
* @param SimpleCollector $collector Directory scanner.
function collect($path, &$collector) {
$collector->collect($this, $path);
* Invokes run() on all of the held test cases, instantiating
* @param SimpleReporter $reporter Current test reporter.
function run(&$reporter) {
$reporter->paintGroupEnd($this->getLabel());
return $reporter->getStatus();
* Number of contained test cases.
* @return integer Total count of cases in the group.
$count +=
$case->getSize();
* Test to see if a class is derived from the
* @param string $class Class name.
if ($class ==
'simpletestcase' ||
$class ==
'testsuite') {
* This is a failing group test for when a test suite hasn't
* Sets the name of the test suite and error message.
* @param string $label Name sent at the start and end
* Accessor for the test name for subclasses.
* @return string Name of the test.
* Sends a single error to the reporter.
* @param SimpleReporter $reporter Current test reporter.
function run(&$reporter) {
$reporter->paintFail('Bad TestSuite [' .
$this->getLabel() .
'] with error [' .
$this->_error .
']');
$reporter->paintGroupEnd($this->getLabel());
return $reporter->getStatus();
* Number of contained test cases. Always zero.
* @return integer Total count of cases in the group.
Documentation generated on Sun, 04 May 2008 09:22:18 -0500 by phpDocumentor 1.3.0