feat: kernel dump

This commit is contained in:
Nuno Maduro
2023-02-18 14:39:44 +00:00
parent d0e949bf19
commit 7fc12613a8
10 changed files with 201 additions and 24 deletions

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Pest\Bootstrappers;
use Pest\Contracts\Bootstrapper;
use Pest\KernelDump;
use Pest\Support\Container;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
final class BootKernelDump implements Bootstrapper
{
/**
* Creates a new Boot Kernel Dump instance.
*/
public function __construct(
private readonly OutputInterface $output,
) {
// ...
}
/**
* Boots the kernel dump.
*/
public function boot(): void
{
Container::getInstance()->add(KernelDump::class, $kernelDump = new KernelDump(
$this->output,
));
$kernelDump->enable();
}
}

View File

@ -24,6 +24,7 @@ final class BootSubscribers implements Bootstrapper
Subscribers\EnsureConfigurationIsValid::class,
Subscribers\EnsureConfigurationIsAvailable::class,
Subscribers\EnsureTeamCityEnabled::class,
Subscribers\EnsureKernelDumpIsFlushed::class,
];
/**

View File

@ -31,6 +31,7 @@ final class Kernel
Bootstrappers\BootSubscribers::class,
Bootstrappers\BootFiles::class,
Bootstrappers\BootView::class,
Bootstrappers\BootKernelDump::class,
];
/**
@ -106,6 +107,12 @@ final class Kernel
*/
public function shutdown(): void
{
$preBufferOutput = Container::getInstance()->get(KernelDump::class);
assert($preBufferOutput instanceof KernelDump);
$preBufferOutput->shutdown();
CallsShutdown::execute();
}
}

91
src/KernelDump.php Normal file
View File

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Pest;
use Pest\Support\View;
use Symfony\Component\Console\Output\OutputInterface;
final class KernelDump
{
/**
* The output buffer, if any.
*/
private string $buffer = '';
/**
* Creates a new Kernel Dump instance.
*/
public function __construct(
private readonly OutputInterface $output,
) {
// ...
}
/**
* Enable the output buffering.
*/
public function enable(): void
{
ob_start(function (string $message): string {
$this->buffer .= $message;
return '';
});
}
/**
* Disable the output buffering.
*/
public function disable(): void
{
ob_clean();
if ($this->buffer !== '') {
$this->flush('INFO');
}
}
/**
* Shutdown the output buffering.
*/
public function shutdown(): void
{
ob_clean();
if ($this->buffer !== '') {
$this->flush('ERROR');
}
}
/**
* Flushes the buffer.
*/
private function flush(string $type): void
{
View::renderUsing($this->output);
if ($this->isOpeningHeadline($this->buffer)) {
$this->buffer = implode(PHP_EOL, array_slice(explode(PHP_EOL, $this->buffer), 2));
}
$this->buffer = trim($this->buffer);
$this->buffer = rtrim($this->buffer, '.').'.';
View::render('components.badge', [
'type' => $type,
'content' => $this->buffer,
]);
$this->buffer = '';
}
/**
* Checks if the given output contains an opening headline.
*/
private function isOpeningHeadline(string $output): bool
{
return str_contains($output, 'by Sebastian Bergmann and contributors.');
}
}

View File

@ -101,15 +101,15 @@ final class Help implements HandlesArguments
]] + $content['Configuration'];
$content['Selection'] = [
[
'arg' => '--todos',
'desc' => 'Output to standard output the list of todos',
],
[
'arg' => '--retry',
'desc' => 'Run non-passing tests first and stop execution upon first error or failure',
],
] + $content['Selection'];
[
'arg' => '--todos',
'desc' => 'Output to standard output the list of todos',
],
[
'arg' => '--retry',
'desc' => 'Run non-passing tests first and stop execution upon first error or failure',
],
] + $content['Selection'];
$content['Code Coverage'] = [
[
@ -123,11 +123,11 @@ final class Help implements HandlesArguments
] + $content['Code Coverage'];
$content['Profiling'] = [
[
'arg' => '--profile ',
'desc' => 'Output to standard output the top ten slowest tests',
],
];
[
'arg' => '--profile ',
'desc' => 'Output to standard output the top ten slowest tests',
],
];
unset($content['Miscellaneous']);

View File

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Pest\Plugins;
use ParaTest\ParaTestCommand;
use Pest\Contracts\Plugins\AddsOutput;
use Pest\Contracts\Plugins\HandlesArguments;
use Pest\Plugins\Actions\CallsAddsOutput;
use Pest\Plugins\Concerns\HandleArguments;

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Pest\Subscribers;
use Pest\KernelDump;
use Pest\Support\Container;
use PHPUnit\Event\TestRunner\Started;
use PHPUnit\Event\TestRunner\StartedSubscriber;
/**
* @internal
*/
final class EnsureKernelDumpIsFlushed implements StartedSubscriber
{
/**
* Runs the subscriber.
*/
public function notify(Started $event): void
{
$kernelDump = Container::getInstance()->get(KernelDump::class);
assert($kernelDump instanceof KernelDump);
$kernelDump->disable();
}
}