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\schema\parameters;
18
 
19
use core\param;
20
use core\router\route;
21
use core\router\schema\referenced_object;
22
use core\router\schema\specification;
23
use core\tests\router\route_testcase;
24
use invalid_parameter_exception;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Slim\Routing\RouteContext;
27
 
28
/**
29
 * Tests for the path parameter.
30
 *
31
 * @package    core
32
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 * @covers     \core\router\schema\parameter
35
 * @covers     \core\router\schema\parameters\path_parameter
36
 * @covers     \core\router\schema\openapi_base
37
 */
38
final class path_parameter_test extends route_testcase {
39
    public function test_in_path(): void {
40
        $param = new path_parameter(name: 'example');
41
        $this->assertEquals('path', $param->get_in());
42
        $this->assertEquals('example', $param->get_name());
43
    }
44
 
45
    /**
46
     * Test the is_required method.
47
     *
48
     * @dataProvider is_required_provider
49
     * @param string $path
50
     * @param bool $expected
51
     */
52
    public function test_is_required(string $path, bool $expected): void {
53
        $route = new route(
54
            path: $path,
55
        );
56
        $param = new path_parameter(name: 'value');
57
        $this->assertEquals($expected, $param->is_required($route));
58
    }
59
 
60
    /**
61
     * Data provider for the is_required method.
62
     *
63
     * @return array
64
     */
65
    public static function is_required_provider(): array {
66
        return [
67
            ['/is/not/found', false],
68
            ['/is/not/found/{values}', false],
69
            ['/is/not/found/{values:.*}', false],
70
            ['/is/not/found/{values:.*?}', false],
71
            ['/is/required/{value}', true],
72
            ['/is/required/{value:.*}', true],
73
            ['/is/required/{value:.*?}/example', true],
74
            ['/is/optional/[{value}]', false],
75
            ['/is/[optional/[{value}]]', false],
76
            ['/is/[optional/[{value:.*}]]', false],
77
            ['/is/[optional/[{value:.*?}/example]]', false],
78
            ['/is/required/{value}[/example]', true],
79
        ];
80
    }
81
 
82
    /**
83
     * Test fo the OPenAPI description in different configurations.
84
     *
85
     * @dataProvider openapi_required_values_provider
86
     * @param string $path If the a value is a required part of the path
87
     * @param bool $required If the value is expected to be required
88
     */
89
    public function test_get_openapi_description_required_values(
90
        string $path,
91
        bool $required,
92
    ): void {
93
        $param = new path_parameter(
94
            name: 'value',
95
            type: param::INT,
96
        );
97
 
98
        $api = new specification();
99
        $result = $param->get_openapi_description($api, $path);
100
        if ($required) {
101
            $this->assertNotNull($result);
102
            $this->assertTrue($result->required);
103
            $this->assertEquals('value', $result->name);
104
        } else {
105
            $this->assertNull($result);
106
        }
107
    }
108
 
109
    /**
110
     * Data provider for OpenAPI Required values.
111
     *
112
     * @return array
113
     */
114
    public static function openapi_required_values_provider(): array {
115
        return [
116
            ['/is/required/{value}/with/children', true],
117
            ['/is/required/{value}', true],
118
            ['/is/optional', false],
119
        ];
120
    }
121
 
122
    /**
123
     * Ensure that a validation failure results in an invalid_parameter_exception.
124
     */
125
    public function test_validation_failure(): void {
126
        $param = new path_parameter(
127
            name: 'example',
128
            type: param::INT,
129
        );
130
        $value = "example";
131
 
132
        $request = $this->create_route(
133
            '/example/{example}',
134
            "/example/{$value}",
135
        );
136
        $route = $request->getAttribute(RouteContext::ROUTE);
137
 
138
        $this->expectException(invalid_parameter_exception::class);
139
        $param->validate($request, $route);
140
    }
141
 
142
    public function test_validation_success(): void {
143
        $param = new path_parameter(
144
            name: 'example',
145
            type: param::INT,
146
        );
147
        $value = 12345;
148
 
149
        $request = $this->create_route(
150
            '/example/{example}',
151
            "/example/{$value}",
152
        );
153
        $route = $request->getAttribute(RouteContext::ROUTE);
154
 
155
        $validatedresult = $param->validate($request, $route);
156
        $this->assertInstanceOf(ServerRequestInterface::class, $validatedresult);
157
    }
158
 
159
    public function test_referenced_object(): void {
160
        $object = new class (
161
            name: 'example',
162
            type: param::INT,
163
        ) extends path_parameter implements referenced_object {
164
        };
165
 
166
        $schema = $object->get_openapi_description(new specification());
167
        $this->assertObjectNotHasProperty('$ref', $schema);
168
        $this->assertObjectHasProperty('schema', $schema);
169
 
170
        $reference = $object->get_openapi_schema(new specification());
171
        $this->assertObjectNotHasProperty('schema', $reference);
172
        $this->assertObjectHasProperty('$ref', $reference);
173
    }
174
}