Пример #1
0
/**
 * Start a test case.
 *
 * @param string $title A string to describe this test case.
 * @param function $fn Default is null(which means pending).
 * @return object TestAPI object.
 */
function it($title, $fn = null)
{
    $case = new TestCase($title, $fn);
    if (!$fn) {
        Util::set_default_filename_and_lineno($case, debug_backtrace());
    }
    Preview::$world->current()->add($case);
    return new TestAPI($case);
}
Пример #2
0
/**
 * create a test case
 * $title is optional. if first param is a closure,
 * $title will be set to empty string
 * and $fn will be that closure.
 *
 * @param string $title
 * @param function|null $fn
 * @return object TestAPI object
 */
function test($title, $fn = null)
{
    if (empty($fn) and $title instanceof \Closure) {
        $fn = $title;
        $title = "";
    }
    $case = new TestCase($title, $fn);
    if (!$fn) {
        Util::set_default_filename_and_lineno($case, debug_backtrace());
    }
    $case->set_parent(Preview::$world->current());
    return new TestAPI($case);
}
Пример #3
0
/**
 * Load an array of tests (a test suite)
 *
 * @param string|array $title, title of a test suite or an array of tests
 * @param array|null $options, an array of tests
 *        key for hooks: "before", "after", "before each", "after each",
 *        other keys are for test cases.
 */
function export($title, $options = null)
{
    if (is_null($options) and is_array($title)) {
        $options = $title;
        $title = "";
    }
    $suite = new TestSuite($title, function () {
    });
    Util::set_default_filename_and_lineno($suite, debug_backtrace());
    // step into test suite.
    $suite->set_parent(Preview::$world->current());
    Preview::$world->push($suite);
    // add hooks
    $hook_names = array("before", "after", "before each", "after each");
    foreach ($hook_names as $hook_name) {
        if (isset($options[$hook_name])) {
            $hooks = $options[$hook_name];
            if (!is_array($options[$hook_name])) {
                $hooks = array($hooks);
            }
            $add_hook = "add_" . str_replace(" ", "_", $hook_name) . "_hook";
            foreach ($hooks as $hook) {
                $suite->{$add_hook}($hook);
            }
        }
    }
    // add tests and child test suites.
    foreach ($options as $title => $fn) {
        if (in_array($title, $hook_names)) {
            continue;
        }
        if (is_array($fn)) {
            export($title, $fn);
        } else {
            $suite->add(new TestCase($title, $fn));
        }
    }
    // step out test suite
    Preview::$world->pop();
}
Пример #4
0
 /**
  * Create a TestSuite object,
  * add it to groups if there's any and
  * skip it if "skip" specified.
  *
  * @param string $param
  * @return null
  */
 private function create_suite($title, $groups)
 {
     $suite = new TestSuite($title, function () {
     });
     Util::set_default_filename_and_lineno($suite, debug_backtrace());
     $this->add_test_to_groups_and_maybe_skip($suite, $groups);
     return $suite;
 }