ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilCertificateBaseTestCase.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\TestCase;
23
24abstract class ilCertificateBaseTestCase extends TestCase
25{
26 protected ?Container $dic;
27
28 protected function setUp(): void
29 {
30 if (!defined('ANONYMOUS_USER_ID')) {
31 define('ANONYMOUS_USER_ID', 13);
32 }
33
34 global $DIC;
35
36 $this->dic = is_object($DIC) ? clone $DIC : $DIC;
37
38 $DIC = new Container();
39
40 parent::setUp();
41 }
42
43 protected function tearDown(): void
44 {
45 global $DIC;
46
48
49 parent::tearDown();
50 }
51
55 protected function assertDoesNotThrow(callable $cb, string $message = ''): void
56 {
57 try {
58 $cb();
59 $this->addToAssertionCount(1);
60 } catch (Throwable $e) {
61 $this->fail(
62 trim($message . ' ' . sprintf(
63 '(unexpected %s: %s)' . PHP_EOL . '%s',
64 get_class($e),
65 $e->getMessage(),
66 $e->getTraceAsString()
67 ))
68 );
69 }
70 }
71
78 protected function assertThrows(
79 callable $cb,
80 ?string $expected_class = null,
81 ?string $expected_message = null
82 ): void {
83 try {
84 $cb();
85 $this->fail(sprintf(
86 'Failed asserting that exception %s was thrown.',
87 $expected_class ?? '(any exception)'
88 ));
89 } catch (Throwable $e) {
90 if ($expected_class !== null && !$e instanceof $expected_class) {
91 $this->fail(sprintf(
92 'Failed asserting exception of type %s. Got %s instead.',
93 $expected_class,
94 get_class($e)
95 ));
96 }
97 if ($expected_message !== null && !str_contains($e->getMessage(), $expected_message)) {
98 $this->fail(sprintf(
99 'Failed asserting exception message contains "%s". Actual message: "%s"',
100 $expected_message,
101 $e->getMessage()
102 ));
103 }
104 $this->addToAssertionCount(1);
105 }
106 }
107
108 protected function setGlobalVariable(string $name, mixed $value): void
109 {
110 global $DIC;
111
112 $GLOBALS[$name] = $value;
113
114 unset($DIC[$name]);
115 $DIC[$name] = static function (Container $c) use ($name) {
116 return $GLOBALS[$name];
117 };
118 }
119}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
assertThrows(callable $cb, ?string $expected_class=null, ?string $expected_message=null)
@template T of Throwable
assertDoesNotThrow(callable $cb, string $message='')
setGlobalVariable(string $name, mixed $value)
$c
Definition: deliver.php:25
try
handle Lrs Init
Definition: xapiproxy.php:86
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54
$message
Definition: xapiexit.php:31