app = new class() extends ApplicationAbstract { protected string $appName = 'Api'; }; $this->app->router = new WebRouter(); $this->app->dispatcher = new Dispatcher($this->app); } /** * @testdox A route can be added and dispatched * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testControllerInputOutput() : void { $this->app->dispatcher->set(new class() extends ModuleAbstract { public string $name = 'test'; public function testFunction() { return $this->name; } }, 'test'); $localization = new Localization(); self::assertTrue( !empty( $this->app->dispatcher->dispatch( 'test:testFunction', new HttpRequest(new HttpUri(''), $localization), new HttpResponse($localization) ) ) ); } /** * @testdox The dispatcher can dispatch a function/closure * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testClosure() : void { $localization = new Localization(); self::assertTrue( !empty( $this->app->dispatcher->dispatch( function($req, $resp, $data = null) : bool { return true; }, new HttpRequest(new HttpUri(''), $localization), new HttpResponse($localization) ) ) ); self::assertTrue( !empty( $this->app->dispatcher->dispatch( function($req) : bool { return true; } ) ) ); } /** * @testdox The dispatcher can dispatch a method as string representation of a controller * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testPathMethod() : void { $localization = new Localization(); self::assertTrue( !empty( $this->app->dispatcher->dispatch( 'phpOMS\tests\Dispatcher\TestController:testFunction', new HttpRequest(new HttpUri(''), $localization), new HttpResponse($localization) ) ) ); } /** * @testdox The dispatcher can dispatch a method as array representation of a controller * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testPathMethodInArray() : void { $localization = new Localization(); self::assertTrue( !empty( $this->app->dispatcher->dispatch( ['dest' => 'phpOMS\tests\Dispatcher\TestController:testFunction'], new HttpRequest(new HttpUri(''), $localization), new HttpResponse($localization) ) ) ); self::assertTrue( !empty( $this->app->dispatcher->dispatch( ['dest' => 'phpOMS\tests\Dispatcher\TestController:testFunctionNoPara'] ) ) ); } /** * @testdox The dispatcher can dispatch a static method as string representation * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testPathStatic() : void { $localization = new Localization(); self::assertTrue( !empty( $this->app->dispatcher->dispatch( 'phpOMS\tests\Dispatcher\TestController::testFunctionStatic', new HttpRequest(new HttpUri(''), $localization), new HttpResponse($localization) ) ) ); } /** * @testdox The dispatcher can dispatch multiple destinations after another * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testArray() : void { $localization = new Localization(); self::assertTrue( !empty( $this->app->dispatcher->dispatch( [ function($app, $req, $resp, $data = null) : bool { return true; }, 'phpOMS\tests\Dispatcher\TestController:testFunction', 'phpOMS\tests\Dispatcher\TestController::testFunctionStatic', ], new HttpRequest(new HttpUri(''), $localization), new HttpResponse($localization) ) ) ); } /** * @testdox The dispatcher can pass additional data to the destination * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testArrayWithData() : void { $localization = new Localization(); self::assertEquals([2], $this->app->dispatcher->dispatch( [ 'dest' => function($app, $req, $resp, $data = null) { return $data; }, 'data' => 2, ], new HttpRequest(new HttpUri(''), $localization), new HttpResponse($localization) ) ); } /** * @testdox A invalid controller path throws a PathException * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testInvalidControllerPath() : void { $this->expectException(\phpOMS\System\File\PathException::class); $this->app->dispatcher->dispatch('phpOMS\tests\Dispatcher\TestControllers::testFunctionStatic'); } /** * @testdox A invalid function path throws a Exception * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testInvalidControllerFunction() : void { $this->expectException(\Exception::class); $this->app->dispatcher->dispatch('phpOMS\tests\Dispatcher\TestController::testFunctionStaticINVALID'); } /** * @testdox A malformed dispatch path throws UnexpectedValueException * @covers \phpOMS\Dispatcher\Dispatcher * @group framework */ public function testInvalidControllerString() : void { $this->expectException(\UnexpectedValueException::class); $this->app->dispatcher->dispatch('phpOMS\tests\Dispatcher\TestController::testFunctionStatic:failure'); } }