Skip to content

Commit 4320e4e

Browse files
authored
Merge pull request #12 from MaplePHP/feat/project-core
Enable Unitary core in project skeleton
2 parents 78012d6 + c71a69a commit 4320e4e

File tree

9 files changed

+81
-44
lines changed

9 files changed

+81
-44
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.DS_Store
33
.sass-cache
44
composer.lock
5-
vendor
5+
vendor
6+
.idea

src/Config/ConfigProps.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ class ConfigProps extends AbstractConfigProps
3232
public ?bool $errorsOnly = null;
3333
public ?bool $smartSearch = null;
3434
public ?bool $failFast = null;
35+
public ?string $helpController = null;
3536

3637
/**
3738
* Hydrate the properties/object with expected data, and handle unexpected data
3839
*
39-
* @param string $key
40+
* @param string|bool $key
4041
* @param mixed $value
4142
* @return void
4243
*/
43-
protected function propsHydration(string $key, mixed $value): void
44+
protected function propsHydration(string|bool $key, mixed $value): void
4445
{
4546
switch ($key) {
4647
case 'path':
@@ -58,6 +59,9 @@ protected function propsHydration(string $key, mixed $value): void
5859
case 'type':
5960
$this->type = (!is_string($value) || $value === '') ? null : $value;
6061
break;
62+
case 'helpController':
63+
$this->helpController = (!is_string($value) || $value === '') ? null : $value;
64+
break;
6165
case 'timezone':
6266
// The default timezone is 'CET'
6367
$this->timezone = (!is_string($value) || $value === '') ? 'Europe/Stockholm' : $value;

src/Console/Application.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MaplePHP\Http\Environment;
88
use MaplePHP\Http\ServerRequest;
99
use MaplePHP\Http\Uri;
10+
use MaplePHP\Emitron\Kernel as EmitronKernel;
1011
use MaplePHP\Unitary\Console\Middlewares\{AddCommandMiddleware,
1112
CheckAllowedProps,
1213
CliInitMiddleware,
@@ -15,6 +16,44 @@
1516

1617
final class Application
1718
{
19+
public function __construct()
20+
{
21+
// Default config
22+
if(is_file(__DIR__ . '/../../../../../unitary.config.php')) {
23+
// From the vendor dir
24+
EmitronKernel::setConfigFilePath(__DIR__ . '/../../../../../unitary.config.php');
25+
} else {
26+
// From the repo dir
27+
EmitronKernel::setConfigFilePath(__DIR__ . '/../../unitary.config.php');
28+
}
29+
EmitronKernel::setRouterFilePath(__DIR__ . "/ConsoleRouter.php");
30+
}
31+
32+
/**
33+
* Change router file
34+
*
35+
* @param string $path
36+
* @return $this
37+
*/
38+
public function withRouter(string $path): self
39+
{
40+
$inst = clone $this;
41+
EmitronKernel::setRouterFilePath($path);
42+
return $inst;
43+
}
44+
45+
/**
46+
* Change the config file
47+
*
48+
* @param string $path
49+
* @return $this
50+
*/
51+
public function withConfig(string $path): self
52+
{
53+
$inst = clone $this;
54+
EmitronKernel::setConfigFilePath($path);
55+
return $inst;
56+
}
1857

1958
/**
2059
* Default error handler boot

src/Console/Controllers/DefaultController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public function __construct(ContainerInterface $container, ResponseInterface $re
5959
protected function forceShowHelp(ResponseInterface $response): void
6060
{
6161
if (!Validator::value($response->getStatusCode())->isHttpSuccess()) {
62-
$help = new HelpController($this->container, $response->withStatus(200));
62+
$props = $this->configs->getProps();
63+
$help = ($props->helpController !== null) ?
64+
$props->helpController : "\MaplePHP\Unitary\Console\Controllers\HelpController";
65+
$help = new $help($this->container, $response->withStatus(200));
6366
$help->index();
6467
exit(1);
6568
}

src/Console/Controllers/RunTestController.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ protected function runJUnit(RunTestService $service): ResponseInterface
6060
$suitesXml = $suites->outputMemory();
6161

6262
// Duration: pick your source (internal timer is fine)
63-
$inst = TestDiscovery::getUnitaryInst();
63+
$inst = TestDiscovery::getUnitaryInst(true);
6464
$xml = new \XMLWriter();
6565
$xml->openMemory();
6666
$xml->setIndent(true);
@@ -91,29 +91,27 @@ protected function runJUnit(RunTestService $service): ResponseInterface
9191
*/
9292
protected function buildFooter(): void
9393
{
94-
$inst = TestDiscovery::getUnitaryInst();
95-
if ($inst !== null) {
96-
$dot = $this->command->getAnsi()->middot();
97-
$this->command->message($this->command->getAnsi()->line(80));
98-
$this->command->message(
99-
$this->command->getAnsi()->style(
100-
["bold", $inst::isSuccessful() ? "green" : "red"],
101-
"\nTests: " . $inst::getTotalTests() . " $dot " .
102-
"Failures: " . $inst::getTotalFailed() . " $dot " .
103-
"Errors: " . $inst::getTotalErrors() . " $dot " .
104-
"Skipped: " . $inst::getTotalSkipped() . " \n"
105-
)
106-
);
107-
$this->command->message(
108-
$this->command->getAnsi()->style(
109-
["italic", "grey"],
110-
"Duration: " . Helpers::formatDuration($inst::getTotalDuration()) . " seconds $dot " .
111-
"Memory: " . Helpers::byteToMegabyte($inst::getTotalMemory()) . " MB $dot " .
112-
"Date: " . Clock::value("now")->iso() . "\n"
113-
)
114-
);
115-
$this->command->message($this->command->getAnsi()->line(80));
116-
$this->command->message("");
117-
}
94+
$inst = TestDiscovery::getUnitaryInst(true);
95+
$dot = $this->command->getAnsi()->middot();
96+
$this->command->message($this->command->getAnsi()->line(80));
97+
$this->command->message(
98+
$this->command->getAnsi()->style(
99+
["bold", $inst::isSuccessful() ? "green" : "red"],
100+
"\nTests: " . $inst::getTotalTests() . " $dot " .
101+
"Failures: " . $inst::getTotalFailed() . " $dot " .
102+
"Errors: " . $inst::getTotalErrors() . " $dot " .
103+
"Skipped: " . $inst::getTotalSkipped() . " \n"
104+
)
105+
);
106+
$this->command->message(
107+
$this->command->getAnsi()->style(
108+
["italic", "grey"],
109+
"Duration: " . Helpers::formatDuration($inst::getTotalDuration()) . " seconds $dot " .
110+
"Memory: " . Helpers::byteToMegabyte($inst::getTotalMemory()) . " MB $dot " .
111+
"Date: " . Clock::value("now")->iso() . "\n"
112+
)
113+
);
114+
$this->command->message($this->command->getAnsi()->line(80));
115+
$this->command->message("");
118116
}
119117
}

src/Console/Kernel.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525

2626
class Kernel
2727
{
28-
private const DEFAULT_ROUTER_FILE = '/src/Console/ConsoleRouter.php';
29-
public const UNITARY_DIR = __DIR__ . '/../../';
30-
public const DEFAULT_CONFIG_FILE_PATH = __DIR__ . '/../../unitary.config';
31-
3228
private ContainerInterface $container;
3329
private array $userMiddlewares;
3430
private ?DispatchConfig $config;
@@ -48,13 +44,6 @@ public function __construct(
4844
$this->container = $container;
4945
$this->userMiddlewares = $userMiddlewares;
5046
$this->config = $dispatchConfig;
51-
52-
if(is_file(self::UNITARY_DIR . '/../../../unitary.config.php')) {
53-
EmitronKernel::setConfigFilePath(self::UNITARY_DIR . '/../../../unitary.config');
54-
} else {
55-
EmitronKernel::setConfigFilePath(self::DEFAULT_CONFIG_FILE_PATH);
56-
}
57-
EmitronKernel::setRouterFilePath(self::UNITARY_DIR);
5847
}
5948

6049
/**
@@ -85,8 +74,7 @@ private function configuration(ServerRequestInterface $request): DispatchConfigI
8574
{
8675
$config = new DispatchConfig(EmitronKernel::getConfigFilePath());
8776
return $config
88-
->setRouter(function ($path) use ($request) {
89-
$routerFile = $path . self::DEFAULT_ROUTER_FILE;
77+
->setRouter(function ($routerFile) use ($request) {
9078
$router = new Router($request->getCliKeyword(), $request->getCliArgs());
9179
if (!is_file($routerFile)) {
9280
throw new Exception('The routes file (' . $routerFile . ') is missing.');

src/Discovery/TestDiscovery.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,14 @@ protected function runBlunder(): void
362362
* This is primary used to access the main test Unit instance that is
363363
* pre-initialized for each test file. Is used by shortcut function like `group()`
364364
*
365+
* @param bool $initIfNull
365366
* @return Unit|null
366367
*/
367-
public static function getUnitaryInst(): ?Unit
368+
public static function getUnitaryInst(bool $initIfNull = false): ?Unit
368369
{
370+
if ($initIfNull) {
371+
self::$unitary = new Unit();
372+
}
369373
return self::$unitary;
370374
}
371375
}

src/Renders/CliRenderer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public function __construct(Command $command)
3434
public function buildBody(): void
3535
{
3636
$this->initDefault();
37-
3837
$this->command->message("");
3938
$this->command->message(
4039
$this->flag . " " .

unitary.config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
return [
77
//'path' => 'app/Libraries/Unitary/tests/unitary-test.php',
88
'type' => "cli",
9+
'helpController' => "\MaplePHP\Unitary\Console\Controllers\HelpController",
910
'path' => false, // false|string|array<int, string>
1011
'smart-search' => false, // bool
1112
'errors-only' => false, // bool

0 commit comments

Comments
 (0)