Files
watchstate/src/Libs/TestCase.php
2024-09-07 17:15:24 +03:00

73 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Libs;
use Closure;
use Monolog\Handler\TestHandler;
use Throwable;
class TestCase extends \PHPUnit\Framework\TestCase
{
protected TestHandler|null $handler = null;
/**
* Prints logs to the standard output.
*
* @return void
*/
protected function printLogs(): void
{
if (null === $this->handler) {
return;
}
foreach ($this->handler->getRecords() as $logs) {
fwrite(STDOUT, $logs['formatted'] . PHP_EOL);
}
}
/**
* Checks if the given closure throws an exception.
*
* @param Closure $closure
* @param string $reason
* @param Throwable|string $exception Expected exception class
* @param string $exceptionMessage (optional) Exception message
* @param int|null $exceptionCode (optional) Exception code
* @return void
*/
protected function checkException(
Closure $closure,
string $reason,
Throwable|string $exception,
string $exceptionMessage = '',
int|null $exceptionCode = null,
): void {
$caught = null;
try {
$closure();
} catch (Throwable $e) {
$caught = $e;
} finally {
if (null === $caught) {
$this->fail($reason);
} else {
$this->assertInstanceOf(
is_object($exception) ? $exception::class : $exception,
$caught,
$reason
);
if (!empty($exceptionMessage)) {
$this->assertStringContainsString($exceptionMessage, $caught->getMessage(), $reason);
}
if (!empty($exceptionCode)) {
$this->assertEquals($exceptionCode, $caught->getCode(), $reason);
}
}
}
}
}