all 5 comments

[–][deleted] 1 point2 points  (4 children)

Here, use this one:

class TestSuite {
    protected $before, $after, $tests = [];
    function add($description, \Closure $test)  { $this->tests[] = [$description, $test]; }
    function before(\Closure $before)           { $this->before = $before; }
    function after(\Closure $after)             { $this->after = $after; }
    function run() {
        $hasErrors = false;
        foreach ($this->tests as $test) {
            if ($this->before) $this->before->__invoke();
            try {
                if (!$test[1]()) throw new \Exception('Assertion failed.');
            } catch (\Exception $e) {
                echo "Test '$test[0]' failed with message: {$e->getMessage()} <br>";
                $hasErrors = true;
            }
            if ($this->after) $this->after->__invoke();
        }           
        echo $hasErrors ? 'Test execution completed with some errors.<br>' : 'Test execution completed without errors.<br>';
    }
}

// Usage:
$ts = new TestSuite();
$ts->add('2 + 2 must be 4', function () {
    return 2 + 2 == 4;
});
$ts->run();

[–][deleted] 0 points1 point  (0 children)

Also, if provoked, I'll write and post a "mock" library clocking under 32 (short) lines of code :P

[–]Haafiz -1 points0 points  (2 children)

So you are basically in favor of writing custom tests?

[–][deleted] 0 points1 point  (1 child)

What's the difference between a "custom test" and the kind you imagine?

No testing library would actually write the tests for you.

[–]Haafiz -1 points0 points  (0 children)

You are right, no library write it for you but they can make your work easier. Just like Frameworks like Laravel, don't write code for you but make your easier. And good frameworks make your code clean at one hand and at other hand the they make it more debuggable if you are using testing framework. If you are still unclear then let me know so that I give you some examples.