2024-03-19 00:01:12 +01:00
|
|
|
<?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);
|
|
|
|
|
2024-12-28 15:35:41 +01:00
|
|
|
$this->assertEquals('a', $reg->findCommand('a')?->getName());
|
|
|
|
$this->assertEquals('b', $reg->findCommand('b')?->getName());
|
2024-03-19 00:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGettingCommandNames()
|
|
|
|
{
|
|
|
|
$cmda = function () {};
|
|
|
|
$cmdb = function () {};
|
|
|
|
|
|
|
|
$reg = new CommandRegistry();
|
|
|
|
$reg->register('a', $cmda);
|
|
|
|
$reg->register('b', $cmdb);
|
|
|
|
|
2024-12-28 15:35:41 +01:00
|
|
|
$this->assertEquals(['a','b'], $reg->getCommandNames());
|
2024-03-19 00:01:12 +01:00
|
|
|
}
|
|
|
|
|
2024-12-28 15:35:41 +01:00
|
|
|
}
|