/** * Constructor * * @param string $name * @param function $fn */ public function __construct($name, $fn) { $this->fn = $fn; $this->name = $name; if (!Preview::is_php53()) { $ref = new \ReflectionFunction($fn); $this->fn = $ref->getClosure(); } }
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()); }); }); });
}); 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); });
$run_hook = "run_{$hook}"; $this->subject->{$add_hook}(function () use(&$run) { $run = true; }); $this->subject->{$run_hook}(); Preview::$world = $old_world; ok($run); }); }); }); shared_example("having hooks for its children", function ($hook) { describe("#run_{$hook}", function () use($hook) { it("should run {$hook} hooks and its parents'", function () use($hook) { $old_world = Preview::$world; $run = false; $parent_run = false; $add_hook = "add_{$hook}_hook"; $run_hook = "run_{$hook}"; $this->parent->{$add_hook}(function () use(&$parent_run) { $parent_run = true; }); $this->subject->{$add_hook}(function () use(&$run) { $run = true; }); $context = new \stdClass(); $this->subject->{$run_hook}($context); Preview::$world = $old_world; ok($run and $parent_run); }); }); });
} 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); }); }); });
<?php namespace Preview\DSL\BDD; use Preview\Preview; require_once 'ok.php'; // for php 5.3 if (Preview::is_php53()) { describe("array_pop", function () { subject(array(0, 1, 2, 3)); it("should return the last item", function ($self) { $end = end($self->subject); ok(array_pop($self->subject) == $end); }); it("should remove the last item from original array", function ($self) { array_pop($self->subject); ok($self->subject == array(0, 1, 2)); }); }); // for php 5.4 and above } else { describe("array_pop", function () { subject(array(0, 1, 2, 3)); it("should return the last item", function () { $end = end($this->subject); ok(array_pop($this->subject) == $end); }); it("should remove the last item from original array", function () { array_pop($this->subject); ok($this->subject == array(0, 1, 2)); });
/** * constructor * * @param string $title * @param function $fn */ public function __construct($title, $fn) { $this->context = new \stdClass(); $this->title = $title; $this->pending = !isset($fn); $this->timer = new Timer(); $this->fn = $fn; if ($fn) { $ref = new \ReflectionFunction($fn); $this->filename = $ref->getFileName(); $this->startline = $ref->getStartLine(); $this->endline = $ref->getEndLine(); if (!Preview::is_php53()) { $this->fn = $ref->getClosure(); } } }
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(); }