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');
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* @dataProvider get_attributes_provider
|
|
|
35 |
*/
|
|
|
36 |
public function test_get_attributes(
|
|
|
37 |
int $expectedcount,
|
|
|
38 |
array $args,
|
|
|
39 |
): void {
|
|
|
40 |
$attributes = attribute_helper::from(...$args);
|
|
|
41 |
$instances = attribute_helper::instances(...$args);
|
|
|
42 |
if ($expectedcount) {
|
|
|
43 |
$this->assertNotEmpty($attributes);
|
|
|
44 |
$this->assertCount($expectedcount, $attributes);
|
|
|
45 |
$this->assertCount($expectedcount, $instances);
|
|
|
46 |
|
|
|
47 |
} else {
|
|
|
48 |
$this->assertEmpty($attributes);
|
|
|
49 |
$this->assertEmpty($instances);
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public static function get_attributes_provider(): array {
|
|
|
54 |
return [
|
|
|
55 |
[3, [[attribute_helper_example::class]]],
|
|
|
56 |
[0, [[attribute_helper_example_without::class]]],
|
|
|
57 |
[3, [[attribute_helper_example::class, 'WITH_ATTRIBUTES']]],
|
|
|
58 |
[0, [[attribute_helper_example::class, 'WITHOUT_ATTRIBUTE']]],
|
|
|
59 |
[3, [[attribute_helper_example::class, 'withattributes']]],
|
|
|
60 |
[0, [[attribute_helper_example::class, 'withoutattributes']]],
|
|
|
61 |
[3, [[attribute_helper_example::class, 'with_attributes']]],
|
|
|
62 |
[0, [[attribute_helper_example::class, 'without_attributes']]],
|
|
|
63 |
[3, [[attribute_helper_enum::class, 'WITH_ATTRIBUTES']]],
|
|
|
64 |
[0, [[attribute_helper_enum::class, 'WITHOUT_ATTRIBUTE']]],
|
|
|
65 |
[3, [__NAMESPACE__ . '\\attribute_helper_method_with']],
|
|
|
66 |
[0, [__NAMESPACE__ . '\\attribute_helper_method_without']],
|
|
|
67 |
[0, [__NAMESPACE__ . '\\function_not_exists']],
|
|
|
68 |
|
|
|
69 |
[3, [attribute_helper_example::class]],
|
|
|
70 |
[0, [attribute_helper_example_without::class]],
|
|
|
71 |
[3, [attribute_helper_example::class . '::WITH_ATTRIBUTES']],
|
|
|
72 |
[0, [attribute_helper_example::class . '::WITHOUT_ATTRIBUTE']],
|
|
|
73 |
[3, [attribute_helper_example::class . '::withattributes']],
|
|
|
74 |
[0, [attribute_helper_example::class . '::withoutattributes']],
|
|
|
75 |
[3, [attribute_helper_example::class . '::with_attributes']],
|
|
|
76 |
[0, [attribute_helper_example::class . '::without_attributes']],
|
|
|
77 |
[3, [attribute_helper_enum::class . '::WITH_ATTRIBUTES']],
|
|
|
78 |
[0, [attribute_helper_enum::class . '::WITHOUT_ATTRIBUTE']],
|
|
|
79 |
|
|
|
80 |
[2, [[attribute_helper_example::class], attribute_helper_attribute_a::class]],
|
|
|
81 |
[0, [[attribute_helper_example_without::class], attribute_helper_attribute_a::class]],
|
|
|
82 |
[2, [[attribute_helper_example::class, 'WITH_ATTRIBUTES'], attribute_helper_attribute_a::class]],
|
|
|
83 |
[0, [[attribute_helper_example::class, 'WITHOUT_ATTRIBUTE'], attribute_helper_attribute_a::class]],
|
|
|
84 |
[2, [[attribute_helper_example::class, 'withattributes'], attribute_helper_attribute_a::class]],
|
|
|
85 |
[0, [[attribute_helper_example::class, 'withoutattributes'], attribute_helper_attribute_a::class]],
|
|
|
86 |
[2, [[attribute_helper_example::class, 'with_attributes'], attribute_helper_attribute_a::class]],
|
|
|
87 |
[0, [[attribute_helper_example::class, 'without_attributes'], attribute_helper_attribute_a::class]],
|
|
|
88 |
[2, [[attribute_helper_enum::class, 'WITH_ATTRIBUTES'], attribute_helper_attribute_a::class]],
|
|
|
89 |
[0, [[attribute_helper_enum::class, 'WITHOUT_ATTRIBUTE'], attribute_helper_attribute_a::class]],
|
|
|
90 |
[2, [__NAMESPACE__ . '\\attribute_helper_method_with', attribute_helper_attribute_a::class]],
|
|
|
91 |
[0, [__NAMESPACE__ . '\\attribute_helper_method_without', attribute_helper_attribute_a::class]],
|
|
|
92 |
];
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public function test_get_attributes_references(): void {
|
|
|
96 |
$attributes = attribute_helper::from(new attribute_helper_example());
|
|
|
97 |
$this->assertCount(3, $attributes);
|
|
|
98 |
|
|
|
99 |
$attributes = attribute_helper::from(
|
|
|
100 |
new attribute_helper_example(),
|
|
|
101 |
attribute_helper_attribute_a::class,
|
|
|
102 |
);
|
|
|
103 |
$this->assertCount(2, $attributes);
|
|
|
104 |
|
|
|
105 |
$attributes = attribute_helper::from(attribute_helper_enum::WITH_ATTRIBUTES);
|
|
|
106 |
$this->assertCount(3, $attributes);
|
|
|
107 |
$instances = attribute_helper::instances(attribute_helper_enum::WITH_ATTRIBUTES);
|
|
|
108 |
$this->assertCount(3, $instances);
|
|
|
109 |
|
|
|
110 |
$attributes = attribute_helper::from(
|
|
|
111 |
attribute_helper_enum::WITH_ATTRIBUTES,
|
|
|
112 |
attribute_helper_attribute_a::class,
|
|
|
113 |
);
|
|
|
114 |
$this->assertCount(2, $attributes);
|
|
|
115 |
$instances = attribute_helper::instances(
|
|
|
116 |
attribute_helper_enum::WITH_ATTRIBUTES,
|
|
|
117 |
attribute_helper_attribute_a::class,
|
|
|
118 |
);
|
|
|
119 |
$this->assertCount(2, $instances);
|
|
|
120 |
array_map(
|
|
|
121 |
fn($instance) => $this->assertInstanceOf(attribute_helper_attribute_a::class, $instance),
|
|
|
122 |
$instances,
|
|
|
123 |
);
|
|
|
124 |
|
|
|
125 |
// Singular fetches.
|
|
|
126 |
$attribute = attribute_helper::one_from(
|
|
|
127 |
attribute_helper_enum::WITH_ATTRIBUTES,
|
|
|
128 |
attribute_helper_attribute_b::class,
|
|
|
129 |
);
|
|
|
130 |
$this->assertInstanceOf(\ReflectionAttribute::class, $attribute);
|
|
|
131 |
$instance = attribute_helper::instance(
|
|
|
132 |
attribute_helper_enum::WITH_ATTRIBUTES,
|
|
|
133 |
attribute_helper_attribute_b::class,
|
|
|
134 |
);
|
|
|
135 |
$this->assertInstanceOf(attribute_helper_attribute_b::class, $instance);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public function test_get_attributes_invalid(): void {
|
|
|
139 |
$this->assertNull(attribute_helper::from(non_existent_class::class));
|
|
|
140 |
$this->assertNull(attribute_helper::from([non_existent_class::class]));
|
|
|
141 |
$this->assertNull(attribute_helper::from([attribute_helper_example::class, 'non_existent']));
|
|
|
142 |
$this->assertNull(attribute_helper::from([non_existent_class::class, 'non_existent']));
|
|
|
143 |
$this->assertNull(attribute_helper::instances(non_existent_class::class));
|
|
|
144 |
$this->assertNull(attribute_helper::instances([non_existent_class::class]));
|
|
|
145 |
$this->assertNull(attribute_helper::instances([attribute_helper_example::class, 'non_existent']));
|
|
|
146 |
$this->assertNull(attribute_helper::instances([non_existent_class::class, 'non_existent']));
|
|
|
147 |
|
|
|
148 |
// Test singular fetches.
|
|
|
149 |
$this->assertNull(attribute_helper::one_from(non_existent_class::class));
|
|
|
150 |
$this->assertNull(attribute_helper::one_from([non_existent_class::class]));
|
|
|
151 |
$this->assertNull(attribute_helper::one_from([attribute_helper_example::class, 'non_existent']));
|
|
|
152 |
$this->assertNull(attribute_helper::one_from([non_existent_class::class, 'non_existent']));
|
|
|
153 |
$this->assertNull(attribute_helper::instance(non_existent_class::class));
|
|
|
154 |
$this->assertNull(attribute_helper::instance([non_existent_class::class]));
|
|
|
155 |
$this->assertNull(attribute_helper::instance([attribute_helper_example::class, 'non_existent']));
|
|
|
156 |
$this->assertNull(attribute_helper::instance([non_existent_class::class, 'non_existent']));
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
public function test_get_attribute_too_many(): void {
|
|
|
160 |
$this->expectException(\coding_exception::class);
|
|
|
161 |
$this->expectExceptionMessage('More than one attribute found');
|
|
|
162 |
attribute_helper::one_from(attribute_helper_example::class);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
public function test_get_instance_too_many(): void {
|
|
|
166 |
$this->expectException(\coding_exception::class);
|
|
|
167 |
$this->expectExceptionMessage('More than one attribute found');
|
|
|
168 |
attribute_helper::instance(attribute_helper_example::class);
|
|
|
169 |
}
|
|
|
170 |
}
|