ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilTestBaseTestCase.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once __DIR__ . '/ilTestBaseTestCaseTrait.php';
22 
25 
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 
62  $DIC = $this->backup_dic;
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();
80  $this->addGlobal_objDefinition();
81  $this->addGlobal_refinery();
82  $this->addGlobal_rbacsystem();
83  $this->addGlobal_rbacreview();
84  $this->addGlobal_ilRbacAdmin();
85  $this->addGlobal_http();
86  $this->addGlobal_fileDelivery();
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();
99  $this->addGlobal_ilObjDataCache();
100  $this->addGlobal_ilHelp();
101  $this->addGlobal_ilTabs();
102  $this->addGlobal_ilLocator();
103  $this->addGlobal_ilToolbar();
104  $this->addGlobal_filesystem();
105  $this->addGlobal_ilLoggerFactory();
108  $this->addGlobal_objectService();
109  $this->addGlobal_resourceStorage();
110  $this->addGlobal_objectMetadata();
111 
112  $this->defineGlobalConstants();
113 
114  $this->prepareLocalDIC();
115 
116  parent::setUp();
117  }
118 
119  private function prepareLocalDIC(): void
120  {
121  (new ReflectionClass(\ILIAS\Test\TestDIC::class))
122  ->getProperty('dic')->setValue(null, null);
123  }
124 
125  public static function callMethod($obj, $name, array $args = [])
126  {
127  return (new ReflectionClass($obj))->getMethod($name)->invokeArgs($obj, $args);
128  }
129 
130  public static function getNonPublicPropertyValue(object $obj, string $name): mixed
131  {
132  $reflection_class = new ReflectionClass($obj);
133 
134  while ($reflection_class !== false && !$reflection_class->hasProperty($name)) {
135  $reflection_class = $reflection_class->getParentClass();
136  }
137 
138  return $reflection_class
139  ? $reflection_class->getProperty($name)->getValue($obj)
140  : throw new ReflectionException('Property not found.');
141  }
142 
143  public function createInstanceOf(string $class_name, array $explicit_parameters = []): object
144  {
145  $constructor = (new ReflectionClass($class_name))->getConstructor();
146 
147  if ($constructor === null) {
148  return new $class_name();
149  }
150 
151  $parameters = [];
152 
153  foreach ($constructor->getParameters() as $constructor_parameter) {
154  $constructor_parameter_name = $constructor_parameter->getName();
155 
156  if (isset($explicit_parameters[$constructor_parameter_name])) {
157  $parameters[$constructor_parameter_name] = $explicit_parameters[$constructor_parameter_name];
158  continue;
159  }
160 
161  if ($constructor_parameter->isDefaultValueAvailable()) {
162  $parameters[$constructor_parameter_name] = $constructor_parameter->getDefaultValue();
163  continue;
164  }
165 
166  if (!$constructor_parameter->hasType()) {
167  throw new Exception('Constructor parameter has no type.');
168  }
169 
170  $constructor_parameter_type_name = $constructor_parameter->getType()?->getName();
171  $parameters[$constructor_parameter_name] = match ($constructor_parameter_type_name) {
172  'string' => '',
173  'int' => 0,
174  'float' => 0.0,
175  'bool', 'true' => true,
176  'false' => false,
177  'array' => [],
178  'null', 'resource' => null,
179  'Closure' => (static fn() => null),
180  'object' => (object) [],
181  default => (function ($constructor_parameter_type_name) {
182  if (enum_exists($constructor_parameter_type_name)) {
183  $enum_cases = $constructor_parameter_type_name::cases();
184  return array_shift($enum_cases);
185  }
186 
187  return $this->getOrCreateMock($constructor_parameter_type_name);
188  })($constructor_parameter_type_name)
189  };
190  }
191 
192  return new $class_name(...$parameters);
193  }
194 
195  public function createTraitInstanceOf(string $class_name, array $explicit_parameters = []): object
196  {
197  if (trait_exists($class_name)) {
198  $dynamic_class_name = self::DYNAMIC_CLASS . ++self::$DYNAMIC_CLASS_COUNT;
199  eval("class $dynamic_class_name{use $class_name;}");
200  return $this->createInstanceOf($dynamic_class_name, $explicit_parameters);
201  }
202 
203  return $this->createInstanceOf($class_name, $explicit_parameters);
204  }
205 
206  private function getOrCreateMock(string $parameter_type): PHPUnit\Framework\MockObject\MockObject
207  {
208  if (isset($this->services[$parameter_type])) {
209  global $DIC;
210  if (!isset($DIC[$this->services[$parameter_type]])) {
211  $DIC[$this->services[$parameter_type]] = $this->createMock($parameter_type);
212  }
213 
214  return $DIC[$this->services[$parameter_type]];
215  }
216 
217  return $this->createMock($parameter_type);
218  }
219 }
addGlobal_ilAppEventHandler()
addGlobal_ilComponentFactory()
createInstanceOf(string $class_name, array $explicit_parameters=[])
const string DYNAMIC_CLASS
static int $DYNAMIC_CLASS_COUNT
addGlobal_filesystem()
addGlobal_rbacsystem()
Interface Observer Contains several chained tasks and infos about them.
addGlobal_objectService()
static getNonPublicPropertyValue(object $obj, string $name)
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
addGlobal_ilRbacAdmin()
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
addGlobal_rbacreview()
addGlobal_fileDelivery()
addGlobal_dataFactory()
getOrCreateMock(string $parameter_type)
static callMethod($obj, $name, array $args=[])
global $DIC
Definition: shib_login.php:26
addGlobal_resourceStorage()
addGlobal_ilLoggerFactory()
defineGlobalConstants()
addGlobal_ilNavigationHistory()
createTraitInstanceOf(string $class_name, array $explicit_parameters=[])
addGlobal_static_url()
addGlobal_objDefinition()
addGlobal_ilObjDataCache()
addGlobal_ilComponentRepository()
addGlobal_uiRenderer()
trait ilTestBaseTestCaseTrait
addGlobal_GlobalScreenService()
const string MOCKED_METHOD_WITHOUT_OUTPUT
addGlobal_objectMetadata()