I'd want to run my tests from a controller and display the output in the browser.
I tried with this code:
<?php namespace App\Controllers;
class Home extends BaseController
{
/**
* Run tests and display output.
*/
public function test(): string
{
$out = [];
exec('./vendor/bin/phpunit', $out, $ret);
return implode('\n', $out);
}
}
But when I go to localhost:8080/Home/test it return an empty page.
The $ret variable contains 1 after the exec, and the $out array remains empty.
If I call ./vendor/bin/phpunit from the command line it works correctly.
I'm pretty sure that it's a matter of paths and maybe permissions. I tried also the paths ../vendor/bin/phpunit and ../../vendor/bin/phpunit but nothing changes.
UPDATE:
changing the content of the method to
return `./vendor/bin/phpunit`;
throws an exception because it return null instead of a string.
Putting phpunit and phpunit.bat inside the public folder and changing the code to
return `phpunit`;
return the string Could not open input file: [project path]\public\/../phpunit/phpunit/phpunit
UPDATE 2:
the last error is solved by copying the folder vendor/phpunit one level in the root of the project. But still it returns null.