ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilDBStepExecutionDBTest.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\MockObject\MockObject;
20use PHPUnit\Framework\TestCase;
21
22class ilDBStepExecutionDBTest extends TestCase
23{
24 public const CLASS_NAME_200 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
25 public const CLASS_NAME_201 = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
29 private ilDBInterface|MockObject $db;
31
32 protected function setUp(): void
33 {
34 $this->db = $this->createMock(\ilDBInterface::class);
35 $this->execution_db = new \ilDBStepExecutionDB($this->db, fn(): \DateTime => new \DateTime());
36 }
37
38 public function testStartedThrowsOnLongClassName(): void
39 {
40 $this->expectException(\InvalidArgumentException::class);
41 $this->execution_db->started(self::CLASS_NAME_201, 1);
42 }
43
44 public function testFinishedThrowsOnLongClassName(): void
45 {
46 $this->expectException(\InvalidArgumentException::class);
47 $this->execution_db->finished(self::CLASS_NAME_201, 1);
48 }
49
51 {
52 $this->expectException(\InvalidArgumentException::class);
53 $this->execution_db->getLastStartedStep(self::CLASS_NAME_201);
54 }
55
57 {
58 $this->expectException(\InvalidArgumentException::class);
59 $this->execution_db->getLastFinishedStep(self::CLASS_NAME_201);
60 }
61
63 {
64 $STEP = 1;
65 $NOW = "2021-08-12 13:37:23.111111";
66
67 $execution_db = $this->getMockBuilder(\ilDBStepExecutionDB::class)
68 ->onlyMethods(["getLastStartedStep", "getLastFinishedStep"])
69 ->setConstructorArgs([$this->db, fn(): \DateTime => new \DateTime($NOW)])
70 ->getMock();
71
72 $execution_db->expects($this->once())
73 ->method("getLastFinishedStep")
74 ->with(self::CLASS_NAME_200)
75 ->willReturn(2);
76
77 $this->expectException(\RuntimeException::class);
78
79 $execution_db->started(self::CLASS_NAME_200, $STEP);
80 }
81
83 {
84 $NOW = "2021-08-12 13:37:23.111111";
85
86 $execution_db = $this->getMockBuilder(\ilDBStepExecutionDB::class)
87 ->onlyMethods(["getLastStartedStep", "getLastFinishedStep"])
88 ->setConstructorArgs([$this->db, fn(): \DateTime => new \DateTime($NOW)])
89 ->getMock();
90
91 $execution_db->expects($this->once())
92 ->method("getLastFinishedStep")
93 ->with(self::CLASS_NAME_200)
94 ->willReturn(1);
95
96
97 $execution_db->expects($this->once())
98 ->method("getLastStartedStep")
99 ->with(self::CLASS_NAME_200)
100 ->willReturn(2);
101
102 $this->expectException(\RuntimeException::class);
103
104 $execution_db->started(self::CLASS_NAME_200, 3);
105 }
106
108 {
109 $STEP = 1;
110 $NOW = "2021-08-12 13:37:23.111111";
111
112 $execution_db = $this->getMockBuilder(\ilDBStepExecutionDB::class)
113 ->onlyMethods(["getLastStartedStep", "getLastFinishedStep"])
114 ->setConstructorArgs([$this->db, fn(): \DateTime => new \DateTime($NOW)])
115 ->getMock();
116
117 $execution_db->expects($this->once())
118 ->method("getLastStartedStep")
119 ->with(self::CLASS_NAME_200)
120 ->willReturn(2);
121
122 $this->expectException(\RuntimeException::class);
123
124 $execution_db->finished(self::CLASS_NAME_200, $STEP);
125 }
126
128 {
129 $result = $this->getMockBuilder(ilDBStatement::class)->getMock();
130 $this->db
131 ->method("query")
132 ->willReturn($result);
133 $this->db
134 ->method("fetchAssoc")
135 ->willReturn([ilDBStepExecutionDB::FIELD_STEP => null]);
136
137 $this->assertSame(0, $this->execution_db->getLastStartedStep(self::CLASS_NAME_200));
138 }
139
141 {
142 $result = $this->getMockBuilder(ilDBStatement::class)->getMock();
143 $this->db
144 ->method("query")
145 ->willReturn($result);
146 $this->db
147 ->method("fetchAssoc")
148 ->willReturn([ilDBStepExecutionDB::FIELD_STEP => null]);
149
150 $this->assertSame(0, $this->execution_db->getLastFinishedStep(self::CLASS_NAME_200));
151 }
152
153 public function testStartedWritesToDB(): void
154 {
155 $STEP = 2;
156 $NOW = "2021-08-12 13:37:23.111111";
157
158 $execution_db = $this->getMockBuilder(\ilDBStepExecutionDB::class)
159 ->onlyMethods(["getLastStartedStep", "getLastFinishedStep"])
160 ->setConstructorArgs([$this->db, fn(): \DateTime => new \DateTime($NOW)])
161 ->getMock();
162
163 $execution_db->expects($this->once())
164 ->method("getLastStartedStep")
165 ->with(self::CLASS_NAME_200)
166 ->willReturn(1);
167
168 $execution_db->expects($this->once())
169 ->method("getLastFinishedStep")
170 ->with(self::CLASS_NAME_200)
171 ->willReturn(1);
172
173 $this->db->expects($this->once())
174 ->method("insert")
175 ->with(
177 [
178 ilDBStepExecutionDB::FIELD_CLASS => ["text", self::CLASS_NAME_200],
179 ilDBStepExecutionDB::FIELD_STEP => ["integer", $STEP],
180 ilDBStepExecutionDB::FIELD_STARTED => ["text", $NOW]
181 ]
182 );
183
184 $execution_db->started(self::CLASS_NAME_200, $STEP);
185 }
186
187 public function testFinishedWritesToDB(): void
188 {
189 $STEP = 2;
190 $NOW = "2021-08-12 13:37:23.222222";
191
192 $execution_db = $this->getMockBuilder(\ilDBStepExecutionDB::class)
193 ->onlyMethods(["getLastStartedStep", "getLastFinishedStep"])
194 ->setConstructorArgs([$this->db, fn(): \DateTime => new \DateTime($NOW)])
195 ->getMock();
196
197 $execution_db->expects($this->once())
198 ->method("getLastStartedStep")
199 ->with(self::CLASS_NAME_200)
200 ->willReturn(2);
201
202 $this->db->expects($this->once())
203 ->method("update")
204 ->with(
206 [
208 ],
209 [
210 ilDBStepExecutionDB::FIELD_CLASS => ["text", self::CLASS_NAME_200],
211 ilDBStepExecutionDB::FIELD_STEP => ["integer", $STEP]
212 ]
213 );
214
215 $execution_db->finished(self::CLASS_NAME_200, $STEP);
216 }
217
218 public function testGetLastStartedStepQueriesDB(): void
219 {
220 $STEP = 23;
221
222 $this->db->expects($this->once())
223 ->method("quote")
224 ->willReturnCallback(
225 function ($field, $type): string {
226 $this->assertEquals(self::CLASS_NAME_200, $field);
227 $this->assertEquals('text', $type);
228 return 'CLASS';
229 }
230 );
231
232 $result = $this->getMockBuilder(ilDBStatement::class)->getMock();
233 $this->db->expects($this->once())
234 ->method("query")
235 ->with(
238 " WHERE " . ilDBStepExecutionDB::FIELD_CLASS . " = CLASS"
239 )
240 ->willReturn($result);
241 $this->db->expects($this->once())
242 ->method("fetchAssoc")
243 ->willReturn([ilDBStepExecutionDB::FIELD_STEP => $STEP]);
244
245 $this->assertSame($STEP, $this->execution_db->getLastStartedStep(self::CLASS_NAME_200));
246 }
247
248 public function testGetLastFinishedStepQueriesDB(): void
249 {
250 $STEP = 23;
251
252 $this->db->expects($this->once())
253 ->method("quote")
254 ->willReturnCallback(
255 function ($field, $type): string {
256 $this->assertEquals(self::CLASS_NAME_200, $field);
257 $this->assertEquals('text', $type);
258 return 'CLASS';
259 }
260 );
261
262
263 $result = $this->getMockBuilder(ilDBStatement::class)->getMock();
264 $this->db->expects($this->once())
265 ->method("query")
266 ->with(
269 " WHERE " . ilDBStepExecutionDB::FIELD_CLASS . " = CLASS" .
270 " AND " . ilDBStepExecutionDB::FIELD_FINISHED . " IS NOT NULL"
271 )
272 ->willReturn($result);
273 $this->db->expects($this->once())
274 ->method("fetchAssoc")
275 ->willReturn([ilDBStepExecutionDB::FIELD_STEP => $STEP]);
276
277 $this->assertSame($STEP, $this->execution_db->getLastFinishedStep(self::CLASS_NAME_200));
278 }
279}
ilDBStepExecutionDB $execution_db
ilDBInterface MockObject $db
This logs the execution of database update steps.
started(string $class, int $step)
finished(string $class, int $step)
Interface ilDBInterface.