<?php
use Testify\Testify;
$testify = new Testify('test of global values $str and $num');
$testify->test('first test', function($testify) {
$num = $testify->data->num;
$str = $testify->data->str;
$testify->assertEquals($num, 2);
$testify->assertEquals($str, '-');
});
$testify->test('second test', function($testify) {
$num = $testify->data->num;
$str = $testify->data->str;
$testify->assertEquals($num, 3);
$testify->assertEquals($str, '--');
});
// called before the tests
$testify->before(function($testify) {
$testify->data->num = 1;
$testify->data->str = '-';
});
// called before every test
$testify->beforeEach(function($testify) {
++$testify->data->num;
});
// called after every test
$testify->afterEach(function($testify) {
$testify->data->str .= '-';
});
// called after tests
$testify->after(function($testify) {
$num = $testify->data->num;
$str = $testify->data->str;
echo 'Values at the end of tests: ' .
$str . ' and ' . $num . PHP_EOL;
});
// run the tests and echo results
$testify->run();
Result
Values at the end of tests: --- and 3
--------------------------------------------------------------------------------
test of global values $str and $num [pass]
--------------------------------------------------------------------------------
[pass] first test {pass 2 / fail 0}
[pass] assertEquals()
line 19, testify2.php
$testify->assertEquals($num, 2);
[pass] assertEquals()
line 20, testify2.php
$testify->assertEquals($str, '-');
--------------------------------------------------------------------------------
[pass] second test {pass 2 / fail 0}
[pass] assertEquals()
line 26, testify2.php
$testify->assertEquals($num, 3);
[pass] assertEquals()
line 27, testify2.php
$testify->assertEquals($str, '--');
================================================================================
Tests: [pass], {pass 4 / fail 0}, 100% success