| 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\response;
|
|
|
18 |
|
|
|
19 |
use core\param;
|
|
|
20 |
use core\router\schema\header_object;
|
|
|
21 |
use core\router\schema\objects\schema_object;
|
|
|
22 |
use core\router\schema\referenced_object;
|
|
|
23 |
use core\router\schema\response\content\json_media_type;
|
|
|
24 |
use core\router\schema\response\content\payload_response_type;
|
|
|
25 |
use core\router\schema\specification;
|
|
|
26 |
use core\tests\router\route_testcase;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Tests for the response schema definition.
|
|
|
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\response\response
|
|
|
35 |
*/
|
|
|
36 |
final class response_test extends route_testcase {
|
|
|
37 |
public function test_defaults(): void {
|
|
|
38 |
$response = new response();
|
|
|
39 |
|
|
|
40 |
// The default status code is 200.
|
|
|
41 |
$this->assertSame(200, $response->get_status_code());
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public function test_basics(): void {
|
|
|
45 |
$response = new response(
|
|
|
46 |
statuscode: 450,
|
|
|
47 |
description: 'This is a nice description about the response',
|
|
|
48 |
headers: [
|
|
|
49 |
new header_object(
|
|
|
50 |
name: 'X-Header',
|
|
|
51 |
type: param::ALPHA,
|
|
|
52 |
description: 'This is a nice description about the header',
|
|
|
53 |
required: true,
|
|
|
54 |
),
|
|
|
55 |
],
|
|
|
56 |
content: new payload_response_type(
|
|
|
57 |
description: 'This is a nice description about the content',
|
|
|
58 |
schema: new schema_object(
|
|
|
59 |
content: [
|
|
|
60 |
'example' => new schema_object(content: []),
|
|
|
61 |
],
|
|
|
62 |
),
|
|
|
63 |
),
|
|
|
64 |
);
|
|
|
65 |
|
|
|
66 |
// The default status code is 200.
|
|
|
67 |
$this->assertSame(450, $response->get_status_code());
|
|
|
68 |
$schema = $response->get_openapi_schema(new specification());
|
|
|
69 |
|
|
|
70 |
$this->assertObjectHasProperty('description', $schema);
|
|
|
71 |
$this->assertEquals('This is a nice description about the response', $schema->description);
|
|
|
72 |
|
|
|
73 |
$this->assertObjectHasProperty('headers', $schema);
|
|
|
74 |
$this->assertArrayHasKey('X-Header', $schema->headers);
|
|
|
75 |
$this->assertObjectHasProperty('description', $schema->headers['X-Header']);
|
|
|
76 |
$this->assertEquals('This is a nice description about the header', $schema->headers['X-Header']->description);
|
|
|
77 |
$this->assertObjectHasProperty('schema', $schema->headers['X-Header']);
|
|
|
78 |
|
|
|
79 |
$this->assertObjectHasProperty('content', $schema);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function test_default_200_description(): void {
|
|
|
83 |
$response = new response(
|
|
|
84 |
statuscode: 200,
|
|
|
85 |
);
|
|
|
86 |
|
|
|
87 |
// The default status code is 200.
|
|
|
88 |
$this->assertSame(200, $response->get_status_code());
|
|
|
89 |
$schema = $response->get_openapi_schema(new specification());
|
|
|
90 |
$this->assertObjectHasProperty('description', $schema);
|
|
|
91 |
$this->assertIsString($schema->description);
|
|
|
92 |
$this->assertEquals('OK', $schema->description);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public function test_array_content(): void {
|
|
|
96 |
$response = new response(
|
|
|
97 |
statuscode: 450,
|
|
|
98 |
content: [
|
|
|
99 |
new json_media_type(
|
|
|
100 |
schema: new schema_object(
|
|
|
101 |
content: [
|
|
|
102 |
'example' => new schema_object(content: []),
|
|
|
103 |
],
|
|
|
104 |
),
|
|
|
105 |
),
|
|
|
106 |
],
|
|
|
107 |
);
|
|
|
108 |
|
|
|
109 |
// The default status code is 200.
|
|
|
110 |
$this->assertSame(450, $response->get_status_code());
|
|
|
111 |
$schema = $response->get_openapi_schema(new specification());
|
|
|
112 |
$this->assertObjectHasProperty('description', $schema);
|
|
|
113 |
$this->assertIsString($schema->description);
|
|
|
114 |
|
|
|
115 |
$this->assertObjectHasProperty('content', $schema);
|
|
|
116 |
$this->assertArrayHasKey('application/json', $schema->content);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function test_invalid_content(): void {
|
|
|
120 |
$this->expectException(\coding_exception::class);
|
|
|
121 |
|
|
|
122 |
$response = new response(
|
|
|
123 |
content: [
|
|
|
124 |
new schema_object(
|
|
|
125 |
content: [],
|
|
|
126 |
),
|
|
|
127 |
],
|
|
|
128 |
);
|
|
|
129 |
$response->get_openapi_schema(new specification());
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Tests for object references.
|
|
|
134 |
*
|
|
|
135 |
* @covers \core\router\schema\openapi_base
|
|
|
136 |
*/
|
|
|
137 |
public function test_referenced_object(): void {
|
|
|
138 |
$object = new class (
|
|
|
139 |
statuscode: 499,
|
|
|
140 |
) extends response implements referenced_object {
|
|
|
141 |
};
|
|
|
142 |
|
|
|
143 |
// Note: The status code is not in the OpenAPI schema, but in the parent.
|
|
|
144 |
$schema = $object->get_openapi_description(new specification());
|
|
|
145 |
$this->assertObjectNotHasProperty('$ref', $schema);
|
|
|
146 |
$this->assertObjectHasProperty('description', $schema);
|
|
|
147 |
|
|
|
148 |
$reference = $object->get_openapi_schema(new specification());
|
|
|
149 |
$this->assertObjectNotHasProperty('description', $reference);
|
|
|
150 |
$this->assertObjectHasProperty('$ref', $reference);
|
|
|
151 |
}
|
|
|
152 |
}
|