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