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\param;
20
use core\tests\router\route_testcase;
21
 
22
/**
23
 * Tests for header objects.
24
 *
25
 * @package    core
26
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers     \core\router\schema\header_object
29
 * @covers     \core\router\schema\parameters\header_object
30
 * @covers     \core\router\schema\openapi_base
31
 */
32
final class header_object_test extends route_testcase {
33
    public function test_in_path(): void {
34
        $param = new header_object(
35
            name: 'example',
36
            type: param::INT,
37
        );
38
        $this->assertEquals('header', $param->get_in());
39
        $this->assertEquals('example', $param->get_name());
40
 
41
        // Fetch the description directly.
42
        $schema = $param->get_openapi_description(new specification());
43
        $this->assertObjectNotHasProperty('$ref', $schema);
44
        $this->assertObjectHasProperty('schema', $schema);
45
        $this->assertObjectHasProperty('type', $schema->schema);
46
        $this->assertEquals('integer', $schema->schema->type);
47
        $this->assertObjectNotHasProperty('in', $schema);
48
        $this->assertObjectNotHasProperty('name', $schema);
49
 
50
        // Should have the same response a via the get_openapi_schema method.
51
        $schema = $param->get_openapi_schema(new specification());
52
        $this->assertObjectNotHasProperty('$ref', $schema);
53
        $this->assertObjectHasProperty('schema', $schema);
54
        $this->assertObjectHasProperty('type', $schema->schema);
55
        $this->assertEquals('integer', $schema->schema->type);
56
        $this->assertObjectNotHasProperty('in', $schema);
57
        $this->assertObjectNotHasProperty('name', $schema);
58
    }
59
 
60
    public function test_referenced_object(): void {
61
        $object = new class extends header_object implements referenced_object {
62
            /**
63
             * Constructor.
64
             */
65
            public function __construct() {
66
                parent::__construct(
67
                    name: 'example',
68
                    type: param::INT,
69
                );
70
            }
71
        };
72
 
73
        $schema = $object->get_openapi_description(new specification());
74
        $this->assertObjectNotHasProperty('$ref', $schema);
75
        $this->assertObjectHasProperty('schema', $schema);
76
        $this->assertObjectHasProperty('type', $schema->schema);
77
        $this->assertEquals('integer', $schema->schema->type);
78
        $this->assertObjectNotHasProperty('in', $schema);
79
        $this->assertObjectNotHasProperty('name', $schema);
80
 
81
        $reference = $object->get_openapi_schema(new specification());
82
        $this->assertObjectNotHasProperty('schema', $reference);
83
        $this->assertObjectHasProperty('$ref', $reference);
84
    }
85
}