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\specification;
21
use core\tests\router\route_testcase;
22
 
23
/**
24
 * Tests for the an array of other objects.
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\array_of_strings
30
 * @covers     \core\router\schema\objects\type_base
31
 * @covers     \core\router\schema\openapi_base
32
 */
33
final class array_of_strings_test extends route_testcase {
34
    public function test_referenced_object(): void {
35
        $object = new array_of_strings();
36
 
37
        $schema = $object->get_openapi_description(new specification());
38
        $this->assertEquals((object) [
39
            'type' => 'object',
40
            'additionalProperties' => [
41
                'type' => 'string',
42
            ],
43
        ], $schema);
44
 
45
        $reference = $object->get_openapi_schema(new specification());
46
        $this->assertObjectNotHasProperty('type', $reference);
47
        $this->assertObjectHasProperty('$ref', $reference);
48
    }
49
 
50
    public function test_validation(): void {
51
        $object = new array_of_strings(
52
            keyparamtype: param::ALPHANUM,
53
            valueparamtype: param::INT,
54
        );
55
 
56
        $data = $object->validate_data([
57
            'example' => 123,
58
            'other' => 321,
59
        ]);
60
 
61
        $this->assertCount(2, $data);
62
        $this->assertEquals([
63
            'example' => '123',
64
            'other' => '321',
65
        ], $data);
66
    }
67
 
68
    /**
69
     * Test tha the validate_data method throws an exception when the data is invalid.
70
     *
71
     * @dataProvider failed_validation_provider
72
     * @param param $keyparamtype
73
     * @param param $valueparamtype
74
     * @param array $data
75
     */
76
    public function test_validation_failures(
77
        param $keyparamtype,
78
        param $valueparamtype,
79
        array $data,
80
    ): void {
81
        $object = new array_of_strings(
82
            keyparamtype: $keyparamtype,
83
            valueparamtype: $valueparamtype,
84
        );
85
 
86
        $this->expectException(\invalid_parameter_exception::class);
87
 
88
        $object->validate_data($data);
89
    }
90
 
91
    /**
92
     * Data provider for test_validation_failures.
93
     *
94
     * @return array
95
     */
96
    public static function failed_validation_provider(): array {
97
        return [
98
            [
99
                param::ALPHANUM,
100
                param::INT,
101
                [
102
                    'example' => 123,
103
                    'other' => 'threetwoone',
104
                ],
105
            ],
106
        ];
107
    }
108
}