First set of tests, argument unwrapping/injection
This commit is contained in:
67
tests/CommandRegistryTest.php
Normal file
67
tests/CommandRegistryTest.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\React\CommandBus;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
#[CoversClass(CommandRegistry::class)]
|
||||
class CommandRegistryTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testAddingNewCommandEmitsEvent()
|
||||
{
|
||||
$count = 0;
|
||||
|
||||
$reg = new CommandRegistry();
|
||||
$reg->on(CommandRegistry::EVENT_REGISTERED, function ($data) use (&$count){
|
||||
$this->assertEquals('foo', $data);
|
||||
$count++;
|
||||
});
|
||||
$reg->register('foo', function () {});
|
||||
$reg->register('foo', function () {});
|
||||
|
||||
$this->assertEquals(1, $count);
|
||||
}
|
||||
|
||||
public function testRemovingCommandEmitsEvent()
|
||||
{
|
||||
$count = 0;
|
||||
|
||||
$reg = new CommandRegistry();
|
||||
$reg->on(CommandRegistry::EVENT_UNREGISTERED, function ($data) use (&$count){
|
||||
$this->assertEquals('foo', $data);
|
||||
$count++;
|
||||
});
|
||||
$reg->register('foo', function () {});
|
||||
$reg->unregister('foo');
|
||||
$reg->unregister('foo');
|
||||
|
||||
$this->assertEquals(1, $count);
|
||||
}
|
||||
|
||||
public function testFindingCommands()
|
||||
{
|
||||
$cmda = function () {};
|
||||
$cmdb = function () {};
|
||||
|
||||
$reg = new CommandRegistry();
|
||||
$reg->register('a', $cmda);
|
||||
$reg->register('b', $cmdb);
|
||||
|
||||
$this->assertEquals('a', $reg->find('a')?->getName());
|
||||
$this->assertEquals('b', $reg->find('b')?->getName());
|
||||
}
|
||||
|
||||
public function testGettingCommandNames()
|
||||
{
|
||||
$cmda = function () {};
|
||||
$cmdb = function () {};
|
||||
|
||||
$reg = new CommandRegistry();
|
||||
$reg->register('a', $cmda);
|
||||
$reg->register('b', $cmdb);
|
||||
|
||||
$this->assertEquals(['a','b'], $reg->getNames());
|
||||
}
|
||||
|
||||
}
|
29
tests/CommandTest.php
Normal file
29
tests/CommandTest.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\React\CommandBus;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
#[CoversClass(Command::class)]
|
||||
class CommandTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testCommandName()
|
||||
{
|
||||
$command = new Command("foobar", function () {});
|
||||
|
||||
$this->assertEquals('foobar', $command->getName());
|
||||
}
|
||||
|
||||
public function testCommandExecution()
|
||||
{
|
||||
$hit = false;
|
||||
$command = new Command("foobar", function () use (&$hit) { $hit=true; });
|
||||
$context = new Context("foobar", []);
|
||||
|
||||
$command->call($context);
|
||||
|
||||
$this->assertEquals(true, $hit);
|
||||
}
|
||||
|
||||
}
|
40
tests/ContextTest.php
Normal file
40
tests/ContextTest.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\React\CommandBus;
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
#[CoversClass(Context::class)]
|
||||
class ContextTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
public function testContextPropertyAccess()
|
||||
{
|
||||
$context = new Context("", [ 'foo'=>true, 'bar'=>'hello' ]);
|
||||
|
||||
$this->assertEquals(true, $context->foo);
|
||||
$this->assertEquals('hello', $context->bar);
|
||||
}
|
||||
|
||||
public function testMakingParameters()
|
||||
{
|
||||
$func1 = function () {};
|
||||
$func2 = function (Context $context) {};
|
||||
$func3 = function ($a, ?int $b, ?string $c, $d) {};
|
||||
|
||||
$context = new Context("", [ 'a'=>true, 'b'=>42, 'c'=>'hello' ]);
|
||||
|
||||
$expect1 = [];
|
||||
$expect2 = [$context];
|
||||
$expect3 = [true,42,'hello',null];
|
||||
|
||||
$args1 = $context->toMethodParameters($func1);
|
||||
$this->assertEquals($expect1, $args1);
|
||||
|
||||
$args2 = $context->toMethodParameters($func2);
|
||||
$this->assertEquals($expect2, $args2);
|
||||
|
||||
$args3 = $context->toMethodParameters($func3);
|
||||
$this->assertEquals($expect3, $args3);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user