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;
18
 
19
use core\router\schema\response\response;
20
use GuzzleHttp\Psr7\ServerRequest;
21
use Psr\Http\Message\ResponseInterface;
22
 
23
/**
24
 * Tests for the response_validator.
25
 *
26
 * @package    core
27
 * @category   test
28
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @covers \core\router\response_validator
31
 */
32
final class response_validator_test extends \advanced_testcase {
33
    public function test_validate_response_without_moodle_route(): void {
34
        $validator = new response_validator();
35
 
36
        $request = new ServerRequest('GET', 'http://example.com');
37
        $response = new \GuzzleHttp\Psr7\Response();
38
        $this->assertNull($validator->validate_response($request, $response));
39
    }
40
 
41
    public function test_validate_response_with_route_no_responses(): void {
42
        $validator = new response_validator();
43
 
44
        $route = new route(path: '/test');
45
        $request = (new ServerRequest('GET', 'http://example.com/test'))
46
            ->withAttribute(route::class, $route);
47
        $response = new \GuzzleHttp\Psr7\Response();
48
        $this->assertNull($validator->validate_response($request, $response));
49
    }
50
 
51
    public function test_validate_response_with_route_and_response(): void {
52
        $validator = new response_validator();
53
 
54
        $routeresponse = new class extends response {
55
            // phpcs:ignore
56
            public function __construct() {
57
                parent::__construct(
58
                    statuscode: 200,
59
                    description: 'Test response',
60
                );
61
            }
62
 
63
            // phpcs:ignore
64
            public function validate(
65
                ResponseInterface $response,
66
            ): void {
67
                throw new \Exception('Test exception');
68
            }
69
        };
70
 
71
        $route = new route(path: '/test', responses: [200 => $routeresponse]);
72
        $request = (new ServerRequest('GET', 'http://example.com/test'))
73
            ->withAttribute(route::class, $route);
74
        $response = new \GuzzleHttp\Psr7\Response();
75
 
76
        $this->expectException(\Exception::class);
77
        $this->expectExceptionMessage('Test exception');
78
        $validator->validate_response($request, $response);
79
    }
80
}