use Preview\Preview; use Preview\Configuration; use Preview\Runner; use Preview\Core\TestSuite; use Preview\Reporter\Base as BaseReporter; require_once 'helper.php'; describe("Runner", function () { describe("#run", function () { before_each(function () { $this->old_config = Preview::$config; }); before_each(function () { $this->new_config = new Configuration(); $this->new_config->reporter = new BaseReporter(); }); before_each(function () { $test1 = new TestSuite("test-1", function () { }); $test2 = new TestSuite("test-2", function () { }); $this->runner = new Runner(array($test1, $test2)); }); it("should run tests", function () { Preview::$config = $this->new_config; $results = $this->runner->run(); Preview::$config = $this->old_config; ok(count($results) == 2); ok($results[0]->finished() and $results[1]->finished()); }); }); });
} ok($run); }); }); describe("after_each_hook", function () { it("should run after every test case", function () { $run = false; $this->config->reporter = new BaseReporter(); $this->config->after_each_hook = function () use(&$run) { $run = true; }; $old_config = Preview::$config; Preview::$config = $this->config; $exception_in_sample_code = null; try { $suite = new TestSuite("sample suite", function () { }); $suite->add(new TestCase("sample case", function () { })); $suite->run(); } catch (\Exception $e) { $exception_in_sample_code = $e; } Preview::$config = $old_config; if ($exception_in_sample_code) { throw $exception_in_sample_code; } ok($run); }); }); });
}); qunit\test("c2", function () { })->skip(); qunit\test("c3"); qunit\suite("s2"); qunit\test(function () { }); qunit\test("c5", function () { })->skip(); qunit\test("c6"); $this->results = $this->world->run(); // ------ end test ------- // end new env // and go back to our normal test env Preview::$world = $this->test_world; Preview::$config = $this->test_config; }); it("should have 2 suite", function () { ok(count($this->results) == 2); }); it("first suite should have 3 test cases", function () { ok(count($this->results[0]->cases()) == 3); }); it("second suite should have 3 test cases", function () { ok(count($this->results[1]->cases()) == 3); }); describe("reporter", function () { describe("#before_all", function () { it("should be called once", function () { ok($this->config->reporter->before_all == 1); });
public function run($args = null) { // parse options $br = PHP_EOL; $this->parse($args); $options = $this->cmd->getOptions(); // setup loader and test config, test world Preview::$world = new World(); Preview::$config = new Configuration(); $loader = new Loader(); // get parsed args, update config and load test files. $config_file = "preview.config.php"; if (isset($options["config"])) { $config_file = $options["config"]; } $config_file = realpath($config_file); if (is_file($config_file)) { Preview::$config->load_from_file($config_file); } if (isset($options["help"])) { $this->cmd->showHelp($this->padding); exit(0); } if (isset($options["list-reporters"])) { $reporters = array("spec (defualt)", "dropdown", "tree", "dot", "line", "blank"); foreach ($reporters as $index => $reporter) { $index = $index + 1; echo " {$index}) {$reporter}{$br}"; } echo $br; exit(0); } if (isset($options["reporter"])) { $reporter_class = ucfirst($options['reporter']); $reporter = "\\Preview\\Reporter\\{$reporter_class}"; Preview::$config->reporter = new $reporter(); } if (isset($options["backtrace"])) { Preview::$config->full_backtrace = true; } if (isset($options["no-this"])) { Preview::$config->use_implicit_context = false; } if (isset($options["no-color"])) { Preview::$config->color_support = false; } if (isset($options["with-error"])) { Preview::$config->error_exception = false; } if (isset($options["fail-fast"])) { Preview::$config->fail_fast = true; } if (isset($options["order"])) { Preview::$config->order = true; } if (isset($options["group"])) { Preview::$config->test_groups = explode(",", $options["group"]); } if (isset($options["exclude-group"])) { Preview::$config->exclude_groups = explode(",", $options["exclude-group"]); } if (isset($options["title"])) { Preview::$config->title = $options["title"]; } $files = $this->cmd->getOperands(); // default help message if (empty($options) and empty($files)) { $this->cmd->showHelp($this->padding); exit(0); } // load all test file foreach ($files as $file) { $loader->load($file); } // list groups if (isset($options["list-groups"])) { $groups = Preview::$world->groups(); if (empty($groups)) { echo " No test groups.{$br}{$br}"; exit(0); } foreach ($groups as $index => $group) { $index = $index + 1; echo " {$index}) {$group}{$br}"; } echo $br; exit(0); } $this->execute(); }