rpcii-php/tests/AttributesTest.php
flashwave 93ec139171 Rename and a set of reworks.
- Aiwass -> RPCii
 - Swapped the meaning of "Procedure" and "Action"
 - IRpcActionHandler is now RpcHandler, abstract class no longer exists like Index
 - Anything suffixed by Trait is now suffixed by Common
 - All I prefixes on interfaces are gone
 - A PHP StreamWrapper implementation of HttpRequest added as a fallback for when cURL isnt installed
 - HttpRpcServer now hosts on /_rpcii instead of /_aiwass
 - Packagist package is now flashii/rpcii instead of flashwave/aiwass

That's probably all of it but I probably forgor.
2024-11-13 23:17:29 +00:00

109 lines
4.7 KiB
PHP

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