Skip to content

Commit 9372330

Browse files
Fixed conflicts
1 parent c640684 commit 9372330

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

src/Components.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,30 @@ private function message(string $message, string $type, string $backgroundColor)
1919
{
2020
return <<<"HTML"
2121
<div>
22-
<div class="px-1 bg-$backgroundColor-600 w-8 text-center">$type</div>
22+
<div class="px-1 bg-$backgroundColor-600 w-11 text-center uppercase">$type</div>
2323
<em class="ml-1">$message</em>
2424
</div>
2525
HTML;
2626
}
2727

28+
public function separator(): void
29+
{
30+
render('<HR>');
31+
}
32+
2833
public function newLine(int $lines = 1): void
2934
{
3035
$this->block(str_repeat('<BR>', $lines));
3136
}
3237

3338
public function success(string $message): void
3439
{
35-
$this->block($this->message($message, 'SUCCESS', 'green'));
40+
$this->block($this->message($message, 'Success', 'green'));
41+
}
42+
43+
public function warning(string $message): void
44+
{
45+
$this->block($this->message($message, 'Warning', 'yellow'));
3646
}
3747

3848
public function info(string $message): void
@@ -46,17 +56,41 @@ public function error(string $message, array $reasons = []): void
4656

4757
if (empty($reasons)) {
4858
$this->block($message);
59+
60+
return;
4961
}
5062

5163
$message .= '<BR>';
5264
$message .= '<div class="font-bold">Possible reasons:</div>';
5365

54-
$list = array_reduce($reasons, function (?string $carry, string $reason) {
55-
return $carry . "<li>$reason</li>";
56-
});
66+
$list = array_reduce(
67+
$reasons,
68+
fn (?string $carry, string $reason) => $carry . "<li>$reason</li>"
69+
);
5770

5871
$message .= "<ul>$list</ul>";
5972

6073
$this->block($message);
6174
}
75+
76+
public function table(array $header, array $data): void
77+
{
78+
$message = '<table><thead><tr>';
79+
$message .= array_reduce(
80+
$header,
81+
fn (?string $carry, string $column) => $carry . "<th>$column</th>"
82+
);
83+
$message .= '</tr></thead>';
84+
85+
foreach ($data as $row) {
86+
$message .= '<tr>' . array_reduce(
87+
$row,
88+
fn (?string $carry, string $column) => $carry . "<td>$column</td>"
89+
) . '<tr>';
90+
}
91+
92+
$message .= '</thead>';
93+
94+
$this->block($message);
95+
}
6296
}

src/Extensions/ResultToCSV.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
class ResultToCSV implements BeforeFirstTestHook, AfterTestHook, AfterLastTestHook
1111
{
1212
private $handle;
13+
1314
private Components $components;
1415

1516
public function __construct(public string $file = 'result.csv')

src/Extensions/SlowestTests.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace MauroBaptista\SlowTests\Extensions;
44

5+
use Closure;
6+
use MauroBaptista\SlowTests\Components;
57
use PHPUnit\Runner\AfterLastTestHook;
68
use PHPUnit\Runner\AfterTestHook;
79
use PHPUnit\Runner\BeforeFirstTestHook;
@@ -11,8 +13,19 @@ class SlowestTests implements BeforeFirstTestHook, AfterTestHook, AfterLastTestH
1113
/** @var array<string, float> array<$test,> */
1214
private array $tests = [];
1315

14-
public function __construct(public int $show = 10)
15-
{
16+
private array $sumUp = [
17+
'success' => 0,
18+
'warning' => 0,
19+
'danger' => 0,
20+
];
21+
22+
private Components $components;
23+
24+
public function __construct(public int $show = 10, public array $threshold = [
25+
'success' => 0.1,
26+
'warning' => 1,
27+
]) {
28+
$this->components = new Components();
1629
}
1730

1831
public function executeBeforeFirstTest(): void
@@ -23,6 +36,12 @@ public function executeBeforeFirstTest(): void
2336
public function executeAfterTest(string $test, float $time): void
2437
{
2538
$this->tests[$test] = $time;
39+
40+
match (true) {
41+
$time <= $this->threshold['success'] => $this->sumUp['success']++,
42+
$time <= $this->threshold['warning'] => $this->sumUp['warning']++,
43+
default => $this->sumUp['danger']++,
44+
};
2645
}
2746

2847
public function executeAfterLastTest(): void
@@ -31,6 +50,28 @@ public function executeAfterLastTest(): void
3150

3251
$slowestTests = array_slice($this->tests, 0, $this->show);
3352

34-
fwrite(STDERR, print_r($slowestTests, true));
53+
array_walk($slowestTests, function (&$value, $key) {
54+
$status = match (true) {
55+
$value <= $this->threshold['success'] => '💚',
56+
$value <= $this->threshold['warning'] => '💛',
57+
default => '💔',
58+
};
59+
60+
$seconds = ($value <= 1) ? 'Less than one second' : $value;
61+
62+
[$class, $test] = explode('::', $key);
63+
64+
$value = [$class, $test, $seconds, $status];
65+
});
66+
67+
$this->components->table(
68+
['Class', 'Test', 'Time', 'Status'],
69+
$slowestTests,
70+
);
71+
72+
$this->components->separator();
73+
$this->components->success("{$this->sumUp['success']} test(s) [Less than {$this->threshold['success']} second(s)");
74+
$this->components->warning("{$this->sumUp['warning']} test(s) [Less than {$this->threshold['warning']} second(s)");
75+
$this->components->error("{$this->sumUp['danger']} test(s) [More than {$this->threshold['warning']} second(s)");
3576
}
3677
}

0 commit comments

Comments
 (0)