Skip to content

Commit a57322a

Browse files
committed
Improve styling
1 parent 6325065 commit a57322a

File tree

2 files changed

+92
-22
lines changed

2 files changed

+92
-22
lines changed

Test.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
namespace MaplePHP\Unitary;
55

6+
use ErrorException;
67
use MaplePHP\DTO\Format\Str;
78
use MaplePHP\Validate\Inp;
89
use InvalidArgumentException;
910

1011
class Test
1112
{
13+
private array $cases = [];
1214
private array $test = [];
1315

1416
/**
@@ -17,9 +19,11 @@ class Test
1719
* @param array $validation
1820
* @param string|null $message
1921
* @return $this
22+
* @throws ErrorException
2023
*/
2124
public function add(mixed $value, array $validation, ?string $message = null): self
2225
{
26+
$this->cases[] = $validation;
2327
foreach($validation as $method => $args) {
2428
if(is_callable($args)) {
2529
$bool = $args($this->valid($value), $value);
@@ -43,14 +47,23 @@ public function add(mixed $value, array $validation, ?string $message = null): s
4347
"method" => $method,
4448
"args" => $args,
4549
"test" => $bool,
46-
"message" => sprintf("Validation-error: %s", $method) . $msg,
50+
"message" => $message,
4751
"readableValue" => $readableValue,
4852
];
4953
}
5054

5155
return $this;
5256
}
5357

58+
/**
59+
* Will return the test results as an array
60+
* @return array
61+
*/
62+
public function getTestCases(): array
63+
{
64+
return $this->cases;
65+
}
66+
5467
/**
5568
* Will return the test results as an array
5669
* @return array
@@ -73,6 +86,7 @@ protected function valid(mixed $value): Inp {
7386
* Used to get a readable value
7487
* @param mixed $value
7588
* @return string
89+
* @throws ErrorException
7690
*/
7791
protected function getReadableValue(mixed $value): string {
7892
if (is_bool($value)) {
@@ -107,6 +121,7 @@ protected function getReadableValue(mixed $value): string {
107121
* Used to get exception to the readable value
108122
* @param string $value
109123
* @return string
124+
* @throws ErrorException
110125
*/
111126
final protected function excerpt(string $value): string
112127
{

Unit.php

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Unit
2121
private array $units;
2222
private int $index = 0;
2323
private array $error;
24+
private static string $file = "";
25+
private static array $info = [];
2426

2527
public function __construct(bool $quite = false)
2628
{
@@ -75,18 +77,23 @@ public function add(string $message, callable $callback): void
7577
$data = $test->getTestResult();
7678
$index = $this->index-1;
7779

78-
$this->error[$index] = [
79-
'message' => $message
80+
$count = count($test->getTestCases());
81+
$this->error['info'] = [
82+
'count' => $count,
83+
'total' => $count
8084
];
85+
$this->error['feed'][$index] = [
86+
'message' => $message,
87+
'file' => self::$file,
88+
'error' => []
89+
];
90+
8191
foreach($data as $key => $row) {
8292
if(!$row['test']) {
83-
$hasError = true;
84-
$this->error[$index]['error'][$key] = $row;
93+
$this->error['feed'][$index]['error'][$key] = $row;
94+
$this->error['info']['count']--;
8595
}
8696
}
87-
if(!$hasError) {
88-
unset($this->error[$index]);
89-
}
9097
}
9198

9299
/**
@@ -127,25 +134,49 @@ public function execute(): void
127134
$run = new Run(new CliHandler());
128135
$run->load();
129136

130-
if(!is_null($this->title) && (!$this->quite || count($this->error) > 0)) {
131-
$this->command->title("\n--------- $this->title ---------");
132-
}
133-
if(count($this->error) > 0) {
134-
foreach($this->error as $error) {
135-
//$tests = [];
136-
$this->command->title("\n{$error['message']}");
137-
foreach($error['error'] as $row) {
138-
//$tests[] = $row['method'];
139-
$this->command->error("Test-value {$row['readableValue']}");
140-
$this->command->error("{$row['message']}\n");
137+
138+
139+
$this->command->message("");
140+
141+
foreach($this->error['feed'] as $error) {
142+
143+
$color = ($error['error'] ? "red" : "blue");
144+
$flag = $this->command->getAnsi()->style(['blueBg', 'white'], " PASS ");
145+
if($error['error']) {
146+
$flag = $this->command->getAnsi()->style(['redBg', 'white'], " FAIL ");
147+
}
148+
149+
$this->command->message("");
150+
$this->command->message(
151+
$flag .
152+
" " .
153+
$this->command->getAnsi()->style(["bold"], $this->formatFileTitle($error['file'])) .
154+
" - " .
155+
$this->command->getAnsi()->style(["bold", $color], $error['message'])
156+
);
157+
foreach($error['error'] as $row) {
158+
$this->command->message("");
159+
160+
$this->command->message($this->command->getAnsi()->style(["bold", "red"], "Error: {$row['message']}"));
161+
$this->command->message($this->command->getAnsi()->bold("Value: ") . "{$row['readableValue']}");
162+
if(is_string($row['method'])) {
163+
$this->command->message($this->command->getAnsi()->bold("Validation: ") . "{$row['method']}");
141164
}
165+
142166
}
143167

144-
} elseif(!$this->quite) {
145-
$this->command->approve("Every test has been successfully run!");
168+
$this->command->message("");
169+
$this->command->message(
170+
$this->command->getAnsi()->bold("Passed: ") .
171+
$this->command->getAnsi()->style([$color, "bold"], $this->error['info']['count'] . "/" . $this->error['info']['total']));
146172
}
147173

148-
$this->command->message($this->output);
174+
175+
176+
177+
if($this->output) {
178+
$this->command->message($this->output);
179+
}
149180
}
150181

151182
/**
@@ -162,6 +193,7 @@ public function executeAll(string $directory): void
162193
} else {
163194
foreach ($files as $file) {
164195
extract($this->args, EXTR_PREFIX_SAME, "wddx");
196+
self::$file = $file;
165197
require_once ($file);
166198
}
167199
}
@@ -248,4 +280,27 @@ function getNaturalPath(string $path): string
248280
{
249281
return str_replace("\\", "/", $path);
250282
}
283+
/**
284+
* Get path as natural path
285+
* @param string $path
286+
* @return string
287+
*/
288+
function getUnnaturalPath(string $path): string
289+
{
290+
return str_replace("/", "\\", $path);
291+
}
292+
293+
/**
294+
* Make file path into a title
295+
* @param string $file
296+
* @return string
297+
*/
298+
function formatFileTitle(string $file): string
299+
{
300+
$file = explode("/", $file);
301+
$file = array_chunk(array_reverse($file), 3);
302+
$file = implode("\\", array_reverse($file[0]));
303+
$exp = explode('.', $file);
304+
return ".." . reset($exp);
305+
}
251306
}

0 commit comments

Comments
 (0)