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