ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ClassFinderTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Tests\Setup;
22
24use PHPUnit\Framework\TestCase;
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
49{
50}
51
53{
54}
55
57{
58}
59
60#[\Attribute(\Attribute::TARGET_CLASS)]
62{
63}
64
65#[\Attribute(\Attribute::TARGET_CLASS)]
67{
68}
69
70#[\Attribute(\Attribute::TARGET_CLASS)]
72{
73}
74
75#[TestAttribute1()]
76class TestClass1 implements TestInterface1
77{
78}
79
80#[TestAttribute2()]
81class TestClass2 implements TestInterface2
82{
83}
84
85#[TestAttribute1()]
86#[TestAttribute2()]
88{
89}
90
91class ClassFinderTest extends TestCase
92{
93 public function testClassImplementsInterface(): void
94 {
95 $finder = new ImplementationOfInterfaceFinder();
96 $this->assertTrue($finder->isClassMatching(TestInterface1::class, new \ReflectionClass(TestClass1::class)));
97 $this->assertTrue($finder->isClassMatching(TestInterface2::class, new \ReflectionClass(TestClass2::class)));
98 $this->assertTrue($finder->isClassMatching(TestInterface1::class, new \ReflectionClass(TestClass3::class)));
99 $this->assertTrue($finder->isClassMatching(TestInterface2::class, new \ReflectionClass(TestClass3::class)));
100
101 $this->assertFalse($finder->isClassMatching(TestInterface3::class, new \ReflectionClass(TestClass3::class)));
102 $this->assertFalse($finder->isClassMatching(TestInterface3::class, new \ReflectionClass(TestClass2::class)));
103 $this->assertFalse($finder->isClassMatching(TestInterface3::class, new \ReflectionClass(TestClass1::class)));
104 }
105
106 public function testClassUsesAttribute(): void
107 {
108 $finder = new UsageOfAttributeFinder();
109 $this->assertTrue($finder->isClassMatching(TestAttribute1::class, new \ReflectionClass(TestClass1::class)));
110 $this->assertTrue($finder->isClassMatching(TestAttribute2::class, new \ReflectionClass(TestClass2::class)));
111 $this->assertTrue($finder->isClassMatching(TestAttribute1::class, new \ReflectionClass(TestClass3::class)));
112 $this->assertTrue($finder->isClassMatching(TestAttribute2::class, new \ReflectionClass(TestClass3::class)));
113
114 $this->assertFalse($finder->isClassMatching(TestAttribute3::class, new \ReflectionClass(TestClass3::class)));
115 $this->assertFalse($finder->isClassMatching(TestAttribute3::class, new \ReflectionClass(TestClass2::class)));
116 $this->assertFalse($finder->isClassMatching(TestAttribute3::class, new \ReflectionClass(TestClass1::class)));
117 }
118
119 public function testGenericClassMatching(): void
120 {
121 $finder = new TestAbstractOfFinder();
122 $finder->class_names = [
123 TestClass1::class,
124 TestClass2::class,
125 TestClass3::class
126 ];
127
128 $all_true = fn (\ReflectionClass $r): bool => true;
129 $expected = [
130 TestClass1::class,
131 TestClass2::class,
132 TestClass3::class
133 ];
134 $this->assertEquals($expected, iterator_to_array($finder->getMatchingClassNames($all_true)));
135
136 $all_false = fn (\ReflectionClass $r): bool => false;
137 $expected = [];
138 $this->assertEquals($expected, iterator_to_array($finder->getMatchingClassNames($all_false)));
139
140 $only_class1 = fn (\ReflectionClass $r): bool => $r->getName() === TestClass1::class;
141 $expected = [
142 TestClass1::class
143 ];
144 $this->assertEquals($expected, iterator_to_array($finder->getMatchingClassNames($only_class1)));
145 }
146}
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...
getAllClassNames(array $additional_ignore, ?string $matching_path=null)