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;
18
 
19
use core\router\schema\objects\schema_object;
20
use core\router\schema\response\content\json_media_type;
21
use core\router\schema\response\content\payload_response_type;
22
use core\router\schema\specification;
23
use core\tests\router\route_testcase;
24
use GuzzleHttp\Psr7\ServerRequest;
25
 
26
/**
27
 * Tests for the request_body object.
28
 *
29
 * @package    core
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\schema\request_body
33
 * @covers     \core\router\schema\openapi_base
34
 */
35
final class request_body_test extends route_testcase {
36
    public function test_basics(): void {
37
        $object = new request_body();
38
 
39
        $schema = $object->get_openapi_description(new specification());
40
        $this->assertEquals((object) [
41
            'description' => '',
42
            'required' => false,
43
            'content' => [],
44
        ], $schema);
45
        $this->assertFalse($schema->required);
46
        $this->assertFalse($object->is_required());
47
    }
48
 
49
    public function test_content_wrong_type(): void {
50
        $this->expectException(\coding_exception::class);
51
        new request_body(
52
            content: [new schema_object(content: [])],
53
        );
54
    }
55
 
56
    public function test_content_array(): void {
57
        $object = new request_body(
58
            content: [
59
                new json_media_type(content: [], required: true),
60
            ],
61
        );
62
 
63
        $schema = $object->get_openapi_schema(new specification());
64
        $this->assertObjectHasProperty('content', $schema);
65
        $this->assertObjectHasProperty('application/json', (object) $schema->content);
66
 
67
        $request = new ServerRequest('GET', 'http://example.com', [
68
            'Content-Type' => json_media_type::get_encoding(),
69
        ]);
70
        $body = $object->get_body_for_request($request);
71
        $this->assertInstanceOf(json_media_type::class, $body);
72
        $this->assertTrue($body->is_required());
73
    }
74
 
75
    public function test_content_not_matching(): void {
76
        $object = new request_body(
77
            content: [
78
                new json_media_type(content: []),
79
            ],
80
        );
81
        $this->expectException(\invalid_parameter_exception::class);
82
        $object->get_body_for_request(new ServerRequest('GET', 'http://example.com'));
83
    }
84
 
85
    public function test_content_payload_type(): void {
86
        $content = new payload_response_type(content: [], required: true);
87
        $object = new request_body(
88
            content: $content,
89
        );
90
 
91
        $schema = $object->get_openapi_schema(new specification());
92
        $this->assertObjectHasProperty('content', $schema);
93
 
94
        foreach ($content->get_supported_content_types() as $contenttypeclass) {
95
            $encoding = $contenttypeclass::get_encoding();
96
            $this->assertObjectHasProperty($encoding, $schema->content);
97
            $this->assertObjectNotHasProperty('$ref', $schema->content->{$encoding});
98
 
99
            $request = new ServerRequest('GET', 'http://example.com', [
100
                'Content-Type' => $encoding,
101
            ]);
102
            $body = $object->get_body_for_request($request);
103
            $this->assertInstanceOf($contenttypeclass, $body);
104
        }
105
    }
106
 
107
    /**
108
     * Test object referencing.
109
     *
110
     * @covers \core\router\schema\openapi_base
111
     */
112
    public function test_referenced_object(): void {
113
        $object = new class extends request_body implements referenced_object {
114
        };
115
 
116
        // Note: The status code is not in the OpenAPI schema, but in the parent.
117
        $schema = $object->get_openapi_description(new specification());
118
        $this->assertObjectNotHasProperty('$ref', $schema);
119
        $this->assertObjectHasProperty('description', $schema);
120
 
121
        $reference = $object->get_openapi_schema(new specification());
122
        $this->assertObjectNotHasProperty('description', $reference);
123
        $this->assertObjectHasProperty('$ref', $reference);
124
    }
125
 
126
    public function test_reference_content(): void {
127
        $object = new request_body(
128
            content: [],
129
        );
130
        $object = new class extends request_body implements referenced_object {
131
        };
132
 
133
        $schema = $object->get_openapi_schema(new specification());
134
        $this->assertObjectNotHasProperty('content', $schema);
135
    }
136
}