ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ClassFinderTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Tests\Setup;
22 
27 
29 {
30  public array $class_names = [];
31 
32  protected function getAllClassNames(array $additional_ignore, string $matching_path = null): \Iterator
33  {
34  foreach ($this->class_names as $class_name) {
35  yield $class_name;
36  }
37  }
38 
42  public function getMatchingClassNames(callable $matcher): \Iterator
43  {
44  yield from $this->genericGetMatchingClassNames($matcher, [], null);
45  }
46 }
47 
48 interface TestInterface1
49 {
50 }
51 
68 interface TestInterface2
69 {
70 }
71 
88 interface TestInterface3
89 {
90 }
91 
108 #[\Attribute(\Attribute::TARGET_CLASS)]
110 {
111 }
112 
113 #[\Attribute(\Attribute::TARGET_CLASS)]
115 {
116 }
117 
118 #[\Attribute(\Attribute::TARGET_CLASS)]
120 {
121 }
122 
123 #[TestAttribute1()]
124 class TestClass1 implements TestInterface1
125 {
126 }
127 
128 #[TestAttribute2()]
129 class TestClass2 implements TestInterface2
130 {
131 }
132 
133 #[TestAttribute1()]
134 #[TestAttribute2()]
136 {
137 }
138 
139 class ClassFinderTest extends TestCase
140 {
141  public function testClassImplementsInterface(): void
142  {
143  $finder = new ImplementationOfInterfaceFinder();
144  $this->assertTrue($finder->isClassMatching(TestInterface1::class, new \ReflectionClass(TestClass1::class)));
145  $this->assertTrue($finder->isClassMatching(TestInterface2::class, new \ReflectionClass(TestClass2::class)));
146  $this->assertTrue($finder->isClassMatching(TestInterface1::class, new \ReflectionClass(TestClass3::class)));
147  $this->assertTrue($finder->isClassMatching(TestInterface2::class, new \ReflectionClass(TestClass3::class)));
148 
149  $this->assertFalse($finder->isClassMatching(TestInterface3::class, new \ReflectionClass(TestClass3::class)));
150  $this->assertFalse($finder->isClassMatching(TestInterface3::class, new \ReflectionClass(TestClass2::class)));
151  $this->assertFalse($finder->isClassMatching(TestInterface3::class, new \ReflectionClass(TestClass1::class)));
152  }
153 
154  public function testClassUsesAttribute(): void
155  {
156  $finder = new UsageOfAttributeFinder();
157  $this->assertTrue($finder->isClassMatching(TestAttribute1::class, new \ReflectionClass(TestClass1::class)));
158  $this->assertTrue($finder->isClassMatching(TestAttribute2::class, new \ReflectionClass(TestClass2::class)));
159  $this->assertTrue($finder->isClassMatching(TestAttribute1::class, new \ReflectionClass(TestClass3::class)));
160  $this->assertTrue($finder->isClassMatching(TestAttribute2::class, new \ReflectionClass(TestClass3::class)));
161 
162  $this->assertFalse($finder->isClassMatching(TestAttribute3::class, new \ReflectionClass(TestClass3::class)));
163  $this->assertFalse($finder->isClassMatching(TestAttribute3::class, new \ReflectionClass(TestClass2::class)));
164  $this->assertFalse($finder->isClassMatching(TestAttribute3::class, new \ReflectionClass(TestClass1::class)));
165  }
166 
167  public function testGenericClassMatching(): void
168  {
169  $finder = new TestAbstractOfFinder();
170  $finder->class_names = [
171  TestClass1::class,
172  TestClass2::class,
173  TestClass3::class
174  ];
175 
176  $all_true = fn (\ReflectionClass $r): bool => true;
177  $expected = [
178  TestClass1::class,
179  TestClass2::class,
180  TestClass3::class
181  ];
182  $this->assertEquals($expected, iterator_to_array($finder->getMatchingClassNames($all_true)));
183 
184  $all_false = fn (\ReflectionClass $r): bool => false;
185  $expected = [];
186  $this->assertEquals($expected, iterator_to_array($finder->getMatchingClassNames($all_false)));
187 
188  $only_class1 = fn (\ReflectionClass $r): bool => $r->getName() === TestClass1::class;
189  $expected = [
190  TestClass1::class
191  ];
192  $this->assertEquals($expected, iterator_to_array($finder->getMatchingClassNames($only_class1)));
193  }
194 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getAllClassNames(array $additional_ignore, string $matching_path=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
genericGetMatchingClassNames(callable $is_matching, array $additional_ignore=[], string $matching_path=null)
The matcher finds the class names implementing the given interface, while ignoring paths in self::$ig...
$r
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...