/
ConsoleController.php
63 lines (59 loc) · 2.34 KB
/
ConsoleController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Lucinda\UnitTest;
use Lucinda\UnitTest\Runner\UnitTest;
/**
* Runs unit tests on unix console and displays results
*/
class ConsoleController extends Controller
{
/**
* Displays results of unit tests
*
* @param UnitTest[] $results
*/
protected function handle(array $results): void
{
$totals = ["passed"=>0, "failed"=>0];
$columns = ["Class", "Method", "Status", "Description"];
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$object = new \Lucinda\Console\Table(array_map(function ($column) {
$text = new \Lucinda\Console\Text($column);
$text->setFontStyle(\Lucinda\Console\FontStyle::BOLD);
return $text;
}, $columns));
foreach ($results as $unitTest) {
$status = null;
if ($unitTest->result->hasPassed()) {
$status = new \Lucinda\Console\Text(" PASSED ");
$status->setBackgroundColor(\Lucinda\Console\BackgroundColor::GREEN);
} else {
$status = new \Lucinda\Console\Text(" FAILED ");
$status->setBackgroundColor(\Lucinda\Console\BackgroundColor::RED);
}
$object->addRow([
$unitTest->className,
$unitTest->methodName,
$status,
$unitTest->result->getMessage()
]);
$totals[$unitTest->result->hasPassed()?"passed":"failed"]++;
}
echo $object->__toString()."\n";
} else {
$object = new \Lucinda\Console\Table(array_map(function ($column) {
return strtoupper($column);
}, $columns));
foreach ($results as $unitTest) {
$object->addRow([
$unitTest->className,
$unitTest->methodName,
($unitTest->result->hasPassed()?"PASSED":"FAILED"),
$unitTest->result->getMessage()
]);
$totals[$unitTest->result->hasPassed()?"passed":"failed"]++;
}
echo $object->__toString()."\n";
}
echo "Total: ".($totals["passed"]+$totals["failed"])." (".$totals["passed"]." passed, ".$totals["failed"]." failed)\n";
}
}