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\router\schema\referenced_object;
20
use core\router\schema\specification;
21
use core\tests\router\route_testcase;
22
 
23
/**
24
 * Tests for the schema_object.
25
 *
26
 * @package    core
27
 * @copyright  2023 Andrew Lyons <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers     \core\router\schema\objects\schema_object
30
 * @covers     \core\router\schema\openapi_base
31
 */
32
final class schema_object_test extends route_testcase {
33
    public function test_referenced_object(): void {
34
        $object = new class (
35
            content: [
36
                'example' => new schema_object(content: []),
37
            ],
38
        ) extends schema_object implements referenced_object {
39
        };
40
 
41
        $schema = $object->get_openapi_description(new specification());
42
        $this->assertObjectNotHasProperty('$ref', $schema);
43
        $this->assertObjectHasProperty('type', $schema);
44
        $this->assertObjectHasProperty('properties', $schema);
45
        $this->assertObjectHasProperty('example', $schema->properties);
46
 
47
        $reference = $object->get_openapi_schema(new specification());
48
        $this->assertObjectNotHasProperty('type', $reference);
49
        $this->assertObjectHasProperty('$ref', $reference);
50
    }
51
 
52
    public function test_basics(): void {
53
        $object = new schema_object(
54
            content: [
55
                'example' => new schema_object(content: []),
56
            ],
57
        );
58
 
59
        $this->assertFalse($object->has('missing'));
60
        $this->assertTrue($object->has('example'));
61
 
62
        $this->assertInstanceOf(schema_object::class, $object->get('example'));
63
    }
64
 
65
    public function test_invalid_content(): void {
66
        $this->expectException(\coding_exception::class);
67
 
68
        new schema_object(content: ['invalid']);
69
    }
70
 
71
    public function test_validation(): void {
72
        $object = new schema_object(
73
            content: [
74
                'example' => new schema_object(content: [
75
                    'somekey' => new schema_object(content: []),
76
                ]),
77
            ],
78
        );
79
 
80
        // Nothing in, nothing out.
81
        $this->assertEmpty($object->validate_data([]));
82
 
83
        // Surplus data is ignored.
84
        $this->assertEmpty($object->validate_data(['surplus' => []]));
85
 
86
        // Valid data is included.
87
        $data = $object->validate_data(['example' => []]);
88
        $this->assertArrayHasKey('example', $data);
89
        $this->assertIsArray($data['example']);
90
 
91
        // Valid data is included and surplus data is removed.
92
        $data = $object->validate_data(['example' => [], 'surplus' => []]);
93
        $this->assertArrayHasKey('example', $data);
94
        $this->assertIsArray($data['example']);
95
        $this->assertArrayNotHasKey('surplus', $data);
96
 
97
        // Applied to nested values.
98
        $data = $object->validate_data(['example' => ['somekey' => []]]);
99
        $this->assertArrayHasKey('example', $data);
100
        $this->assertIsArray($data['example']);
101
        $this->assertArrayHasKey('somekey', $data['example']);
102
    }
103
}