First set of tests, argument unwrapping/injection

This commit is contained in:
2024-03-19 00:01:12 +01:00
parent 74960345ba
commit 35efaa1b0d
11 changed files with 870 additions and 8 deletions

View File

@ -9,6 +9,7 @@ use React\EventLoop\Loop;
use React\Promise\Promise;
$commands = new CommandRegistry();
// Register some function to call. The name here is "hello", and it will
// receive a Context holding the call context. Any passed data will be
// available as properties on the Context object.
@ -21,23 +22,28 @@ $commands->register("hello", function (Context $context) {
});
});
});
$commands->register("hello2", function (Context $context) {
// Note how the arguments can be unwrapped. You can still add Context as
// an argument to get the full context. Values will be cast to match the
// type, so keep this in mind when feeding strings like 'false' into a
// boolean argument. It is also a good idea to make the arguments nullable.
$commands->register("hello2", function (?string $name, ?string $phrase, ?string $missing) {
// Just returning the value works just fine!
return "Hello2, {$context->name}";
return "Greetings, {$name}. {$phrase} to you too!";
});
// Create the bus
$bus = new CommandBus($commands);
// And execute some commands!
$bus->execute('hello', ['name'=>'Bob'])->then(
function ($result) {
$bus->execute('hello', ['name'=>'Bob'])
->then(function ($result) {
var_dump($result);
}
);
$bus->execute('hello2', ['name'=>'Bob'])->then(
function ($result) {
$bus->execute('hello2', ['name'=>'Bob','phrase'=>'Good day'])
->then(function ($result) {
var_dump($result);
}
);