123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709 |
- <?php
- 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');
- if (version_compare(phpversion(), '5') >= 0) {
- require_once(dirname(__FILE__) . '/exceptions.php');
- require_once(dirname(__FILE__) . '/reflection_php5.php');
- } else {
- require_once(dirname(__FILE__) . '/reflection_php4.php');
- }
- if (! defined('SIMPLE_TEST')) {
-
- define('SIMPLE_TEST', dirname(__FILE__) . DIRECTORY_SEPARATOR);
- }
- class SimpleTestCase {
- var $_label = false;
- var $_reporter;
- var $_observers;
- var $_should_skip = false;
-
- function SimpleTestCase($label = false) {
- if ($label) {
- $this->_label = $label;
- }
- }
-
- function getLabel() {
- return $this->_label ? $this->_label : get_class($this);
- }
-
- function skip() {
- }
-
- function skipIf($should_skip, $message = '%s') {
- if ($should_skip && ! $this->_should_skip) {
- $this->_should_skip = true;
- $message = sprintf($message, 'Skipping [' . get_class($this) . ']');
- $this->_reporter->paintSkip($message . $this->getAssertionLine());
- }
- }
-
- function skipUnless($shouldnt_skip, $message = false) {
- $this->skipIf(! $shouldnt_skip, $message);
- }
-
- function &createInvoker() {
- $invoker = &new SimpleErrorTrappingInvoker(new SimpleInvoker($this));
- if (version_compare(phpversion(), '5') >= 0) {
- $invoker = &new SimpleExceptionTrappingInvoker($invoker);
- }
- return $invoker;
- }
-
- function run(&$reporter) {
- $context = &SimpleTest::getContext();
- $context->setTest($this);
- $context->setReporter($reporter);
- $this->_reporter = &$reporter;
- $started = false;
- foreach ($this->getTests() as $method) {
- if ($reporter->shouldInvoke($this->getLabel(), $method)) {
- $this->skip();
- if ($this->_should_skip) {
- break;
- }
- if (! $started) {
- $reporter->paintCaseStart($this->getLabel());
- $started = true;
- }
- $invoker = &$this->_reporter->createInvoker($this->createInvoker());
- $invoker->before($method);
- $invoker->invoke($method);
- $invoker->after($method);
- }
- }
- if ($started) {
- $reporter->paintCaseEnd($this->getLabel());
- }
- unset($this->_reporter);
- return $reporter->getStatus();
- }
-
- function getTests() {
- $methods = array();
- foreach (get_class_methods(get_class($this)) as $method) {
- if ($this->_isTest($method)) {
- $methods[] = $method;
- }
- }
- return $methods;
- }
-
- function _isTest($method) {
- if (strtolower(substr($method, 0, 4)) == 'test') {
- return ! SimpleTestCompatibility::isA($this, strtolower($method));
- }
- return false;
- }
-
- function before($method) {
- $this->_reporter->paintMethodStart($method);
- $this->_observers = array();
- }
-
- function setUp() {
- }
-
- function tearDown() {
- }
-
- function after($method) {
- for ($i = 0; $i < count($this->_observers); $i++) {
- $this->_observers[$i]->atTestEnd($method, $this);
- }
- $this->_reporter->paintMethodEnd($method);
- }
-
- function tell(&$observer) {
- $this->_observers[] = &$observer;
- }
-
- function pass($message = "Pass") {
- if (! isset($this->_reporter)) {
- trigger_error('Can only make assertions within test methods');
- }
- $this->_reporter->paintPass(
- $message . $this->getAssertionLine());
- return true;
- }
-
- function fail($message = "Fail") {
- if (! isset($this->_reporter)) {
- trigger_error('Can only make assertions within test methods');
- }
- $this->_reporter->paintFail(
- $message . $this->getAssertionLine());
- return false;
- }
-
- function error($severity, $message, $file, $line) {
- if (! isset($this->_reporter)) {
- trigger_error('Can only make assertions within test methods');
- }
- $this->_reporter->paintError(
- "Unexpected PHP error [$message] severity [$severity] in [$file line $line]");
- }
-
- function exception($exception) {
- $this->_reporter->paintException($exception);
- }
-
- function signal($type, &$payload) {
- if (! isset($this->_reporter)) {
- trigger_error('Can only make assertions within test methods');
- }
- $this->_reporter->paintSignal($type, $payload);
- }
-
- function assert(&$expectation, $compare, $message = '%s') {
- if ($expectation->test($compare)) {
- return $this->pass(sprintf(
- $message,
- $expectation->overlayMessage($compare, $this->_reporter->getDumper())));
- } else {
- return $this->fail(sprintf(
- $message,
- $expectation->overlayMessage($compare, $this->_reporter->getDumper())));
- }
- }
-
- function assertExpectation(&$expectation, $compare, $message = '%s') {
- return $this->assert($expectation, $compare, $message);
- }
-
- function getAssertionLine() {
- $trace = new SimpleStackTrace(array('assert', 'expect', 'pass', 'fail', 'skip'));
- return $trace->traceMethod();
- }
-
- function dump($variable, $message = false) {
- $dumper = $this->_reporter->getDumper();
- $formatted = $dumper->dump($variable);
- if ($message) {
- $formatted = $message . "\n" . $formatted;
- }
- $this->_reporter->paintFormattedMessage($formatted);
- return $variable;
- }
-
- function sendMessage($message) {
- $this->_reporter->PaintMessage($message);
- }
-
- function getSize() {
- return 1;
- }
- }
- class SimpleFileLoader {
-
- function &load($test_file) {
- $existing_classes = get_declared_classes();
- $existing_globals = get_defined_vars();
- include_once($test_file);
- $new_globals = get_defined_vars();
- $this->_makeFileVariablesGlobal($existing_globals, $new_globals);
- $new_classes = array_diff(get_declared_classes(), $existing_classes);
- if (empty($new_classes)) {
- $new_classes = $this->_scrapeClassesFromFile($test_file);
- }
- $classes = $this->selectRunnableTests($new_classes);
- $suite = &$this->createSuiteFromClasses($test_file, $classes);
- return $suite;
- }
-
- function _makeFileVariablesGlobal($existing, $new) {
- $globals = array_diff(array_keys($new), array_keys($existing));
- foreach ($globals as $global) {
- $_GLOBALS[$global] = $new[$global];
- }
- }
-
- function _scrapeClassesFromFile($test_file) {
- preg_match_all('~^\s*class\s+(\w+)(\s+(extends|implements)\s+\w+)*\s*\{~mi',
- file_get_contents($test_file),
- $matches );
- return $matches[1];
- }
-
- function selectRunnableTests($candidates) {
- $classes = array();
- foreach ($candidates as $class) {
- if (TestSuite::getBaseTestCase($class)) {
- $reflection = new SimpleReflection($class);
- if ($reflection->isAbstract()) {
- SimpleTest::ignore($class);
- } else {
- $classes[] = $class;
- }
- }
- }
- return $classes;
- }
-
- function &createSuiteFromClasses($title, $classes) {
- if (count($classes) == 0) {
- $suite = &new BadTestSuite($title, "No runnable test cases in [$title]");
- return $suite;
- }
- SimpleTest::ignoreParentsIfIgnored($classes);
- $suite = &new TestSuite($title);
- foreach ($classes as $class) {
- if (! SimpleTest::isIgnored($class)) {
- $suite->addTestClass($class);
- }
- }
- return $suite;
- }
- }
- class TestSuite {
- var $_label;
- var $_test_cases;
-
- function TestSuite($label = false) {
- $this->_label = $label;
- $this->_test_cases = array();
- }
-
- function getLabel() {
- if (! $this->_label) {
- return ($this->getSize() == 1) ?
- get_class($this->_test_cases[0]) : get_class($this);
- } else {
- return $this->_label;
- }
- }
-
- function addTestCase(&$test_case) {
- $this->_test_cases[] = &$test_case;
- }
-
- function addTestClass($class) {
- if (TestSuite::getBaseTestCase($class) == 'testsuite') {
- $this->_test_cases[] = &new $class();
- } else {
- $this->_test_cases[] = $class;
- }
- }
-
- function add(&$test_case) {
- $class = null;
- if (! is_string($test_case)) {
- $this->_test_cases[] = &$test_case;
- } elseif (TestSuite::getBaseTestCase($class) == 'testsuite') {
- $this->_test_cases[] = &new $class();
- } else {
- $this->_test_cases[] = $class;
- }
- }
-
- function addTestFile($test_file) {
- $this->addFile($test_file);
- }
-
- function addFile($test_file) {
- $extractor = new SimpleFileLoader();
- $this->add($extractor->load($test_file));
- }
-
- function collect($path, &$collector) {
- $collector->collect($this, $path);
- }
-
- function run(&$reporter) {
- $reporter->paintGroupStart($this->getLabel(), $this->getSize());
- for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) {
- if (is_string($this->_test_cases[$i])) {
- $class = $this->_test_cases[$i];
- $test = &new $class();
- $test->run($reporter);
- unset($test);
- } else {
- $this->_test_cases[$i]->run($reporter);
- }
- }
- $reporter->paintGroupEnd($this->getLabel());
- return $reporter->getStatus();
- }
-
- function getSize() {
- $count = 0;
- foreach ($this->_test_cases as $case) {
- if (is_string($case)) {
- if (! SimpleTest::isIgnored($case)) {
- $count++;
- }
- } else {
- $count += $case->getSize();
- }
- }
- return $count;
- }
-
- function getBaseTestCase($class) {
- while ($class = get_parent_class($class)) {
- $class = strtolower($class);
- if ($class == 'simpletestcase' || $class == 'testsuite') {
- return $class;
- }
- }
- return false;
- }
- }
- class GroupTest extends TestSuite { }
- class BadTestSuite {
- var $_label;
- var $_error;
-
- function BadTestSuite($label, $error) {
- $this->_label = $label;
- $this->_error = $error;
- }
-
- function getLabel() {
- return $this->_label;
- }
-
- function run(&$reporter) {
- $reporter->paintGroupStart($this->getLabel(), $this->getSize());
- $reporter->paintFail('Bad TestSuite [' . $this->getLabel() .
- '] with error [' . $this->_error . ']');
- $reporter->paintGroupEnd($this->getLabel());
- return $reporter->getStatus();
- }
-
- function getSize() {
- return 0;
- }
- }
- class BadGroupTest extends BadTestSuite { }
- ?>
|