Skip to content

Commit f880317

Browse files
Merge pull request #8 from MaplePHP/develop
Minor code exit patch
2 parents 8cb6457 + 07278ac commit f880317

File tree

9 files changed

+103
-4
lines changed

9 files changed

+103
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
},
3333
"autoload": {
3434
"psr-4": {
35-
"MaplePHP\\Unitary\\": ""
35+
"MaplePHP\\Unitary\\": "src"
3636
}
3737
},
3838
"bin": [
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function executeAll(string $directory): void
5252
throw new RuntimeException("The Unitary Unit class has not been initiated inside \"$file\".");
5353
}
5454
}
55-
5655
Unit::completed();
56+
exit((int)Unit::isSuccessful());
5757
}
5858
}
5959

@@ -145,7 +145,8 @@ private function requireUnitFile(string $file): ?Closure
145145
$clone = clone $this;
146146
$call = function () use ($file, $clone): void {
147147
$cli = new CliHandler();
148-
if(isset(self::$headers['args']['trace'])) {
148+
149+
if(Unit::getArgs('trace') !== false) {
149150
$cli->enableTraceLines(true);
150151
}
151152
$run = new Run($cli);
File renamed without changes.

src/TestMem.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MaplePHP\Unitary;
6+
7+
class TestMem
8+
{
9+
private float $startTime;
10+
private int $startMemory;
11+
//private static int $correction = 0;
12+
13+
public function __construct()
14+
{
15+
$this->startTime = microtime(true);
16+
$this->startMemory = memory_get_usage();
17+
}
18+
19+
/**
20+
* Get execution time
21+
* @return float
22+
*/
23+
public function getExecutionTime(): float
24+
{
25+
$endTime = microtime(true);
26+
return $endTime - $this->startTime;
27+
}
28+
29+
/**
30+
* Get memory usage in KB
31+
* @return float
32+
*/
33+
public function getMemoryUsage(): float
34+
{
35+
$endMemory = memory_get_usage();
36+
return ($endMemory - $this->startMemory) / 1024;
37+
}
38+
39+
/**
40+
* Get peak memory usage in KB
41+
* @return float
42+
*/
43+
public function getMemoryPeakUsage(): float
44+
{
45+
return (memory_get_peak_usage() / 1024);
46+
}
47+
}
File renamed without changes.

Unit.php renamed to src/Unit.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@ public function case(string $message, Closure $callback): void
142142
$this->index++;
143143
}
144144

145+
public function performance(Closure $func, ?string $title = null): void
146+
{
147+
$start = new TestMem();
148+
$func = $func->bindTo($this);
149+
if(!is_null($func)) {
150+
$func();
151+
}
152+
$line = $this->command->getAnsi()->line(80);
153+
$this->command->message("");
154+
$this->command->message($this->command->getAnsi()->style(["bold", "yellow"], "Performance" . (!is_null($title) ? " - $title:" : ":")));
155+
156+
$this->command->message($line);
157+
$this->command->message(
158+
$this->command->getAnsi()->style(["bold"], "Execution time: ") .
159+
(round($start->getExecutionTime(), 3) . " seconds")
160+
);
161+
$this->command->message(
162+
$this->command->getAnsi()->style(["bold"], "Memory Usage: ") .
163+
(round($start->getMemoryUsage(), 2) . " KB")
164+
);
165+
/*
166+
$this->command->message(
167+
$this->command->getAnsi()->style(["bold", "grey"], "Peak Memory Usage: ") .
168+
$this->command->getAnsi()->blue(round($start->getMemoryPeakUsage(), 2) . " KB")
169+
);
170+
*/
171+
$this->command->message($line);
172+
}
173+
145174
/**
146175
* Execute tests suite
147176
* @return bool
@@ -313,6 +342,16 @@ public static function setHeaders(array $headers): void
313342
self::$headers = $headers;
314343
}
315344

345+
/**
346+
* Get global header
347+
* @param string $key
348+
* @return mixed
349+
*/
350+
public static function getArgs(string $key): mixed
351+
{
352+
return (self::$headers['args'][$key] ?? false);
353+
}
354+
316355
/**
317356
* Append to global header
318357
* @param string $key
@@ -362,16 +401,28 @@ public static function getUnit(): ?Unit
362401
public static function completed(): void
363402
{
364403
if(!is_null(self::$current) && is_null(self::$current->handler)) {
404+
$dot = self::$current->command->getAnsi()->middot();
405+
365406
self::$current->command->message("");
366407
self::$current->command->message(
367408
self::$current->command->getAnsi()->style(
368409
["italic", "grey"],
369-
"Total: " . self::$totalPassedTests . "/" . self::$totalTests
410+
"Total: " . self::$totalPassedTests . "/" . self::$totalTests . " $dot " .
411+
"Peak memory usage: " . round(memory_get_peak_usage() / 1024, 2) . " KB"
370412
)
371413
);
372414
}
373415
}
374416

417+
/**
418+
* Check if successful
419+
* @return bool
420+
*/
421+
public static function isSuccessful(): bool
422+
{
423+
return (self::$totalPassedTests !== self::$totalTests);
424+
}
425+
375426
/**
376427
* DEPRECATED: Not used anymore
377428
* @return $this

0 commit comments

Comments
 (0)