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\objects;
18
 
19
use core\param;
20
use core\router\schema\referenced_object;
21
use core\router\schema\specification;
22
use core\tests\router\route_testcase;
23
 
24
/**
25
 * Tests for the an array of other objects.
26
 *
27
 * @package    core
28
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @covers     \core\router\schema\objects\array_of_things
31
 * @covers     \core\router\schema\objects\type_base
32
 * @covers     \core\router\schema\openapi_base
33
 */
34
final class array_of_things_test extends route_testcase {
35
    public function test_referenced_object(): void {
36
        $object = new class ( // phpcs:ignore
37
            thingtype: 'integer',
38
            content: [
39
                'example' => new schema_object(content: []),
40
            ],
41
        ) extends array_of_things implements referenced_object {
42
        };
43
 
44
        $schema = $object->get_openapi_description(new specification());
45
        $this->assertObjectNotHasProperty('$ref', $schema);
46
        $this->assertObjectHasProperty('type', $schema);
47
        $this->assertEquals('object', $schema->type);
48
        $this->assertObjectNotHasProperty('properties', $schema);
49
        $this->assertObjectHasProperty('additionalProperties', $schema);
50
        $this->assertArrayHasKey('type', $schema->additionalProperties);
51
        $this->assertEquals('integer', $schema->additionalProperties['type']);
52
 
53
        $reference = $object->get_openapi_schema(new specification());
54
        $this->assertObjectNotHasProperty('type', $reference);
55
        $this->assertObjectHasProperty('$ref', $reference);
56
    }
57
 
58
    public function test_referenced_object_typebase(): void {
59
        $object = new class ( // phpcs:ignore
60
            thingtype: new scalar_type(param::INT),
61
        ) extends array_of_things implements referenced_object {
62
        };
63
 
64
        $schema = $object->get_openapi_description(new specification());
65
        $this->assertArrayHasKey('$ref', $schema->additionalProperties);
66
 
67
        $reference = $object->get_openapi_schema(new specification());
68
        $this->assertObjectNotHasProperty('type', $reference);
69
        $this->assertObjectHasProperty('$ref', $reference);
70
    }
71
 
72
    public function test_basics(): void {
73
        $object = new array_of_things(
74
            thingtype: 'integer',
75
        );
76
 
77
        $schema = $object->get_openapi_description(new specification());
78
        $this->assertEquals((object) [
79
            'type' => 'object',
80
            'additionalProperties' => [
81
                'type' => 'integer',
82
            ],
83
        ], $schema);
84
    }
85
 
86
 
87
    /**
88
     * Test tha the validate_data method successfully validates content.
89
     *
90
     * @dataProvider successful_validation_provider
91
     * @param param|string|type_base|null $valueparamtype
92
     * @param mixed $data
93
     */
94
    public function test_validation_success(
95
        param|string|type_base|null $valueparamtype,
96
        mixed $data,
97
    ): void {
98
        $object = new array_of_things(
99
            $valueparamtype,
100
        );
101
 
102
        $result = $object->validate_data($data);
103
        $this->assertEquals($data, $result);
104
    }
105
 
106
    /**
107
     * Data provider for test_validation_success.
108
     *
109
     * @return array
110
     */
111
    public static function successful_validation_provider(): array {
112
        return [
113
            [
114
                param::INT,
115
                [
116
                    'example' => 123,
117
                    'other' => 321,
118
                    'more' => 5634543456543456,
119
                ],
120
            ],
121
            [
122
                'int',
123
                [
124
                    'example' => 123,
125
                    'other' => 321,
126
                    'more' => 5634543456543456,
127
                ],
128
            ],
129
            [
130
                new scalar_type(param::INT),
131
                [
132
                    'example' => 123,
133
                    'other' => 321,
134
                    'more' => 5634543456543456,
135
                ],
136
            ],
137
            [
138
                null,
139
                [
140
                    'example' => 123,
141
                    'other' => 321,
142
                    'more' => 'This is a string',
143
                ],
144
            ],
145
        ];
146
    }
147
 
148
    /**
149
     * Test tha the validate_data method throws an exception when the data is invalid.
150
     *
151
     * @dataProvider failed_validation_provider
152
     * @param param|string|type_base|null $valueparamtype
153
     * @param mixed $data
154
     */
155
    public function test_validation_failures(
156
        param|string|type_base|null $valueparamtype,
157
        mixed $data,
158
    ): void {
159
        $object = new array_of_things(
160
            $valueparamtype,
161
        );
162
 
163
        $this->expectException(\invalid_parameter_exception::class);
164
        $object->validate_data($data);
165
    }
166
 
167
    /**
168
     * Data provider for test_validation_failures.
169
     *
170
     * @return array
171
     */
172
    public static function failed_validation_provider(): array {
173
        return [
174
            [
175
                'int',
176
                [
177
                    'example' => 123,
178
                    'other' => 'threetwoone',
179
                ],
180
            ],
181
            [
182
                new scalar_type(param::INT),
183
                [
184
                    'example' => 123,
185
                    'other' => 'threetwoone',
186
                ],
187
            ],
188
            [
189
                param::INT,
190
                [
191
                    'example' => 123,
192
                    'other' => 'threetwoone',
193
                ],
194
            ],
195
            [
196
                param::INT,
197
                'string',
198
            ],
199
        ];
200
    }
201
}