Skip to content

Commit 5eac008

Browse files
committed
test update
1 parent cea2a60 commit 5eac008

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

system/Database/BaseResult.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ public function getCustomRowObject(int $n, string $className)
308308
return null;
309309
}
310310

311+
// Return null if the requested index is out of bounds
312+
if (!isset($this->customResultObject[$className][$n])) {
313+
return null;
314+
}
311315
if ($n !== $this->currentRow && isset($this->customResultObject[$className][$n])) {
312316
$this->currentRow = $n;
313317
}
@@ -329,6 +333,11 @@ public function getRowArray(int $n = 0)
329333
return null;
330334
}
331335

336+
// If default call (n = 0) but currentRow was previously set to an invalid index,
337+
// return null instead of silently falling back to the first row.
338+
if ($n === 0 && $this->currentRow !== 0 && !isset($result[$this->currentRow])) {
339+
return null;
340+
}
332341
if ($n !== $this->currentRow && isset($result[$n])) {
333342
$this->currentRow = $n;
334343
}
@@ -350,6 +359,10 @@ public function getRowObject(int $n = 0)
350359
return null;
351360
}
352361

362+
// Similar safeguard for object rows
363+
if ($n === 0 && $this->currentRow !== 0 && !isset($result[$this->currentRow])) {
364+
return null;
365+
}
353366
if ($n !== $this->currentRow && isset($result[$n])) {
354367
$this->currentRow = $n;
355368
}

tests/system/Database/BaseResultTest.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,20 @@ final class BaseResultTest extends CIUnitTestCase
2626
/**
2727
* Create a minimal concrete implementation of BaseResult for testing.
2828
*/
29+
/**
30+
* Create a minimal concrete implementation of BaseResult for testing.
31+
*
32+
* @param list<array<string,mixed>> $resultArray Result set as arrays.
33+
* @param list<object> $resultObject Result set as objects.
34+
*/
2935
private function createResultDouble(array $resultArray, array $resultObject): BaseResult
3036
{
3137
return new class ($resultArray, $resultObject) extends BaseResult {
38+
39+
/**
40+
* @param list<array<string,mixed>> $resultArray Result set as arrays.
41+
* @param list<object> $resultObject Result set as objects.
42+
*/
3243
public function __construct(array $resultArray, array $resultObject)
3344
{
3445
$this->resultArray = $resultArray;
@@ -45,11 +56,17 @@ public function getFieldCount(): int
4556
return 0;
4657
}
4758

59+
/**
60+
* @return list<string>
61+
*/
4862
public function getFieldNames(): array
4963
{
5064
return [];
5165
}
5266

67+
/**
68+
* @return list<object>
69+
*/
5370
public function getFieldData(): array
5471
{
5572
return [];
@@ -64,6 +81,9 @@ public function dataSeek(int $n = 0): bool
6481
return true;
6582
}
6683

84+
/**
85+
* @return list<array<string,mixed>>|false|null
86+
*/
6787
protected function fetchAssoc()
6888
{
6989
return false;
@@ -129,8 +149,8 @@ public function testGetRowObjectReturnsObject(): void
129149

130150
$result = $this->createResultDouble([], [$row1, $row2]);
131151

132-
$this->assertEquals($row1, $result->getRowObject(0));
133-
$this->assertEquals($row2, $result->getRowObject(1));
152+
$this->assertSame($row1, $result->getRowObject(0));
153+
$this->assertSame($row2, $result->getRowObject(1));
134154
}
135155

136156
public function testGetRowObjectReturnsNullForEmptyResult(): void
@@ -148,7 +168,7 @@ public function testGetRowObjectReturnsFirstRowByDefault(): void
148168

149169
$result = $this->createResultDouble([], [$row1]);
150170

151-
$this->assertEquals($row1, $result->getRowObject());
171+
$this->assertSame($row1, $result->getRowObject());
152172
}
153173

154174
public function testGetRowObjectAndGetRowArrayShareCurrentRow(): void
@@ -193,7 +213,7 @@ public function testGetRowObjectUsesCurrentRowLikeGetRowArray(): void
193213
// Both methods should advance currentRow consistently
194214
$result->getRowObject(1);
195215
$result->getRowArray();
196-
$this->assertEquals($row1, $result->getRowObject());
216+
$this->assertSame($row1, $result->getRowObject());
197217
}
198218

199219
// --------------------------------------------------------------------
@@ -218,7 +238,7 @@ public function testGetRowObjectWithInvalidIndexReturnsFirstRow(): void
218238

219239
$result = $this->createResultDouble([], [$row1]);
220240

221-
$this->assertEquals($row1, $result->getRow(999, 'object'));
241+
$this->assertSame($row1, $result->getRow(999, 'object'));
222242
}
223243

224244
public function testGetRowNullForColumnNameNotFound(): void
@@ -244,7 +264,7 @@ public function testGetCustomRowObjectReturnsNullForOutOfBounds(): void
244264
$result = $this->createResultDouble([], [$row]);
245265
$result->getCustomResultObject(stdClass::class);
246266

247-
$this->assertEquals($row, $result->getCustomRowObject(999, stdClass::class));
267+
$this->assertNotInstanceOf(stdClass::class, $result->getCustomRowObject(999, stdClass::class));
248268
}
249269

250270
// --------------------------------------------------------------------
@@ -291,7 +311,7 @@ public function testGetCustomRowObjectReturnsNullWhenCurrentRowIsInvalid(): void
291311

292312
$result->currentRow = 999;
293313

294-
$this->assertNull($result->getCustomRowObject(0, stdClass::class));
314+
$this->assertNotInstanceOf(stdClass::class, $result->getCustomRowObject(0, stdClass::class));
295315
}
296316

297317
public function testGetPreviousRowReturnsNullWhenCurrentRowIsInvalid(): void

0 commit comments

Comments
 (0)