. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\GuestUser; use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; use Fisharebest\Webtrees\Module\AbstractModule; use Fisharebest\Webtrees\Module\ModuleInterface; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\TestCase; use PHPUnit\Framework\Attributes\CoversClass; use Psr\Http\Message\ResponseInterface; use function response; #[CoversClass(ModuleAction::class)] class ModuleActionTest extends TestCase { public function testModuleAction(): void { $module_service = $this->createMock(ModuleService::class); $module_service ->expects($this->once()) ->method('findByName') ->with('test') ->willReturn($this->fooModule()); $user = new GuestUser(); $request = self::createRequest() ->withAttribute('module', 'test') ->withAttribute('action', 'Test') ->withAttribute('user', $user); $handler = new ModuleAction($module_service); $response = $handler->handle($request); self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); self::assertSame('It works!', (string) $response->getBody()); } public function testNonExistingAction(): void { $this->expectException(HttpNotFoundException::class); $this->expectExceptionMessage('Method getTestingAction() not found in test'); $module_service = $this->createMock(ModuleService::class); $module_service ->expects($this->once()) ->method('findByName') ->with('test') ->willReturn($this->fooModule()); $user = new GuestUser(); $request = self::createRequest() ->withAttribute('module', 'test') ->withAttribute('action', 'Testing') ->withAttribute('user', $user); $handler = new ModuleAction($module_service); $handler->handle($request); } public function testNonExistingModule(): void { $this->expectException(HttpNotFoundException::class); $this->expectExceptionMessage('Module test does not exist'); $module_service = $this->createMock(ModuleService::class); $module_service ->expects($this->once()) ->method('findByName') ->with('test') ->willReturn(null); $user = new GuestUser(); $request = self::createRequest() ->withAttribute('module', 'test') ->withAttribute('action', 'Test') ->withAttribute('user', $user); $handler = new ModuleAction($module_service); $handler->handle($request); } public function testAdminAction(): void { $this->expectException(HttpAccessDeniedException::class); $this->expectExceptionMessage('Admin only action'); $module_service = $this->createMock(ModuleService::class); $module_service ->expects($this->once()) ->method('findByName') ->with('test') ->willReturn($this->fooModule()); $user = new GuestUser(); $request = self::createRequest() ->withAttribute('module', 'test') ->withAttribute('action', 'Admin') ->withAttribute('user', $user); $handler = new ModuleAction($module_service); $handler->handle($request); } private function fooModule(): ModuleInterface { return new class () extends AbstractModule { public function getTestAction(): ResponseInterface { return response('It works!'); } }; } }