ILIAS  release_7 Revision v7.30-3-g800a261c036
ilDatabaseUpdateStepsTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5use PHPUnit\Framework\TestCase;
6
10
12{
13 public $called = [];
14
15 public $step_2_precondition = null;
16
17 public function step_1(\ilDBInterface $db)
18 {
19 $this->called[] = 1;
20 // Call some function on the interface to check if this step
21 // is really called.
22 $db->connect();
23 }
24
25 // 4 comes before 2 to check if the class gets the sorting right
26 public function step_4(\ilDBInterface $db)
27 {
28 $this->called[] = 4;
29 // Call some function on the interface to check if this step
30 // is really called.
31 $db->connect();
32 }
33
34 public function step_2(\ilDBInterface $db)
35 {
36 $this->called[] = 2;
37 // Call some function on the interface to check if this step
38 // is really called.
39 $db->connect();
40 }
41
42 public function getAdditionalPreconditionsForStep(int $num) : array
43 {
44 if ($this->step_2_precondition && $num === 2) {
46 }
47 return [];
48 }
49
50 public function _getSteps() : array
51 {
52 return $this->getSteps();
53 }
54}
55
56class ilDatabaseUpdateStepsTest extends TestCase
57{
58 protected function setUp() : void
59 {
60 $this->base = $this->createMock(Objective::class);
61
62 $this->test1 = new Test_ilDatabaseUpdateSteps($this->base);
63 }
64
65 public function testGetStep1()
66 {
67 $env = $this->createMock(Environment::class);
68
69 $step1 = $this->test1->getStep(1);
70
71 $this->assertInstanceOf(ilDatabaseUpdateStep::class, $step1);
72 $this->assertEquals(
73 hash("sha256", Test_ilDatabaseUpdateSteps::class . "::step_1"),
74 $step1->getHash()
75 );
76
77 $preconditions = $step1->getPreconditions($env);
78
79 $this->assertCount(1, $preconditions);
80 $this->assertSame($this->base, $preconditions[0]);
81 }
82
83 public function testGetStep2()
84 {
85 $env = $this->createMock(Environment::class);
86
87 $step1 = $this->test1->getStep(1);
88 $step2 = $this->test1->getStep(2);
89
90 $this->assertInstanceOf(ilDatabaseUpdateStep::class, $step2);
91 $this->assertEquals(
92 hash("sha256", Test_ilDatabaseUpdateSteps::class . "::step_2"),
93 $step2->getHash()
94 );
95
96 $preconditions = $step2->getPreconditions($env);
97
98 $this->assertCount(1, $preconditions);
99 $this->assertEquals($step1->getHash(), $preconditions[0]->getHash());
100 }
101
103 {
104 $env = $this->createMock(Environment::class);
105
106 $this->test1->step_2_precondition = new ILIAS\Setup\Objective\NullObjective;
107
108 $step1 = $this->test1->getStep(1);
109 $step2 = $this->test1->getStep(2);
110
111 $preconditions = $step2->getPreconditions($env);
112
113 $this->assertCount(2, $preconditions);
114 $this->assertEquals($step1->getHash(), $preconditions[0]->getHash());
115 $this->assertEquals($this->test1->step_2_precondition, $preconditions[1]);
116 }
117
118 public function testGetStep4Finished2()
119 {
120 $env = $this->createMock(Environment::class);
121
122 $step4 = $this->test1->getStep(4, 2);
123
124 $this->assertInstanceOf(ilDatabaseUpdateStep::class, $step4);
125 $this->assertEquals(
126 hash("sha256", Test_ilDatabaseUpdateSteps::class . "::step_4"),
127 $step4->getHash()
128 );
129
130 $preconditions = $step4->getPreconditions($env);
131
132 $this->assertCount(1, $preconditions);
133 $this->assertEquals($this->base, $preconditions[0]);
134 }
135
136 public function testGetStep4Finished1()
137 {
138 $env = $this->createMock(Environment::class);
139
140 $step4 = $this->test1->getStep(4, 1);
141 $step2 = $this->test1->getStep(2, 1);
142
143 $this->assertInstanceOf(ilDatabaseUpdateStep::class, $step4);
144 $this->assertEquals(
145 hash("sha256", Test_ilDatabaseUpdateSteps::class . "::step_4"),
146 $step4->getHash()
147 );
148
149 $preconditions = $step4->getPreconditions($env);
150
151 $this->assertCount(1, $preconditions);
152 $this->assertEquals($step2->getHash(), $preconditions[0]->getHash());
153 }
154
155 public function testGetAllSteps()
156 {
157 $steps = $this->test1->_getSteps();
158
159 $expected = [1,2,4];
160
161 $this->assertEquals($expected, array_values($steps));
162 }
163
164 public function testAchieveAllSteps()
165 {
166 $env = $this->createMock(Environment::class);
167 $db = $this->createMock(\ilDBInterface::class);
168
169 $env
170 ->method("getResource")
171 ->will($this->returnValueMap([
172 [Environment::RESOURCE_DATABASE, $db],
173 [\ilDatabaseUpdateStepExecutionLog::class, null]
174 ]));
175
176 $db
177 ->expects($this->exactly(3))
178 ->method("connect");
179
180 $this->base
181 ->method("getPreconditions")
182 ->willReturn([]);
183
184 $this->base
185 ->expects($this->once())
186 ->method("achieve")
187 ->with($env)
188 ->willReturn($env);
189
190 $i = new ObjectiveIterator($env, $this->test1);
191 while ($i->valid()) {
192 $current = $i->current();
193 $env = $current->achieve($env);
194 $i->setEnvironment($env);
195 $i->next();
196 }
197
198 $this->assertEquals([1,2,4], $this->test1->called);
199 }
200
201 public function testAchieveSomeSteps()
202 {
203 $env = $this->createMock(Environment::class);
204 $log = $this->createMock(\ilDatabaseUpdateStepExecutionLog::class);
205 $db = $this->createMock(\ilDBInterface::class);
206
207 $env
208 ->method("getResource")
209 ->will($this->returnValueMap([
210 [Environment::RESOURCE_DATABASE, $db],
211 [\ilDatabaseUpdateStepExecutionLog::class, $log]
212 ]));
213
214 $log
215 ->expects($this->atLeastOnce())
216 ->method("getLastFinishedStep")
217 ->with(Test_ilDatabaseUpdateSteps::class)
218 ->willReturn(1);
219
220 $db
221 ->expects($this->exactly(2))
222 ->method("connect");
223
224 $this->base
225 ->method("getPreconditions")
226 ->willReturn([]);
227
228 $this->base
229 ->expects($this->once())
230 ->method("achieve")
231 ->with($env)
232 ->willReturn($env);
233
234 $i = new ObjectiveIterator($env, $this->test1);
235 while ($i->valid()) {
236 $current = $i->current();
237 $env = $current->achieve($env);
238 $i->setEnvironment($env);
239 $i->next();
240 }
241
242 $this->assertEquals([2,4], $this->test1->called);
243 }
244}
base()
Definition: base.php:4
An exception for terminatinating execution or to throw for unit testing.
Tries to enumerate all preconditions for the given objective, where the ones that can be achieved (i....
A non-objective, nothing to do to achieve it...
getPreconditions(Setup\Environment $environment)
getAdditionalPreconditionsForStep(int $num)
Get preconditions for steps.
This base-class simplifies the creation of (consecutive) database updates.
getSteps()
Get the numbers of the steps in this class.
An environment holds resources to be used in the setup process.
Definition: Environment.php:12
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
connect($return_false_on_error=false)
$steps
Definition: latex.php:3
$i
Definition: metadata.php:24
$log
Definition: result.php:15