test_case.php
来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 669 行 · 第 1/2 页
PHP
669 行
<?php /** * Base include file for SimpleTest * @package SimpleTest * @subpackage UnitTester * @version $Id: test_case.php 163 2008-01-14 04:40:16Z matt $ */ /**#@+ * 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'); 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')) { /** * @ignore */ define('SIMPLE_TEST', dirname(__FILE__) . DIRECTORY_SEPARATOR); } /**#@-*/ /** * Basic test case. This is the smallest unit of a test * suite. It searches for * all methods that start with the the string "test" and * runs them. Working test cases extend this class. * @package SimpleTest * @subpackage UnitTester */ class SimpleTestCase { var $_label = false; var $_reporter; var $_observers; var $_should_skip = false; /** * Sets up the test with no display. * @param string $label If no test name is given then * the class name is used. * @access public */ function SimpleTestCase($label = false) { if ($label) { $this->_label = $label; } } /** * Accessor for the test name for subclasses. * @return string Name of the test. * @access public */ function getLabel() { return $this->_label ? $this->_label : get_class($this); } /** * This is a placeholder for skipping tests. In this * method you place skipIf() and skipUnless() calls to * set the skipping state. * @access public */ function skip() { } /** * 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. * @access public */ 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()); } } /** * 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. * @access public */ function skipUnless($shouldnt_skip, $message = false) { $this->skipIf(! $shouldnt_skip, $message); } /** * Used to invoke the single tests. * @return SimpleInvoker Individual test runner. * @access public */ function &createInvoker() { $invoker = &new SimpleErrorTrappingInvoker(new SimpleInvoker($this)); if (version_compare(phpversion(), '5') >= 0) { $invoker = &new SimpleExceptionTrappingInvoker($invoker); } return $invoker; } /** * Uses reflection to run every method within itself * starting with the string "test" unless a method * is specified. * @param SimpleReporter $reporter Current test reporter. * @return boolean True if all tests passed. * @access public */ 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(); } /** * 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. * @access public */ function getTests() { $methods = array(); foreach (get_class_methods(get_class($this)) as $method) { if ($this->_isTest($method)) { $methods[] = $method; } } return $methods; } /** * 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. * @access protected */ function _isTest($method) { if (strtolower(substr($method, 0, 4)) == 'test') { return ! SimpleTestCompatibility::isA($this, strtolower($method)); } return false; } /** * Announces the start of the test. * @param string $method Test method just started. * @access public */ function before($method) { $this->_reporter->paintMethodStart($method); $this->_observers = array(); } /** * Sets up unit test wide variables at the start * of each test method. To be overridden in * actual user test cases. * @access public */ function setUp() { } /** * Clears the data set in the setUp() method call. * To be overridden by the user in actual user test cases. * @access public */ function tearDown() { } /** * Announces the end of the test. Includes private clean up. * @param string $method Test method just finished. * @access public */ function after($method) { for ($i = 0; $i < count($this->_observers); $i++) { $this->_observers[$i]->atTestEnd($method, $this); } $this->_reporter->paintMethodEnd($method); } /** * Sets up an observer for the test end. * @param object $observer Must have atTestEnd() * method. * @access public */ function tell(&$observer) { $this->_observers[] = &$observer; } /** * @deprecated */ 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; } /** * Sends a fail event with a message. * @param string $message Message to send. * @access public */ 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; } /** * Formats a PHP error and dispatches it to the * reporter. * @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. * @access public */ 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]"); } /** * Formats an exception and dispatches it to the * reporter. * @param Exception $exception Object thrown. * @access public */ function exception($exception) { $this->_reporter->paintException($exception); } /** * @deprecated */ function signal($type, &$payload) { if (! isset($this->_reporter)) { trigger_error('Can only make assertions within test methods'); } $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 * @access public */ 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()))); } } /** * @deprecated */ function assertExpectation(&$expectation, $compare, $message = '%s') { 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. * @access public */ function getAssertionLine() { $trace = new SimpleStackTrace(array('assert', 'expect', 'pass', 'fail', 'skip')); return $trace->traceMethod();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?