xref: /webtrees/tests/app/Http/Middleware/BootModulesTest.php (revision d709fcb38b4132db21700fb78c2b31d7b5b55349)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\Middleware;
20
21use Fisharebest\Webtrees\Module\WebtreesTheme;
22use Fisharebest\Webtrees\Module\XeneaTheme;
23use Fisharebest\Webtrees\Services\ModuleService;
24use Fisharebest\Webtrees\TestCase;
25use Illuminate\Support\Collection;
26use Psr\Http\Server\RequestHandlerInterface;
27
28use function response;
29
30/**
31 * Test the BootModules middleware.
32 *
33 * @covers \Fisharebest\Webtrees\Http\Middleware\BootModules
34 */
35class BootModulesTest extends TestCase
36{
37    /**
38     * @return void
39     */
40    public function testMiddleware(): void
41    {
42        $handler = $this->createMock(RequestHandlerInterface::class);
43        $handler->method('handle')->willReturn(response());
44
45        // Theme 1 (not default) is not booted.
46        $theme1 = new class extends WebtreesTheme {
47            public function boot()
48            {
49                throw new \Exception('Should not get here!');
50            }
51        };
52
53        // Theme 2 (default) is booted.
54        $theme2 = new class($this) extends XeneaTheme {
55            private $booted = false;
56            private $test;
57
58            public function __construct($test)
59            {
60                $this->test = $test;
61            }
62
63            public function boot()
64            {
65                $this->booted = true;
66            }
67
68            public function __destruct()
69            {
70                $this->test->assertTrue($this->booted);
71            }
72        };
73
74        $module_service = $this->createMock(ModuleService::class);
75        $module_service->method('all')->willReturn(new Collection([$theme1, $theme2]));
76
77        $request    = self::createRequest();
78        $middleware = new BootModules($module_service, $theme2);
79        $response   = $middleware->process($request, $handler);
80
81        $this->assertSame(self::STATUS_OK, $response->getStatusCode());
82    }
83}
84