Issues (83)

src/Output/DebugOutputtingSubscriber.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester\Output;
6
7
use hanneskod\readmetester\Event;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
final class DebugOutputtingSubscriber extends DefaultOutputtingSubscriber
11
{
12
    private SyntaxHighlighter $highlighter;
13
14
    public function __construct()
15
    {
16
        $this->highlighter = new SyntaxHighlighter(
17
            commentColor: "#797979",
0 ignored issues
show
A parse error occurred: Syntax error, unexpected ':', expecting ',' or ')' on line 17 at column 24
Loading history...
18
            keywordColor: "yellow",
19
            stringColor: "cyan",
20
            defaultColor: 'white',
21
            htmlColor: 'blue',
22
        );
23
    }
24
25
    public function onExecutionStarted(Event\ExecutionStarted $event): void
26
    {
27
        // Debug is always very verbose
28
        $event->getOutput()->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
29
30
        // Bypass highlighter if not decorated (eg. --no-ansi) as highlighter could be unstable
31
        if (!$event->getOutput()->isDecorated()) {
32
            $this->highlighter = new VoidSyntaxHighlighter();
33
        }
34
35
        parent::onExecutionStarted($event);
36
    }
37
38
    public function onEvaluationStarted(Event\EvaluationStarted $event): void
39
    {
40
        $this->examplePassed = true;
41
        $this->exampleCount++;
42
43
        $this->getOutput()->writeln(
44
            "\n<fg=green;options=underscore>{$event->getOutcome()->getExample()->getName()->getFullName()}</>"
45
        );
46
47
        $this->getOutput()->writeln("Attributes:");
48
49
        foreach ($event->getOutcome()->getExample()->getAttributes() as $attribute) {
50
            $this->getOutput()->writeln(
51
                $this->highlighter->highlight($attribute->asAttribute())
52
            );
53
        }
54
55
        $this->getOutput()->writeln("\nResulted in code:");
56
57
        $this->getOutput()->writeln(
58
            $this->highlighter->highlight(trim($event->getOutcome()->getExample()->getCodeBlock()->getCode()))
59
        );
60
61
        $this->getOutput()->writeln(
62
            sprintf(
63
                "\nGenerated outcome:\n<comment>%s</comment>",
64
                trim($event->getOutcome()->getContent())
65
            )
66
        );
67
68
        $this->getOutput()->write("\nPerformed tests:");
69
    }
70
71
    public function onEvaluationDone(Event\EvaluationDone $event): void
72
    {
73
        $this->getOutput()->writeln('');
74
    }
75
}
76