. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fig\Http\Message\RequestMethodInterface; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\Factories\IndividualFactory; use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Module\StatisticsChartModule; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\Services\TreeService; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\Tree; use Illuminate\Support\Collection; /** * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectStatisticsPhp */ class RedirectStatisticsPhpTest 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])); $module = $this->createStub(StatisticsChartModule::class); $module ->expects(self::once()) ->method('chartUrl') ->willReturn('https://www.example.com'); $module_service = $this->createStub(ModuleService::class); $module_service ->expects(self::once()) ->method('findByInterface') ->with(StatisticsChartModule::class) ->willReturn(new Collection([$module])); $handler = new RedirectStatisticsPhp($module_service, $tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); $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 testModuleDisabled(): void { $module_service = $this->createStub(ModuleService::class); $module_service ->expects(self::once())->method('findByInterface') ->with(StatisticsChartModule::class) ->willReturn(new Collection()); $tree = $this->createStub(Tree::class); $tree_service = $this->createStub(TreeService::class); $tree_service ->expects(self::once()) ->method('all') ->willReturn(new Collection([$tree])); $handler = new RedirectStatisticsPhp($module_service, $tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); $this->expectException(HttpNotFoundException::class); $handler->handle($request); } /** * @return void */ public function testNoSuchTree(): void { $module = $this->createStub(StatisticsChartModule::class); $module_service = $this->createStub(ModuleService::class); $module_service ->expects(self::once()) ->method('findByInterface') ->with(StatisticsChartModule::class) ->willReturn(new Collection([$module])); $tree_service = $this->createStub(TreeService::class); $tree_service ->expects(self::once()) ->method('all') ->willReturn(new Collection([])); $handler = new RedirectStatisticsPhp($module_service, $tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); $this->expectException(HttpNotFoundException::class); $handler->handle($request); } }