ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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
113 $this->defineGlobalConstants();
114
115 $this->prepareLocalDIC();
116
117 parent::setUp();
118 }
119
120 private function prepareLocalDIC(): void
121 {
122 (new ReflectionClass(\ILIAS\Test\TestDIC::class))
123 ->getProperty('dic')->setValue(null, null);
124 }
125
126 public static function callMethod($obj, $name, array $args = [])
127 {
128 return (new ReflectionClass($obj))->getMethod($name)->invokeArgs($obj, $args);
129 }
130
131 public static function getNonPublicPropertyValue(object $obj, string $name): mixed
132 {
133 $reflection_class = new ReflectionClass($obj);
134
135 while ($reflection_class !== false && !$reflection_class->hasProperty($name)) {
136 $reflection_class = $reflection_class->getParentClass();
137 }
138
139 return $reflection_class
140 ? $reflection_class->getProperty($name)->getValue($obj)
141 : throw new ReflectionException('Property not found.');
142 }
143
144 public function createInstanceOf(string $class_name, array $explicit_parameters = []): object
145 {
146 $constructor = (new ReflectionClass($class_name))->getConstructor();
147
148 if ($constructor === null) {
149 return new $class_name();
150 }
151
152 $parameters = [];
153
154 foreach ($constructor->getParameters() as $constructor_parameter) {
155 $constructor_parameter_name = $constructor_parameter->getName();
156
157 if (isset($explicit_parameters[$constructor_parameter_name])) {
158 $parameters[$constructor_parameter_name] = $explicit_parameters[$constructor_parameter_name];
159 continue;
160 }
161
162 if ($constructor_parameter->isDefaultValueAvailable()) {
163 $parameters[$constructor_parameter_name] = $constructor_parameter->getDefaultValue();
164 continue;
165 }
166
167 if (!$constructor_parameter->hasType()) {
168 throw new Exception('Constructor parameter has no type.');
169 }
170
171 $constructor_parameter_type_name = $constructor_parameter->getType()?->getName();
172 $parameters[$constructor_parameter_name] = match ($constructor_parameter_type_name) {
173 'string' => '',
174 'int' => 0,
175 'float' => 0.0,
176 'bool', 'true' => true,
177 'false' => false,
178 'array' => [],
179 'null', 'resource' => null,
180 'Closure' => (static fn() => null),
181 'object' => (object) [],
182 default => (function ($constructor_parameter_type_name) {
183 if (enum_exists($constructor_parameter_type_name)) {
184 $enum_cases = $constructor_parameter_type_name::cases();
185 return array_shift($enum_cases);
186 }
187
188 return $this->getOrCreateMock($constructor_parameter_type_name);
189 })($constructor_parameter_type_name)
190 };
191 }
192
193 return new $class_name(...$parameters);
194 }
195
196 public function createTraitInstanceOf(string $class_name, array $explicit_parameters = []): object
197 {
198 if (trait_exists($class_name)) {
199 $dynamic_class_name = self::DYNAMIC_CLASS . ++self::$DYNAMIC_CLASS_COUNT;
200 eval("class $dynamic_class_name{use $class_name;}");
201 return $this->createInstanceOf($dynamic_class_name, $explicit_parameters);
202 }
203
204 return $this->createInstanceOf($class_name, $explicit_parameters);
205 }
206
207 private function getOrCreateMock(string $parameter_type): PHPUnit\Framework\MockObject\MockObject
208 {
209 if (isset($this->services[$parameter_type])) {
210 global $DIC;
211 if (!isset($DIC[$this->services[$parameter_type]])) {
212 $DIC[$this->services[$parameter_type]] = $this->createMock($parameter_type);
213 }
214
215 return $DIC[$this->services[$parameter_type]];
216 }
217
218 return $this->createMock($parameter_type);
219 }
220}
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