ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilDatabaseUpdateStepsExecutedObjectiveTest.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\TestCase;
22
24{
25 public array $called = [];
26
27 protected ?ilDBInterface $db = null;
28
29 public function prepare(ilDBInterface $db): void
30 {
31 $this->db = $db;
32 }
33
34
35 public function step_1(): void
36 {
37 $this->called[] = 1;
38 // Call some function on the interface to check if this step
39 // is really called.
40 $this->db->connect();
41 }
42
43 // 4 comes before 2 to check if the class gets the sorting right
44 public function step_4(): void
45 {
46 $this->called[] = 4;
47 // Call some function on the interface to check if this step
48 // is really called.
49 $this->db->connect();
50 }
51
52 public function step_2(): void
53 {
54 $this->called[] = 2;
55 // Call some function on the interface to check if this step
56 // is really called.
57 $this->db->connect();
58 }
59}
60
62{
65
66 protected function setUp(): void
67 {
68 $this->steps = new Test_ilDatabaseUpdateSteps();
69 $this->objective = new ilDatabaseUpdateStepsExecutedObjective($this->steps);
70 }
71
72 public function testCorrectExecutionOrder(): void
73 {
74 $execution_log = new class () implements ilDatabaseUpdateStepExecutionLog {
75 public function started(string $class, int $step): void
76 {
77 }
78 public function finished(string $class, int $step): void
79 {
80 }
81 public function getLastStartedStep(string $class): int
82 {
83 return 0;
84 }
85 public function getLastFinishedStep(string $class): int
86 {
87 return 0;
88 }
89 };
90 $steps_reader = new class () extends ilDBStepReader {
91 };
92 $db = $this->createMock(ilDBInterface::class);
93 $env = new ArrayEnvironment([
94 ilDBStepReader::class => $steps_reader,
95 ilDatabaseUpdateStepExecutionLog::class => $execution_log,
96 Environment::RESOURCE_DATABASE => $db
97 ]);
98
99 $db->expects($this->exactly(3))
100 ->method("connect");
101
102 $this->objective->achieve($env);
103
104 $this->assertSame([1,2,4], $this->steps->called);
105 }
106
107 public function testUsesExecutionLock(): void
108 {
109 $execution_log = new class ($this) implements ilDatabaseUpdateStepExecutionLog {
110 public function __construct(protected ilDatabaseUpdateStepsExecutedObjectiveTest $test)
111 {
112 }
113 public function started(string $class, int $step): void
114 {
115 $this->test->steps->called[] = ["started", $class, $step];
116 }
117 public function finished(string $class, int $step): void
118 {
119 $this->test->steps->called[] = ["finished", $class, $step];
120 }
121 public function getLastStartedStep(string $class): int
122 {
123 return 0;
124 }
125 public function getLastFinishedStep(string $class): int
126 {
127 return 0;
128 }
129 };
130 $steps_reader = new class () extends ilDBStepReader {
131 };
132 $db = $this->createMock(ilDBInterface::class);
133 $env = new ArrayEnvironment([
134 ilDBStepReader::class => $steps_reader,
135 ilDatabaseUpdateStepExecutionLog::class => $execution_log,
136 Environment::RESOURCE_DATABASE => $db
137 ]);
138
139 $this->objective->achieve($env);
140
141 $expected = [
142 ["started", Test_ilDatabaseUpdateSteps::class, 1],
143 1,
144 ["finished", Test_ilDatabaseUpdateSteps::class, 1],
145 ["started", Test_ilDatabaseUpdateSteps::class, 2],
146 2,
147 ["finished", Test_ilDatabaseUpdateSteps::class, 2],
148 ["started", Test_ilDatabaseUpdateSteps::class, 4],
149 4,
150 ["finished", Test_ilDatabaseUpdateSteps::class, 4]
151 ];
152
153 $this->assertSame($expected, $this->steps->called);
154 }
155
156 public function testOnlyExecuteNonExecutedSteps(): void
157 {
158 $execution_log = new class () implements ilDatabaseUpdateStepExecutionLog {
159 public function started(string $class, int $step): void
160 {
161 }
162 public function finished(string $class, int $step): void
163 {
164 }
165 public function getLastStartedStep(string $class): int
166 {
167 return 1;
168 }
169 public function getLastFinishedStep(string $class): int
170 {
171 return 1;
172 }
173 };
174 $steps_reader = new class () extends ilDBStepReader {
175 };
176 $db = $this->createMock(ilDBInterface::class);
177 $env = new ArrayEnvironment([
178 ilDBStepReader::class => $steps_reader,
179 ilDatabaseUpdateStepExecutionLog::class => $execution_log,
180 Environment::RESOURCE_DATABASE => $db
181 ]);
182
183 $db->expects($this->exactly(2))
184 ->method("connect");
185
186 $this->objective->achieve($env);
187
188 $this->assertSame([2,4], $this->steps->called);
189 }
190
192 {
193 $this->expectException(RuntimeException::class);
194
195 $execution_log = new class () implements ilDatabaseUpdateStepExecutionLog {
196 public function started(string $class, int $step): void
197 {
198 }
199 public function finished(string $class, int $step): void
200 {
201 }
202 public function getLastStartedStep(string $class): int
203 {
204 return 2;
205 }
206 public function getLastFinishedStep(string $class): int
207 {
208 return 1;
209 }
210 };
211 $db = $this->createMock(ilDBInterface::class);
212 $env = new ArrayEnvironment([
213 ilDatabaseUpdateStepExecutionLog::class => $execution_log,
214 Environment::RESOURCE_DATABASE => $db
215 ]);
216 $this->objective->achieve($env);
217 }
218}
This class attempt to achieve a set of database update steps.
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
Interface ilDBInterface.
This logs the execution of database update steps.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc