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