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