Issues (12)

src/ResultsFormatters/Console.php (1 issue)

Severity
1
<?php
2
declare(strict_types=1);
3
4
namespace MyTester\ResultsFormatters;
5
6
use MyTester\ConsoleColors;
7
use MyTester\Events\TestSuiteFinished;
8
use MyTester\IConsoleAwareResultsFormatter;
9
use MyTester\JobResult;
10
use MyTester\SkippedTest;
11
use MyTester\TestWarning;
12
13
/**
14
 * Console results formatter for Tester
15
 * Prints the results to console/standard output
16
 *
17
 * @author Jakub Konečný
18
 */
19
final class Console extends AbstractResultsFormatter implements IConsoleAwareResultsFormatter
20
{
21
    private ConsoleColors $console;
22
23
    /** @var array<string, string> */
24
    private array $failures = [];
25
26
    /** @var SkippedTest[] */
27
    private array $skipped = [];
28
29
    /** @var TestWarning[] */
30
    private array $warnings = [];
31
32
    private string $results = "";
33
34
    public function setConsole(ConsoleColors $console): void
35
    {
36 1
        $this->console = $console;
37 1
    }
38
39
    public function reportTestCaseFinished(TestSuiteFinished $event): void
40
    {
41 1
        foreach ($event->testSuite->jobs as $job) {
42 1
            switch ($job->result) {
43 1
                case JobResult::SKIPPED:
44 1
                    $this->skipped[] = new SkippedTest($job->nameWithDataSet, (is_string($job->skip) ? $job->skip : ""));
0 ignored issues
show
Line exceeds 120 characters; contains 121 characters
Loading history...
45 1
                    break;
46 1
                case JobResult::FAILED:
47 1
                    $output = $job->output;
48 1
                    if (strlen($output) > 0) {
49 1
                        $this->failures[$job->nameWithDataSet] = $output;
50
                    }
51 1
                    break;
52 1
                case JobResult::WARNING:
53 1
                    $output = $job->output;
54 1
                    $output = str_replace("Warning: ", "", $output);
55 1
                    $this->warnings[] = new TestWarning($job->nameWithDataSet, $output);
56 1
                    break;
57
            }
58 1
            $this->results .= $job->result->output();
59
        }
60 1
    }
61
62
    public function render(): string
63
    {
64 1
        ob_start();
65 1
        $results = $this->results;
66 1
        $rp = JobResult::PASSED->output();
67 1
        $rf = JobResult::FAILED->output();
68 1
        $rs = JobResult::SKIPPED->output();
69 1
        $rw = JobResult::WARNING->output();
70 1
        $results = str_replace($rf, $this->console->color($rf, "red"), $results);
71 1
        $results = str_replace($rs, $this->console->color($rs, "yellow"), $results);
72 1
        $results = str_replace($rw, $this->console->color($rw, "yellow"), $results);
73 1
        echo $results . "\n";
74 1
        $results = $this->results;
75 1
        $this->printWarnings();
76 1
        $this->printSkipped();
77 1
        $failed = str_contains($results, $rf);
78 1
        if (!$failed) {
79 1
            echo "\n";
80 1
            $resultsLine = "OK";
81
        } else {
82 1
            $this->printFailed();
83 1
            echo "\n";
84 1
            $resultsLine = "Failed";
85
        }
86 1
        $resultsLine .= " (" . strlen($results) . " tests";
87 1
        if (str_contains($results, $rp)) {
88 1
            $resultsLine .= ", " . substr_count($results, $rp) . " passed";
89
        }
90 1
        if (str_contains($results, $rw)) {
91 1
            $resultsLine .= ", " . substr_count($results, $rw) . " passed with warnings";
92
        }
93 1
        if ($failed) {
94 1
            $resultsLine .= ", " . substr_count($results, $rf) . " failed";
95
        }
96 1
        if (str_contains($results, $rs)) {
97 1
            $resultsLine .= ", " . substr_count($results, $rs) . " skipped";
98
        }
99 1
        $time = \Ayesh\PHP_Timer\Formatter::formatTime($this->totalTime);
100 1
        $resultsLine .= ", $time)";
101 1
        $resultsLine = $this->console->color($resultsLine, (!$failed) ? "green" : "red");
102 1
        echo $resultsLine . "\n";
103
        /** @var string $result */
104 1
        $result = ob_get_clean();
105 1
        return $result;
106
    }
107
108
    /**
109
     * Print info about tests with warnings
110
     */
111
    private function printWarnings(): void
112
    {
113 1
        foreach ($this->warnings as $testWarning) {
114 1
            echo $testWarning;
115
        }
116 1
    }
117
118
    /**
119
     * Print info about skipped tests
120
     */
121
    private function printSkipped(): void
122
    {
123 1
        foreach ($this->skipped as $skipped) {
124 1
            echo $skipped;
125
        }
126 1
    }
127
128
    /**
129
     * Print info about failed tests
130
     */
131
    private function printFailed(): void
132
    {
133 1
        foreach ($this->failures as $name => $text) {
134 1
            echo "--- " . $name . "\n$text\n";
135
        }
136 1
    }
137
}
138