From 99a5405e7b8dad490fa5948cdd678491cee1242e Mon Sep 17 00:00:00 2001 From: Razvan Tanase Date: Fri, 5 Jul 2024 11:58:16 +0200 Subject: [PATCH] feat(Command): renaming remaining run symbols, making it so that required flags default to an error value and adding tests --- bin/start | 2 +- src/lib/Core/CommandBuilder.php | 2 +- src/scripts/Core/Build/functions.php | 2 +- tests/CoreTest.php | 78 ++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/bin/start b/bin/start index d270c15..6a652ee 100644 --- a/bin/start +++ b/bin/start @@ -8,4 +8,4 @@ use CatPaw\Core\Interfaces\CommandInterface; require 'vendor/autoload.php'; Container::provide(CommandInterface::class, $command = new SimpleCommand); -$command->run(new Application(__FILE__))->try(); +$command->register(new Application(__FILE__))->try(); diff --git a/src/lib/Core/CommandBuilder.php b/src/lib/Core/CommandBuilder.php index cba1fed..db0a06f 100644 --- a/src/lib/Core/CommandBuilder.php +++ b/src/lib/Core/CommandBuilder.php @@ -64,7 +64,7 @@ public function withRequiredFlag(string $shortName, string $longName):self { longName: $longName, shortName: $shortName, isFlag: true, - value: ok("Required flag `--$longName (-$shortName)` is missing."), + value: error("Required flag `--$longName (-$shortName)` is missing."), ); $this->options[$shortName] = $option; $this->options[$longName] = $option; diff --git a/src/scripts/Core/Build/functions.php b/src/scripts/Core/Build/functions.php index bde0ae7..9695562 100644 --- a/src/scripts/Core/Build/functions.php +++ b/src/scripts/Core/Build/functions.php @@ -86,7 +86,7 @@ function build(bool $optimize = false):Unsafe { require 'vendor/autoload.php'; Container::provide(CommandInterface::class, \$command = new SimpleCommand); - \$command->run(new ApplicationBundled( + \$command->register(new ApplicationBundled( main: "$main", name: "$name", libraries: "$libraries", diff --git a/tests/CoreTest.php b/tests/CoreTest.php index 749d701..35b1d10 100755 --- a/tests/CoreTest.php +++ b/tests/CoreTest.php @@ -3,10 +3,16 @@ use function CatPaw\Core\anyError; use function CatPaw\Core\asFileName; + +use CatPaw\Core\CommandBuilder; +use CatPaw\Core\CommandContext; use CatPaw\Core\Container; use function CatPaw\Core\env; use CatPaw\Core\File; use CatPaw\Core\GoffiContract; +use CatPaw\Core\Implementations\Command\SimpleCommand; +use CatPaw\Core\Interfaces\CommandInterface; +use CatPaw\Core\Interfaces\CommandRunnerInterface; use CatPaw\Core\Interfaces\EnvironmentInterface; use CatPaw\Core\None; use function CatPaw\Core\ok; @@ -17,6 +23,7 @@ class CoreTest extends TestCase { public function testAll():void { + Container::provide(CommandInterface::class, new SimpleCommand); Container::requireLibraries(asFileName(__DIR__, '../src/lib'))->unwrap($error); $this->assertNull($error); Container::loadDefaultProviders("Test")->unwrap($error); @@ -27,6 +34,7 @@ public function testAll():void { yield Container::run($this->makeSureUnsafeWorksWithAnyError(...)); yield Container::run($this->makeSureSignalsWork(...)); yield Container::run($this->makeSureGoffiWorks(...)); + yield Container::run($this->makeSureCommandWorks(...)); })->unwrap($error); $this->assertNull($error); } @@ -102,6 +110,76 @@ public function makeSureGoffiWorks():Unsafe { return ok(); }); } + + /** + * @return Unsafe + */ + public function makeSureCommandWorks(CommandInterface $command):Unsafe { + return anyError(function() use ($command) { + $command->register($runner = new class implements CommandRunnerInterface { + public CommandBuilder $builder; + public function build(CommandBuilder $builder): Unsafe { + $this->builder = $builder; + $builder->withRequiredFlag('r', 'run'); + $builder->withOption('p', 'port', ok('80')); + $builder->withOption('c', 'certificate'); + $builder->withFlag('s', 'secure'); + return ok(); + } + + public function run(CommandContext $context): Unsafe { + return ok(); + } + }); + + $options = $runner->builder->options(); + + $this->assertArrayHasKey('r', $options); + $this->assertArrayHasKey('run', $options); + $this->assertArrayHasKey('p', $options); + $this->assertArrayHasKey('port', $options); + $this->assertArrayHasKey('c', $options); + $this->assertArrayHasKey('certificate', $options); + $this->assertArrayHasKey('s', $options); + $this->assertArrayHasKey('secure', $options); + + $r = $options['r']; + $run = $options['run']; + $this->assertEquals($r, $run); + + $p = $options['p']; + $port = $options['port']; + $this->assertEquals($p, $port); + + $c = $options['c']; + $certificate = $options['certificate']; + $this->assertEquals($c, $certificate); + + $s = $options['s']; + $secure = $options['secure']; + $this->assertEquals($s, $secure); + + $this->assertTrue($r->isFlag); + $this->assertFalse($p->isFlag); + $this->assertFalse($c->isFlag); + $this->assertTrue($s->isFlag); + + $runValue = $run->value->unwrap($error); + $this->assertNull($runValue); + $this->assertEquals("Required flag `--run (-r)` is missing.", $error->getMessage()); + + $portValue = $port->value->unwrap($error); + $this->assertEquals('80', $portValue); + + $certificateValue = $certificate->value->unwrap($error); + $this->assertEquals('', $certificateValue); + + $secureValue = $secure->value->unwrap($error); + $this->assertEquals('0', $secureValue); + + return ok(); + }); + } } interface Contract {