xref: /webtrees/tests/app/Http/Middleware/HandleExceptionsTest.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
103f10823SGreg Roach<?php
203f10823SGreg Roach
303f10823SGreg Roach/**
403f10823SGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
603f10823SGreg Roach * This program is free software: you can redistribute it and/or modify
703f10823SGreg Roach * it under the terms of the GNU General Public License as published by
803f10823SGreg Roach * the Free Software Foundation, either version 3 of the License, or
903f10823SGreg Roach * (at your option) any later version.
1003f10823SGreg Roach * This program is distributed in the hope that it will be useful,
1103f10823SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1203f10823SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1303f10823SGreg Roach * GNU General Public License for more details.
1403f10823SGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1603f10823SGreg Roach */
17fcfa147eSGreg Roach
1803f10823SGreg Roachdeclare(strict_types=1);
1903f10823SGreg Roach
2003f10823SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
2103f10823SGreg Roach
2271378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpServerErrorException;
2403f10823SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
25040e7dbaSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
2603f10823SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
2703f10823SGreg Roachuse Fisharebest\Webtrees\TestCase;
2803f10823SGreg Roachuse Illuminate\Support\Collection;
2903f10823SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3003f10823SGreg Roach
3103f10823SGreg Roachuse function app;
3203f10823SGreg Roach
3303f10823SGreg Roach/**
3403f10823SGreg Roach * Test the HandleExceptions middleware.
3503f10823SGreg Roach *
3603f10823SGreg Roach * @covers \Fisharebest\Webtrees\Http\Middleware\HandleExceptions
3703f10823SGreg Roach */
3803f10823SGreg Roachclass HandleExceptionsTest extends TestCase
3903f10823SGreg Roach{
4003f10823SGreg Roach    /**
4103f10823SGreg Roach     * @return void
4203f10823SGreg Roach     */
4303f10823SGreg Roach    public function testMiddleware(): void
4403f10823SGreg Roach    {
455e933c21SGreg Roach        $tree_service = self::createMock(TreeService::class);
46040e7dbaSGreg Roach
475e933c21SGreg Roach        $handler = self::createMock(RequestHandlerInterface::class);
48d501c45dSGreg Roach        $handler->method('handle')->willThrowException(new HttpServerErrorException('eek'));
4903f10823SGreg Roach
505e933c21SGreg Roach        $module_service = self::createMock(ModuleService::class);
51075d1a05SGreg Roach        $module_service->method('findByInterface')->willReturn(new Collection());
52075d1a05SGreg Roach        $module_service->method('findByComponent')->willReturn(new Collection());
5303f10823SGreg Roach        app()->instance(ModuleService::class, $module_service);
5403f10823SGreg Roach
5503f10823SGreg Roach        $request    = self::createRequest();
56040e7dbaSGreg Roach        $middleware = new HandleExceptions($tree_service);
5703f10823SGreg Roach        $response   = $middleware->process($request, $handler);
5803f10823SGreg Roach
595e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR, $response->getStatusCode());
6003f10823SGreg Roach
6103f10823SGreg Roach        app()->forgetInstance(ModuleService::class);
6203f10823SGreg Roach        app()->forgetInstance(UserService::class);
6303f10823SGreg Roach    }
6403f10823SGreg Roach}
65