function gentree($spy, $max_depth, $describes, $methods = array()) { $generate_test_block = function ($block_method, $path, $index, $spy) { array_push($path, $block_method, $index); $spy_method_name = implode(".", $path); if ($block_method == 'it') { call_user_func($block_method, $spy_method_name, array($spy, $spy_method_name)); } else { call_user_func($block_method, array($spy, $spy_method_name)); } }; $generate = function ($depth, $path) use(&$generate, $describes, $methods, &$generate_test_block, $spy, $max_depth) { foreach ($methods as $block_method => $num) { foreach (range(1, $num) as $index) { $generate_test_block($block_method, $path, $index, $spy); } } if ($depth < $max_depth) { foreach (range(1, $describes) as $index) { describe("describe_{$index}", function () use(&$generate, $depth, $path, $index) { $generate($depth + 1, array_merge($path, array("describe", "{$index}"))); }); } } }; return suite('Root', function ($ctx) use($generate) { $generate(1, array()); }); }
<?php namespace Preview\DSL\Qunit; require_once 'stack.php'; require_once __DIR__ . '/../ok.php'; suite("Stack"); setup(function () { $this->stack = new \Stack(array(1, 2, 3)); }); test("#size returns the size of stack", function () { ok($this->stack->size() == 3); }); test("#peek eturns the last element", function () { ok($this->stack->peek() == 3); }); test("#push pushes an element to stack", function () { $this->stack->push(4); ok($this->stack->peek() == 4); ok($this->stack->size() == 4); }); test("#pop pops out the last element", function () { ok($this->stack->pop() == 3); ok($this->stack->size() == 2); });
// Level L1:Level R2:nested 1 expect($result->totalTests())->to->eql(2); }); }); }); describe('Error Capture and Reporting', function ($ctx) { before(function ($ctx) { $ctx->spy = $spy = Mockery::mock()->shouldIgnoreMissing(); $ctx->listener = Mockery::mock('Matura\\Events\\Listener')->shouldIgnoreMissing(); $ctx->suite = suite('Fixture', function ($inner_ctx) use($spy, $ctx) { $ctx->before_all = before_all(array($spy, 'before_all')); $ctx->after_all = after_all(array($spy, 'after_all')); $ctx->after = after(array($spy, 'after')); $ctx->before = before(array($spy, 'before')); $ctx->describe = describe('Inner', function ($inner_ctx) use($spy, $ctx) { $ctx->inner_before_all = before_all(array($spy, 'inner_before_all')); $ctx->inner_after_all = after_all(array($spy, 'inner_after_all')); $ctx->inner_after = after(array($spy, 'inner_after')); $ctx->inner_before = before(array($spy, 'inner_before')); $ctx->test = it('should have a test case', array($spy, 'it')); }); }); $ctx->suite_runner = new SuiteRunner($ctx->suite, new ResultSet()); $ctx->suite_runner->addListener($ctx->listener); }); describe('At the Suite Level', function ($ctx) { it('should capture before_all errors', function ($ctx) { $ctx->spy->shouldReceive('before_all')->once()->andThrow('\\Exception'); $ctx->suite_runner->run(); $failures = $ctx->suite_runner->getResultSet()->getFailures(); expect($failures)->to->have->length(1);
require_once 'stack.php'; require_once __DIR__ . '/../ok.php'; suite("Stack", function () { setup(function () { $this->stack = new \Stack(array(1, 2, 3)); }); suite("#size", function () { test("returns the size of stack", function () { ok($this->stack->size() == 3); }); }); suite("#peek", function () { test("returns the last element", function () { ok($this->stack->peek() == 3); }); }); suite("#push", function () { test("pushes an element to stack", function () { $this->stack->push(4); ok($this->stack->peek() == 4); ok($this->stack->size() == 4); }); }); suite("#pop", function () { test("pops out the last element", function () { ok($this->stack->pop() == 3); ok($this->stack->size() == 2); }); }); });
<?php namespace Preview\DSL\Qunit; require_once __DIR__ . '/../ok.php'; suite("Create a test suite"); test("Run a test", function () { ok(true); }); test("Run a another test", function () { ok(true); }); // test with no description test(function () { ok(true); }); suite("Create a another test suite, " . "So the following test case belongs to this one"); test("A test case for the new suite", function () { ok(true); });
suite("suite_setup", function () { suite_setup(function () { $this->usage = "run suite_setup the current test suite"; }); suite_setup(function () { $this->note_1 = "suite_setup hooks are run in order"; }); suite_setup(function () { $this->ref = new \stdClass(); $this->ref->name = "wenjun.yan"; $this->value = "string"; }); test("can access the variable set in suite_setup hooks", function () { ok($this->note_1); ok($this->note_2); ok($this->value); ok($this->ref->name); $this->value = null; $this->ref->name = null; }); test("suite_setup hooks run only once in current test suite", function () { /* * run tests in order, this will pass. */ ok($this->value); // string is passed by value ok(empty($this->ref->name)); // object is passed by "ref". }); suite_setup(function () { $this->note_2 = "wherever you put the suite_setup each hook, " . "it will run suite_setup this suite"; }); });
<?php /* * How to use teardown */ namespace Preview\DSL\TDD; require_once __DIR__ . '/../ok.php'; suite("teardown", function () { teardown(function () { $this->usage = "run teardown each test case"; }); teardown(function () { $this->note = "teardown hooks are run in order"; }); });
<?php /* * How to use suite_teardown. */ namespace Preview\DSL\TDD; require_once __DIR__ . '/../ok.php'; suite("suite_teardown", function () { suite_teardown(function () { $this->usage = "run suite_teardown the current test suite"; }); suite_teardown(function () { $this->note = "suite_teardown hooks are run in order"; }); });
<?php /* * This file is part of PHPTest * * Copyright (c) 2014 Andrew Lawson <http://adlawson.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace PhpTest\example\functional; suite('suite name', function () { test('test name', function () { // test something }); suite('nested suite', function () { test('nested test', function () { // test something }); test('nested test with args', function ($arg) { // each args array creates a separate test }, [['foo'], ['bar']]); }); });
<?php namespace Preview\DSL\Qunit; require_once __DIR__ . '/../ok.php'; suite("A grouped test suite")->group("sample group"); test("A skipped test case", function () { ok(true); })->group("test group", "another group");
<?php namespace Preview\DSL\Qunit; require_once __DIR__ . '/../ok.php'; suite("Create a test suite"); test("A pending test");
<?php namespace Preview\DSL\TDD; require_once 'ok.php'; suite("array_pop", function () { suite_setup(function () { // connect to database. }); // run before each test case; setup(function () { $this->arr = array(1, 2); }); // use the variable set in the setup function test("return last element", function () { ok(array_pop($this->arr) == 2); }); // skip this case test("return null for empty string", function () { $tmp = array_pop(array()); ok(empty($tmp)); })->skip(); // pending test case test("array_pop a string?"); });
<?php namespace Preview\DSL\Qunit; require_once __DIR__ . '/../ok.php'; suite("teardown"); teardown(function () { $this->usage = "this will function be called after each test case"; }); teardown(function () { $this->note_1 = "you can have multiple teardown"; }); teardown(function () { $this->note_2 = "teardown functions are run in order"; }); test("Run a test", function () { ok(true); }); test("Run a another test", function () { ok(true); }); teardown(function () { $this->note_3 = "It dosen't even matter where you put teardown function"; });
use Matura\Exceptions\Exception; use Matura\Test\User; use Matura\Test\Group; /** * Tests the construction of our test graph via our DSL. */ describe('Matura', function ($ctx) { before(function ($ctx) { // Officially, nesting suites in this manner is unsupported. // A Suite block is automatically created for every test file. $ctx->suite = suite('Suite', function () { describe('Fixture', function ($ctx) { it('TestMethod', function ($ctx) { }); before(function ($ctx) { }); before_all(function ($ctx) { }); }); }); }); describe('Suite', function ($ctx) { before(function ($ctx) { $ctx->describe = $ctx->suite->find('Suite:Fixture'); }); it('should be a Suite Block', function ($ctx) { expect($ctx->suite)->to->be->an('Matura\\Blocks\\Suite'); }); it('should have a name', function ($ctx) { expect($ctx->suite->getName())->to->eql('Suite'); });
<?php namespace Preview\DSL\Qunit; require_once __DIR__ . '/../ok.php'; suite("setup"); setup(function () { $this->usage = "this will function be called before each test case"; }); setup(function () { $this->note_1 = "you can have multiple setup"; }); setup(function () { $this->note_2 = "setup functions are run in order"; }); test("have access to vars defined in setups", function () { ok($this->note_1); ok($this->note_2); ok($this->note_3); }); test("Run a another test", function () { ok(true); }); setup(function () { $this->note_3 = "It dosen't even matter where you put setup function"; });
<?php namespace Preview\DSL\Qunit; require_once __DIR__ . '/../ok.php'; suite("A skipped test suite")->skip(); test("A skipped test case", function () { ok(true); })->skip();
require_once 'ok.php'; suite("array_pop"); // run before each test case; setup(function () { $this->arr = array(1, 2); }); // use the variable set in the setup function test("return last element", function () { ok(array_pop($this->arr) == 2); })->skip(); // skip this case test("return null for empty string", function () { $empty_array = array(); ok(is_null(array_pop($empty_array))); }); // test case without description; test(function () { array_pop($this->arr); ok(count($this->arr) == 1); }); // pending test case test("array_pop a string?"); // another suite; suite("array_push"); // following test cases now belong to the "array_push" suite test("add one element to array", function () { $target = array(1, 2, 3, 4); array_push($target, 5); ok(count($target) == 5); });
<?php /* * Basic usage */ namespace Preview\DSL\TDD; require_once __DIR__ . '/../ok.php'; suite("An test suite.", function () { test("Run a test case", function () { ok(true); }); suite("Nested test suite.", function () { test("Run a test case", function () { ok(true); }); }); });
suite("setup", function () { setup(function () { $this->usage = "run before each test case"; }); setup(function () { $this->note_1 = "setup hooks are run in order"; }); setup(function () { $this->ref = new \stdClass(); $this->ref->name = "wenjun.yan"; $this->value = "string"; }); test("can access the variable set in setup hooks", function () { ok($this->note_1); ok($this->note_2); ok($this->value); ok($this->ref->name); $this->value = null; $this->ref->name = null; }); test("setup hooks are run before each test case", function () { // $this-value and $this->ref are reassigned. ok($this->value); // string is passed by value ok($this->ref->name); // object is passed by "ref". }); setup(function () { $this->note_2 = "wherever you put the before each hook, " . "it will run before each test case"; }); });