Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\router\middleware;
18
 
19
use core\di;
20
use core\router\response_handler;
21
use core\tests\router\route_testcase;
22
use GuzzleHttp\Psr7\ServerRequest;
23
use Psr\Http\Message\ResponseInterface;
24
 
25
/**
26
 * Tests for the CORS middleware.
27
 *
28
 * @package    core
29
 * @category   test
30
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @covers \core\router\middleware\error_handling_middleware
33
 */
34
final class error_handling_middleware_test extends route_testcase {
35
    /**
36
     * When no errors, the error handle is not called.
37
     */
38
    public function test_no_errors(): void {
39
        $responsehandler = $this->getMockBuilder(response_handler::class)
40
            ->disableOriginalConstructor()
41
            ->getMock();
42
        $responsehandler->expects($this->never())->method('get_response_from_exception');
43
 
44
        di::set(response_handler::class, $responsehandler);
45
 
46
        $app = $this->get_simple_app();
47
        $app->add(di::get(error_handling_middleware::class));
48
 
49
        $app->map(['GET'], '/test', fn ($request, $response) => $response);
50
 
51
        // Handle the request.
52
        $request = new ServerRequest('GET', '/test');
53
        $returns = $app->handle($request);
54
        $this->assertInstanceOf(ResponseInterface::class, $returns);
55
    }
56
 
57
    /**
58
     * When no errors, the error handle is not called.
59
     */
60
    public function test_error_handling(): void {
61
        $responsehandler = $this->getMockBuilder(response_handler::class)
62
            ->disableOriginalConstructor()
63
            ->getMock();
64
        $responsehandler->expects($this->once())->method('get_response_from_exception');
65
 
66
        di::set(response_handler::class, $responsehandler);
67
 
68
        // Configure the app with one middleware that throws an exception.
69
        $app = $this->get_simple_app();
70
        $app->add(fn ($request, $handler) => throw new \Exception('Test'));
71
        $app->add(di::get(error_handling_middleware::class));
72
 
73
        $app->map(['GET'], '/test', fn ($request, $response) => $response);
74
 
75
        // Handle the request.
76
        $request = new ServerRequest('GET', '/test');
77
        $returns = $app->handle($request);
78
        $this->assertInstanceOf(ResponseInterface::class, $returns);
79
    }
80
}