. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fig\Http\Message\RequestMethodInterface; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\Factories\NoteFactory; use Fisharebest\Webtrees\Note; use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException; use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Services\TreeService; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\Tree; use Illuminate\Support\Collection; /** * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectNotePhp */ class RedirectNotePhpTest extends TestCase { /** * @return void */ public function testRedirect(): void { $tree = $this->createStub(Tree::class); $tree ->method('name') ->willReturn('tree1'); $tree_service = $this->createStub(TreeService::class); $tree_service ->expects(self::once()) ->method('all') ->willReturn(new Collection(['tree1' => $tree])); $note = $this->createStub(Note::class); $note ->method('url') ->willReturn('https://www.example.com'); $note_factory = $this->createStub(NoteFactory::class); $note_factory ->expects(self::once()) ->method('make') ->with('X123', $tree) ->willReturn($note); Registry::noteFactory($note_factory); $handler = new RedirectNotePhp($tree_service); $request = self::createRequest( RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'nid' => 'X123'] ); $response = $handler->handle($request); self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); } /** * @return void */ public function testNoSuchRecord(): void { $tree = $this->createStub(Tree::class); $tree_service = $this->createStub(TreeService::class); $tree_service ->expects(self::once()) ->method('all') ->willReturn(new Collection([$tree])); $handler = new RedirectNotePhp($tree_service); $request = self::createRequest( RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'nid' => 'X123'] ); $this->expectException(HttpNotFoundException::class); $handler->handle($request); } /** * @return void */ public function testMissingTreeParameter(): void { $tree_service = $this->createStub(TreeService::class); $handler = new RedirectNotePhp($tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['nid' => 'X123']); $this->expectException(HttpBadRequestException::class); $handler->handle($request); } /** * @return void */ public function testMissingXrefParameter(): void { $tree_service = $this->createStub(TreeService::class); $handler = new RedirectNotePhp($tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); $this->expectException(HttpBadRequestException::class); $handler->handle($request); } }