rpcii-php/tests/AttributesTest.php

108 lines
4.6 KiB
PHP

<?php
// AttributesTest.php
// Created: 2024-08-16
// Updated: 2024-08-16
declare(strict_types=1);
use RuntimeException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
use Aiwass\Server\{RpcAction,RpcActionHandler,RpcProcedure,RpcQuery,RpcServer};
#[CoversClass(RpcAction::class)]
#[CoversClass(RpcActionHandler::class)]
#[CoversClass(RpcProcedure::class)]
#[CoversClass(RpcQuery::class)]
#[UsesClass(RpcServer::class)]
final class AttributesTest extends TestCase {
public function testAttributes(): void {
$handler = new class($this) extends RpcActionHandler {
private static $that;
public function __construct(private $self) {
self::$that = $self;
}
#[RpcAction(true, 'aiwass:test:proc1')]
public function procedureRegisteredUsingActionAttribute() {
return 'procedure registered using action attribute';
}
#[RpcAction(false, 'aiwass:test:query1')]
public function queryRegisteredUsingActionAttribute(string $beans) {
$this->self->assertEquals('it is beans', $beans);
return 'query registered using action attribute';
}
#[RpcQuery('aiwass:test:query2')]
public static function staticQueryRegisteredUsingQueryAttribute(string $required, string $optional = 'the') {
self::$that->assertEquals('internet', $required);
self::$that->assertEquals('the', $optional);
return 'static query registered using query attribute';
}
#[RpcProcedure('aiwass:test:proc2')]
public function dynamicProcedureRegisteredUsingProcedureAttribute(string $optional = 'meow') {
$this->self->assertEquals('meow', $optional);
return 'dynamic procedure registered using procedure attribute';
}
#[RpcQuery('aiwass:test:query3')]
#[RpcProcedure('aiwass:test:proc3')]
public function multiple() {
return 'a dynamic method registered as both a query and a procedure';
}
public function hasNoAttr() {
return 'not an action handler';
}
};
$server = new RpcServer;
$server->register($handler);
$this->assertNull($server->getActionInfo('aiwass:none'));
$proc1 = $server->getActionInfo('aiwass:test:proc1');
$this->assertNotNull($proc1);
$this->assertTrue($proc1->isProcedure());
$this->assertEquals('aiwass:test:proc1', $proc1->getName());
$this->assertEquals('procedure registered using action attribute', $proc1->invokeHandler());
$proc2 = $server->getActionInfo('aiwass:test:proc2');
$this->assertNotNull($proc2);
$this->assertTrue($proc2->isProcedure());
$this->assertEquals('aiwass:test:proc2', $proc2->getName());
$this->assertEquals('dynamic procedure registered using procedure attribute', $proc2->invokeHandler());
$query1 = $server->getActionInfo('aiwass:test:query1');
$this->assertNotNull($query1);
$this->assertFalse($query1->isProcedure());
$this->assertEquals('aiwass:test:query1', $query1->getName());
$this->assertEquals('query registered using action attribute', $query1->invokeHandler(['beans' => 'it is beans']));
$query2 = $server->getActionInfo('aiwass:test:query2');
$this->assertNotNull($query2);
$this->assertFalse($query2->isProcedure());
$this->assertEquals('aiwass:test:query2', $query2->getName());
$this->assertEquals('static query registered using query attribute', $query2->invokeHandler(['required' => 'internet']));
$query3 = $server->getActionInfo('aiwass:test:query3');
$proc3 = $server->getActionInfo('aiwass:test:proc3');
$this->assertNotNull($query3);
$this->assertNotNull($proc3);
$this->assertFalse($query3->isProcedure());
$this->asserttrue($proc3->isProcedure());
$this->assertEquals('aiwass:test:query3', $query3->getName());
$this->assertEquals('aiwass:test:proc3', $proc3->getName());
$this->assertEquals($query3->getHandler(), $proc3->getHandler());
$this->assertEquals('a dynamic method registered as both a query and a procedure', $query3->invokeHandler());
$this->assertEquals('a dynamic method registered as both a query and a procedure', $proc3->invokeHandler());
$this->assertNull($server->getActionInfo('doesnotexist'));
$this->expectException(RuntimeException::class);
$query1->invokeHandler(['notbeans' => 'it is not beans']);
}
}