The question I have here is about how to build in a forward compatible way the problem I am facing. Technically, I know how to do it in the current version (8.5, looking to move to 9 soon), but what is officially supported, seems to not satisfy the needs I have and my fear of course, is to be blocked in the future with my current approach.
I am currently using PHPUnit extension hooks. Pretty much following the same exact example we have in the documentation: https://phpunit.readthedocs.io/en/9.5/extending-phpunit.html For the sake of simplicity, I'll paste a slightly modified version:
<?php declare(strict_types=1);
namespace Vendor;
use PHPUnit\Runner\AfterLastTestHook;
use PHPUnit\Runner\AfterTestFailureHook;
use PHPUnit\Runner\BeforeFirstTestHook;
class TestExtension implements BeforeFirstTestHook, AfterLastTestHook, AfterTestFailureHook
{
public function executeBeforeFirstTest(): void
{
echo 'Testing with configuration value';
}
public function executeAfterLastTest(): void
{
echo 'Second config value is OK!';
}
public function executeAfterTestFailure(string $test, string $message, float $time): void
{
echo "Failed test took $time seconds";
}
}
However, this triggers an awkward result:
PHPUnit 8.5.21 by Sebastian Bergmann and contributors.
Runtime: PHP 8.0.11 with Xdebug 3.0.4
Configuration: tests/unit.xml
Testing with configuration value....Failed test took 0.0055649280548096 seconds.. 6 / 6 (100%)Second config value is OK!
Time: 2.21 seconds, Memory: 38.00 MB
OK (6 tests, 9 assertions)
What I would like to do though, is much closer to what is possible to do with an instance of PHPUnit\Util\Printer which allows to append messages or information about the test and is render as part of the result. Upon failure, I would like to be able to see something along the lines of:
PHPUnit 8.5.21 by Sebastian Bergmann and contributors.
Runtime: PHP 8.0.11 with Xdebug 3.0.4
Configuration: /Users/sebastian.machuca/projects/cloud-dev-vm/bcappvm/codebases/bigcommerce/tests/unit.xml
Testing with configuration value......
Time: 47.63 seconds, Memory: 132.50 MB
There were 2 errors:
1) Vendor\Tests\TestMyClass::testSomeMethod
Failed test took 0.25880694389343 seconds
The problem here is that PHPUnit\Framework\TestListener is considered to be @deprecated and PHPUnit\Util\Printer is considered to be @internal with a clear "This class is not covered by the backward compatibility promise for PHPUnit" message.
So, again, the question here is about how to build in a forward compatible way. How could achieve the equivalent behaviour that today is possible with Printer Listeners, but using non internal and non deprecated functionality?