Without CLI


#Without CLI

The PHP library is designed for use from the command line. It provides output specifically for this purpose.

However, you can run automated tests outside of the console.

#In a simple PHP file

Copied!
1require_once './vendor/autoload.php';
2 
3use Tests\Products as ProductsTests;
4 
5$productsTests = new ProductsTests();
6$productsTests->run();
7 
8$results = json_decode($productsTests->results(), true);
9 
10echo '<pre>';
11var_dump($results['stats'], JSON_PRETTY_PRINT);
12echo '</pre>';

#Follow a suite while it runs

run() only returns once the whole suite has finished. To report progress before that, such as "3/10 passed" in a UI, set onAfterTest. It is called once per test, right after the test's state is settled:

Copied!
1$productsTests = new ProductsTests();
2 
3$productsTests->onAfterTest = function (TestsSuite $suite, array $test): void {
4 // $test['state'] is 'pass', 'fail', 'skip', 'skipped' or 'todo'
5 // $test['title'], and $test['time'] in ms once the test has run
6 saveProgress($suite->results(false)['stats'], $test['title'], $test['state']);
7};
8 
9$productsTests->run();

The callback returns nothing. Any exception it throws is swallowed, so progress reporting can never break a run.