xref: /webtrees/tests/app/Http/RequestHandlers/RedirectCalendarPhpTest.php (revision d4ec5ec0fe611c85b1d77527db3e9becde7e473d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\RequestMethodInterface;
23use Fig\Http\Message\StatusCodeInterface;
24use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
25use Fisharebest\Webtrees\Services\TreeService;
26use Fisharebest\Webtrees\TestCase;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29
30/**
31 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectCalendarPhp
32 */
33class RedirectCalendarPhpTest extends TestCase
34{
35    /**
36     * @return void
37     */
38    public function testRedirect(): void
39    {
40        $tree = $this->createStub(Tree::class);
41        $tree
42            ->method('name')
43            ->willReturn('tree1');
44
45        $tree_service = $this->createStub(TreeService::class);
46        $tree_service
47            ->expects(self::once())
48            ->method('all')
49            ->willReturn(new Collection(['tree1' => $tree]));
50
51        $handler = new RedirectCalendarPhp($tree_service);
52
53        $request = self::createRequest(
54            RequestMethodInterface::METHOD_GET,
55            ['ged' => 'tree1'],
56            [],
57            [],
58            ['base_url' => 'https://www.example.com']
59        );
60
61        $response = $handler->handle($request);
62
63        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
64        self::assertSame(
65            'https://www.example.com/index.php?route=%2Ftree1%2Fcalendar%2Fday',
66            $response->getHeaderLine('Location')
67        );
68    }
69
70    /**
71     * @return void
72     */
73    public function testNoSuchTree(): void
74    {
75        $tree_service = $this->createStub(TreeService::class);
76        $tree_service
77            ->expects(self::once())
78            ->method('all')
79            ->willReturn(new Collection([]));
80
81        $handler = new RedirectCalendarPhp($tree_service);
82
83        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
84
85        $this->expectException(HttpNotFoundException::class);
86
87        $handler->handle($request);
88    }
89}
90