test_case.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 709 行 · 第 1/2 页
PHP
709 行
/**
* @deprecated
*/
function sendMessage($message) {
$this->_reporter->PaintMessage($message);
}
/**
* Accessor for the number of subtests including myelf.
* @return integer Number of test cases.
* @access public
* @static
*/
function getSize() {
return 1;
}
}
/**
* Helps to extract test cases automatically from a file.
*/
class SimpleFileLoader {
/**
* 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
* test case classes.
* @return TestSuite The new test suite.
* @access public
*/
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;
}
/**
* 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.
* @access private
*/
function _makeFileVariablesGlobal($existing, $new) {
$globals = array_diff(array_keys($new), array_keys($existing));
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
* twice anyway.
* @param string $test_file File name with classes.
* @access private
*/
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];
}
/**
* Calculates the incoming test cases. Skips abstract
* and ignored classes.
* @param array $candidates Candidate classes.
* @return array New classes which are test
* cases that shouldn't be ignored.
* @access public
*/
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;
}
/**
* 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
* test cases.
* @access public
*/
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;
}
}
/**
* This is a composite test class for combining
* test cases and other RunnableTest classes into
* a group test.
* @package SimpleTest
* @subpackage UnitTester
*/
class TestSuite {
var $_label;
var $_test_cases;
/**
* Sets the name of the test suite.
* @param string $label Name sent at the start and end
* of the test.
* @access public
*/
function TestSuite($label = false) {
$this->_label = $label;
$this->_test_cases = array();
}
/**
* 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.
* @access public
*/
function getLabel() {
if (! $this->_label) {
return ($this->getSize() == 1) ?
get_class($this->_test_cases[0]) : get_class($this);
} else {
return $this->_label;
}
}
/**
* @deprecated
*/
function addTestCase(&$test_case) {
$this->_test_cases[] = &$test_case;
}
/**
* @deprecated
*/
function addTestClass($class) {
if (TestSuite::getBaseTestCase($class) == 'testsuite') {
$this->_test_cases[] = &new $class();
} else {
$this->_test_cases[] = $class;
}
}
/**
* 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
* case implementing the
* runnable test interface.
* @access public
*/
function add(&$test_case) {
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;
}
}
/**
* @deprecated
*/
function addTestFile($test_file) {
$this->addFile($test_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
* test case classes.
* @access public
*/
function addFile($test_file) {
$extractor = new SimpleFileLoader();
$this->add($extractor->load($test_file));
}
/**
* Delegates to a visiting collector to add test
* files.
* @param string $path Path to scan from.
* @param SimpleCollector $collector Directory scanner.
* @access public
*/
function collect($path, &$collector) {
$collector->collect($this, $path);
}
/**
* Invokes run() on all of the held test cases, instantiating
* them if necessary.
* @param SimpleReporter $reporter Current test reporter.
* @access public
*/
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();
}
/**
* Number of contained test cases.
* @return integer Total count of cases in the group.
* @access public
*/
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;
}
/**
* Test to see if a class is derived from the
* SimpleTestCase class.
* @param string $class Class name.
* @access public
* @static
*/
function getBaseTestCase($class) {
while ($class = get_parent_class($class)) {
$class = strtolower($class);
if ($class == 'simpletestcase' || $class == 'testsuite') {
return $class;
}
}
return false;
}
}
/**
* @package SimpleTest
* @subpackage UnitTester
* @deprecated
*/
class GroupTest extends TestSuite { }
/**
* This is a failing group test for when a test suite hasn't
* loaded properly.
* @package SimpleTest
* @subpackage UnitTester
*/
class BadTestSuite {
var $_label;
var $_error;
/**
* Sets the name of the test suite and error message.
* @param string $label Name sent at the start and end
* of the test.
* @access public
*/
function BadTestSuite($label, $error) {
$this->_label = $label;
$this->_error = $error;
}
/**
* Accessor for the test name for subclasses.
* @return string Name of the test.
* @access public
*/
function getLabel() {
return $this->_label;
}
/**
* Sends a single error to the reporter.
* @param SimpleReporter $reporter Current test reporter.
* @access public
*/
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();
}
/**
* Number of contained test cases. Always zero.
* @return integer Total count of cases in the group.
* @access public
*/
function getSize() {
return 0;
}
}
/**
* @package SimpleTest
* @subpackage UnitTester
* @deprecated
*/
class BadGroupTest extends BadTestSuite { }
?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?