rpcii-php/tests/AttributesTest.php

110 lines
4.7 KiB
PHP
Raw Normal View History

2024-08-16 14:50:38 +00:00
<?php
// AttributesTest.php
// Created: 2024-08-16
// Updated: 2024-11-13
2024-08-16 14:50:38 +00:00
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\{CoversClass,UsesClass};
use RPCii\Server\{RpcAction,RpcHandler,RpcHandlerCommon,RpcProcedure,RpcQuery,HttpRpcServer};
2024-08-16 14:50:38 +00:00
#[CoversClass(RpcAction::class)]
#[CoversClass(RpcHandler::class)]
2024-08-16 14:50:38 +00:00
#[CoversClass(RpcProcedure::class)]
#[CoversClass(RpcQuery::class)]
#[UsesClass(HttpRpcServer::class)]
2024-08-16 14:50:38 +00:00
final class AttributesTest extends TestCase {
public function testAttributes(): void {
$handler = new class($this) implements RpcHandler {
use RpcHandlerCommon;
2024-08-16 14:50:38 +00:00
private static AttributesTest $that;
public function __construct(private AttributesTest $self) {
2024-08-16 14:50:38 +00:00
self::$that = $self;
}
#[RpcProcedure(true, 'aiwass:test:proc1')]
public function procedureRegisteredUsingActionAttribute(): string {
2024-08-16 14:50:38 +00:00
return 'procedure registered using action attribute';
}
#[RpcProcedure(false, 'aiwass:test:query1')]
public function queryRegisteredUsingActionAttribute(string $beans): string {
2024-08-16 14:50:38 +00:00
$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'): string {
2024-08-16 14:50:38 +00:00
self::$that->assertEquals('internet', $required);
self::$that->assertEquals('the', $optional);
return 'static query registered using query attribute';
}
#[RpcAction('aiwass:test:proc2')]
public function dynamicProcedureRegisteredUsingProcedureAttribute(string $optional = 'meow'): string {
2024-08-16 14:50:38 +00:00
$this->self->assertEquals('meow', $optional);
return 'dynamic procedure registered using procedure attribute';
}
#[RpcQuery('aiwass:test:query3')]
#[RpcAction('aiwass:test:proc3')]
public function multiple(): string {
2024-08-16 14:50:38 +00:00
return 'a dynamic method registered as both a query and a procedure';
}
public function hasNoAttr(): string {
2024-08-16 14:50:38 +00:00
return 'not an action handler';
}
};
$server = new HttpRpcServer;
2024-08-16 14:50:38 +00:00
$server->register($handler);
$this->assertNull($server->getProcedureInfo('aiwass:none'));
2024-08-16 14:50:38 +00:00
$act1 = $server->getProcedureInfo('aiwass:test:proc1');
$this->assertNotNull($act1);
$this->assertTrue($act1->isAction());
$this->assertEquals('aiwass:test:proc1', $act1->getName());
$this->assertEquals('procedure registered using action attribute', $act1->invokeHandler());
2024-08-16 14:50:38 +00:00
$act2 = $server->getProcedureInfo('aiwass:test:proc2');
$this->assertNotNull($act2);
$this->assertTrue($act2->isAction());
$this->assertEquals('aiwass:test:proc2', $act2->getName());
$this->assertEquals('dynamic procedure registered using procedure attribute', $act2->invokeHandler());
2024-08-16 14:50:38 +00:00
$query1 = $server->getProcedureInfo('aiwass:test:query1');
2024-08-16 15:14:44 +00:00
$this->assertNotNull($query1);
$this->assertFalse($query1->isAction());
2024-08-16 14:50:38 +00:00
$this->assertEquals('aiwass:test:query1', $query1->getName());
$this->assertEquals('query registered using action attribute', $query1->invokeHandler(['beans' => 'it is beans']));
$query2 = $server->getProcedureInfo('aiwass:test:query2');
2024-08-16 15:14:44 +00:00
$this->assertNotNull($query2);
$this->assertFalse($query2->isAction());
2024-08-16 14:50:38 +00:00
$this->assertEquals('aiwass:test:query2', $query2->getName());
$this->assertEquals('static query registered using query attribute', $query2->invokeHandler(['required' => 'internet']));
$query3 = $server->getProcedureInfo('aiwass:test:query3');
$proc3 = $server->getProcedureInfo('aiwass:test:proc3');
2024-08-16 15:14:44 +00:00
$this->assertNotNull($query3);
$this->assertNotNull($proc3);
$this->assertFalse($query3->isAction());
$this->asserttrue($proc3->isAction());
2024-08-16 14:50:38 +00:00
$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->getProcedureInfo('doesnotexist'));
2024-08-16 15:14:44 +00:00
2024-08-16 14:50:38 +00:00
$this->expectException(RuntimeException::class);
$query1->invokeHandler(['notbeans' => 'it is not beans']);
}
}