ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
ilTestBaseTestCase.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once __DIR__ . '/ilTestBaseTestCaseTrait.php';
22
23use PHPUnit\Framework\TestCase;
25
30class ilTestBaseTestCase extends TestCase
31{
33
34 public const string MOCKED_METHOD_WITHOUT_OUTPUT = 'MOCKED_METHOD_WITHOUT_OUTPUT';
35 public const string DYNAMIC_CLASS = 'DynamicClass';
36 protected static int $DYNAMIC_CLASS_COUNT = 0;
37 protected ?Container $dic = null;
38 protected ?Container $backup_dic = null;
39
43 protected function setUp(): void
44 {
45 error_reporting(E_ALL);
46 $this->defineGlobalConstants();
47
48 global $DIC;
49 $this->backup_dic = is_object($DIC) ? clone $DIC : $DIC;
50
51 $DIC = $this->getMockBuilder(Container::class)->onlyMethods([])->getMock();
52 $this->addGlobals();
53 $this->dic = $DIC;
54
55 parent::setUp();
56 }
57
58 protected function tearDown(): void
59 {
60 global $DIC;
61
63
64 parent::tearDown();
65 }
66
67 private function addGlobals(): void
68 {
69 $this->addGlobal_ilAccess();
70 $this->addGlobal_dataFactory();
71 $this->addGlobal_tpl();
72 $this->addGlobal_ilDB();
73 $this->addGlobal_ilUser();
74 $this->addGlobal_ilias();
75 $this->addGlobal_ilErr();
76 $this->addGlobal_tree();
77 $this->addGlobal_lng();
81 $this->addGlobal_refinery();
82 $this->addGlobal_rbacsystem();
83 $this->addGlobal_rbacreview();
84 $this->addGlobal_ilRbacAdmin();
85 $this->addGlobal_http();
89 $this->addGlobal_uiFactory();
90 $this->addGlobal_uiRenderer();
91 $this->addGlobal_uiService();
92 $this->addGlobal_static_url();
93 $this->addGlobal_upload();
94 $this->addGlobal_ilLog();
95 $this->addGlobal_ilBench();
96 $this->addGlobal_ilSetting();
97 $this->addGlobal_ilCtrl();
98 $this->addGlobal_ilCtrl();
100 $this->addGlobal_ilHelp();
101 $this->addGlobal_ilTabs();
102 $this->addGlobal_ilLocator();
103 $this->addGlobal_ilToolbar();
104 $this->addGlobal_filesystem();
111 $this->addGlobal_user();
112 $this->addGlobal_mail();
113
114 $this->defineGlobalConstants();
115
116 $this->prepareLocalDIC();
117
118 parent::setUp();
119 }
120
121 private function prepareLocalDIC(): void
122 {
123 (new ReflectionClass(\ILIAS\Test\TestDIC::class))
124 ->getProperty('dic')->setValue(null, null);
125 }
126
127 public static function callMethod($obj, $name, array $args = [])
128 {
129 return (new ReflectionClass($obj))->getMethod($name)->invokeArgs($obj, $args);
130 }
131
132 public static function getNonPublicPropertyValue(object $obj, string $name): mixed
133 {
134 $reflection_class = new ReflectionClass($obj);
135
136 while ($reflection_class !== false && !$reflection_class->hasProperty($name)) {
137 $reflection_class = $reflection_class->getParentClass();
138 }
139
140 return $reflection_class
141 ? $reflection_class->getProperty($name)->getValue($obj)
142 : throw new ReflectionException('Property not found.');
143 }
144
145 public function createInstanceOf(string $class_name, array $explicit_parameters = []): object
146 {
147 $constructor = (new ReflectionClass($class_name))->getConstructor();
148
149 if ($constructor === null) {
150 return new $class_name();
151 }
152
153 $parameters = [];
154
155 foreach ($constructor->getParameters() as $constructor_parameter) {
156 $constructor_parameter_name = $constructor_parameter->getName();
157
158 if (isset($explicit_parameters[$constructor_parameter_name])) {
159 $parameters[$constructor_parameter_name] = $explicit_parameters[$constructor_parameter_name];
160 continue;
161 }
162
163 if ($constructor_parameter->isDefaultValueAvailable()) {
164 $parameters[$constructor_parameter_name] = $constructor_parameter->getDefaultValue();
165 continue;
166 }
167
168 if (!$constructor_parameter->hasType()) {
169 throw new Exception('Constructor parameter has no type.');
170 }
171
172 $constructor_parameter_type_name = $constructor_parameter->getType()?->getName();
173 $parameters[$constructor_parameter_name] = match ($constructor_parameter_type_name) {
174 'string' => '',
175 'int' => 0,
176 'float' => 0.0,
177 'bool', 'true' => true,
178 'false' => false,
179 'array' => [],
180 'null', 'resource' => null,
181 'Closure' => (static fn() => null),
182 'object' => (object) [],
183 default => (function ($constructor_parameter_type_name) {
184 if (enum_exists($constructor_parameter_type_name)) {
185 $enum_cases = $constructor_parameter_type_name::cases();
186 return array_shift($enum_cases);
187 }
188
189 return $this->getOrCreateMock($constructor_parameter_type_name);
190 })($constructor_parameter_type_name)
191 };
192 }
193
194 return new $class_name(...$parameters);
195 }
196
197 public function createTraitInstanceOf(string $class_name, array $explicit_parameters = []): object
198 {
199 if (trait_exists($class_name)) {
200 $dynamic_class_name = self::DYNAMIC_CLASS . ++self::$DYNAMIC_CLASS_COUNT;
201 eval("class $dynamic_class_name{use $class_name;}");
202 return $this->createInstanceOf($dynamic_class_name, $explicit_parameters);
203 }
204
205 return $this->createInstanceOf($class_name, $explicit_parameters);
206 }
207
208 private function getOrCreateMock(string $parameter_type): PHPUnit\Framework\MockObject\MockObject
209 {
210 if (isset($this->services[$parameter_type])) {
211 global $DIC;
212 if (!isset($DIC[$this->services[$parameter_type]])) {
213 $DIC[$this->services[$parameter_type]] = $this->createMock($parameter_type);
214 }
215
216 return $DIC[$this->services[$parameter_type]];
217 }
218
219 return $this->createMock($parameter_type);
220 }
221}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Class ilTestBaseClass.
createTraitInstanceOf(string $class_name, array $explicit_parameters=[])
const string DYNAMIC_CLASS
static int $DYNAMIC_CLASS_COUNT
createInstanceOf(string $class_name, array $explicit_parameters=[])
static getNonPublicPropertyValue(object $obj, string $name)
getOrCreateMock(string $parameter_type)
const string MOCKED_METHOD_WITHOUT_OUTPUT
static callMethod($obj, $name, array $args=[])
addGlobal_static_url()
addGlobal_filesystem()
addGlobal_objDefinition()
addGlobal_ilAppEventHandler()
addGlobal_ilNavigationHistory()
addGlobal_ilRbacAdmin()
addGlobal_ilComponentFactory()
addGlobal_GlobalScreenService()
addGlobal_dataFactory()
addGlobal_objectMetadata()
addGlobal_fileDelivery()
addGlobal_ilObjDataCache()
addGlobal_resourceStorage()
addGlobal_ilComponentRepository()
defineGlobalConstants()
addGlobal_ilLoggerFactory()
addGlobal_uiRenderer()
addGlobal_rbacsystem()
trait ilTestBaseTestCaseTrait
addGlobal_rbacreview()
addGlobal_objectService()
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26