Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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;
18
 
19
/**
20
 * Tests for the attribute_helper.
21
 *
22
 * @package    core
23
 * @category   test
24
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @covers \core\attribute_helper
27
 */
28
final class attribute_helper_test extends \advanced_testcase {
29
    public static function setUpBeforeClass(): void {
30
        require_once(__DIR__ . '/fixtures/attribute_helper_example.php');
1441 ariadna 31
        parent::setUpBeforeClass();
1 efrain 32
    }
33
 
34
    /**
35
     * @dataProvider get_attributes_provider
36
     */
37
    public function test_get_attributes(
38
        int $expectedcount,
39
        array $args,
40
    ): void {
41
        $attributes = attribute_helper::from(...$args);
42
        $instances = attribute_helper::instances(...$args);
43
        if ($expectedcount) {
44
            $this->assertNotEmpty($attributes);
45
            $this->assertCount($expectedcount, $attributes);
46
            $this->assertCount($expectedcount, $instances);
47
 
48
        } else {
49
            $this->assertEmpty($attributes);
50
            $this->assertEmpty($instances);
51
        }
52
    }
53
 
54
    public static function get_attributes_provider(): array {
55
        return [
56
            [3, [[attribute_helper_example::class]]],
57
            [0, [[attribute_helper_example_without::class]]],
58
            [3, [[attribute_helper_example::class, 'WITH_ATTRIBUTES']]],
59
            [0, [[attribute_helper_example::class, 'WITHOUT_ATTRIBUTE']]],
60
            [3, [[attribute_helper_example::class, 'withattributes']]],
61
            [0, [[attribute_helper_example::class, 'withoutattributes']]],
62
            [3, [[attribute_helper_example::class, 'with_attributes']]],
63
            [0, [[attribute_helper_example::class, 'without_attributes']]],
64
            [3, [[attribute_helper_enum::class, 'WITH_ATTRIBUTES']]],
65
            [0, [[attribute_helper_enum::class, 'WITHOUT_ATTRIBUTE']]],
66
            [3, [__NAMESPACE__ . '\\attribute_helper_method_with']],
67
            [0, [__NAMESPACE__ . '\\attribute_helper_method_without']],
68
            [0, [__NAMESPACE__ . '\\function_not_exists']],
69
 
70
            [3, [attribute_helper_example::class]],
71
            [0, [attribute_helper_example_without::class]],
72
            [3, [attribute_helper_example::class . '::WITH_ATTRIBUTES']],
73
            [0, [attribute_helper_example::class . '::WITHOUT_ATTRIBUTE']],
74
            [3, [attribute_helper_example::class . '::withattributes']],
75
            [0, [attribute_helper_example::class . '::withoutattributes']],
76
            [3, [attribute_helper_example::class . '::with_attributes']],
77
            [0, [attribute_helper_example::class . '::without_attributes']],
78
            [3, [attribute_helper_enum::class . '::WITH_ATTRIBUTES']],
79
            [0, [attribute_helper_enum::class . '::WITHOUT_ATTRIBUTE']],
80
 
81
            [2, [[attribute_helper_example::class], attribute_helper_attribute_a::class]],
82
            [0, [[attribute_helper_example_without::class], attribute_helper_attribute_a::class]],
83
            [2, [[attribute_helper_example::class, 'WITH_ATTRIBUTES'], attribute_helper_attribute_a::class]],
84
            [0, [[attribute_helper_example::class, 'WITHOUT_ATTRIBUTE'], attribute_helper_attribute_a::class]],
85
            [2, [[attribute_helper_example::class, 'withattributes'], attribute_helper_attribute_a::class]],
86
            [0, [[attribute_helper_example::class, 'withoutattributes'], attribute_helper_attribute_a::class]],
87
            [2, [[attribute_helper_example::class, 'with_attributes'], attribute_helper_attribute_a::class]],
88
            [0, [[attribute_helper_example::class, 'without_attributes'], attribute_helper_attribute_a::class]],
89
            [2, [[attribute_helper_enum::class, 'WITH_ATTRIBUTES'], attribute_helper_attribute_a::class]],
90
            [0, [[attribute_helper_enum::class, 'WITHOUT_ATTRIBUTE'], attribute_helper_attribute_a::class]],
91
            [2, [__NAMESPACE__ . '\\attribute_helper_method_with', attribute_helper_attribute_a::class]],
92
            [0, [__NAMESPACE__ . '\\attribute_helper_method_without', attribute_helper_attribute_a::class]],
93
        ];
94
    }
95
 
96
    public function test_get_attributes_references(): void {
97
        $attributes = attribute_helper::from(new attribute_helper_example());
98
        $this->assertCount(3, $attributes);
99
 
100
        $attributes = attribute_helper::from(
101
            new attribute_helper_example(),
102
            attribute_helper_attribute_a::class,
103
        );
104
        $this->assertCount(2, $attributes);
105
 
106
        $attributes = attribute_helper::from(attribute_helper_enum::WITH_ATTRIBUTES);
107
        $this->assertCount(3, $attributes);
108
        $instances = attribute_helper::instances(attribute_helper_enum::WITH_ATTRIBUTES);
109
        $this->assertCount(3, $instances);
110
 
111
        $attributes = attribute_helper::from(
112
            attribute_helper_enum::WITH_ATTRIBUTES,
113
            attribute_helper_attribute_a::class,
114
        );
115
        $this->assertCount(2, $attributes);
116
        $instances = attribute_helper::instances(
117
            attribute_helper_enum::WITH_ATTRIBUTES,
118
            attribute_helper_attribute_a::class,
119
        );
120
        $this->assertCount(2, $instances);
121
        array_map(
122
            fn($instance) => $this->assertInstanceOf(attribute_helper_attribute_a::class, $instance),
123
            $instances,
124
        );
125
 
126
        // Singular fetches.
127
        $attribute = attribute_helper::one_from(
128
            attribute_helper_enum::WITH_ATTRIBUTES,
129
            attribute_helper_attribute_b::class,
130
        );
131
        $this->assertInstanceOf(\ReflectionAttribute::class, $attribute);
132
        $instance = attribute_helper::instance(
133
            attribute_helper_enum::WITH_ATTRIBUTES,
134
            attribute_helper_attribute_b::class,
135
        );
136
        $this->assertInstanceOf(attribute_helper_attribute_b::class, $instance);
137
    }
138
 
1441 ariadna 139
    public function test_get_attributes_not_valid(): void {
1 efrain 140
        $this->assertNull(attribute_helper::from(non_existent_class::class));
141
        $this->assertNull(attribute_helper::from([non_existent_class::class]));
142
        $this->assertNull(attribute_helper::from([attribute_helper_example::class, 'non_existent']));
143
        $this->assertNull(attribute_helper::from([non_existent_class::class, 'non_existent']));
144
        $this->assertNull(attribute_helper::instances(non_existent_class::class));
145
        $this->assertNull(attribute_helper::instances([non_existent_class::class]));
146
        $this->assertNull(attribute_helper::instances([attribute_helper_example::class, 'non_existent']));
147
        $this->assertNull(attribute_helper::instances([non_existent_class::class, 'non_existent']));
148
 
149
        // Test singular fetches.
150
        $this->assertNull(attribute_helper::one_from(non_existent_class::class));
151
        $this->assertNull(attribute_helper::one_from([non_existent_class::class]));
152
        $this->assertNull(attribute_helper::one_from([attribute_helper_example::class, 'non_existent']));
153
        $this->assertNull(attribute_helper::one_from([non_existent_class::class, 'non_existent']));
154
        $this->assertNull(attribute_helper::instance(non_existent_class::class));
155
        $this->assertNull(attribute_helper::instance([non_existent_class::class]));
156
        $this->assertNull(attribute_helper::instance([attribute_helper_example::class, 'non_existent']));
157
        $this->assertNull(attribute_helper::instance([non_existent_class::class, 'non_existent']));
158
    }
159
 
160
    public function test_get_attribute_too_many(): void {
161
        $this->expectException(\coding_exception::class);
162
        $this->expectExceptionMessage('More than one attribute found');
163
        attribute_helper::one_from(attribute_helper_example::class);
164
    }
165
 
166
    public function test_get_instance_too_many(): void {
167
        $this->expectException(\coding_exception::class);
168
        $this->expectExceptionMessage('More than one attribute found');
169
        attribute_helper::instance(attribute_helper_example::class);
170
    }
171
}