Skip to content

Commit

Permalink
feat(Command): renaming remaining run symbols, making it so that requ…
Browse files Browse the repository at this point in the history
…ired flags default to an error value and adding tests
  • Loading branch information
razshare committed Jul 5, 2024
1 parent 860b0fe commit 99a5405
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bin/start
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion src/lib/Core/CommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/Core/Build/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
78 changes: 78 additions & 0 deletions tests/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -102,6 +110,76 @@ public function makeSureGoffiWorks():Unsafe {
return ok();
});
}

/**
* @return Unsafe<None>
*/
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 {
Expand Down

0 comments on commit 99a5405

Please sign in to comment.