|
14 | 14 | namespace CodeIgniter\Models; |
15 | 15 |
|
16 | 16 | use CodeIgniter\Database\Exceptions\DataException; |
| 17 | +use CodeIgniter\Events\Events; |
| 18 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
17 | 19 | use CodeIgniter\I18n\Time; |
18 | 20 | use PHPUnit\Framework\Attributes\Group; |
19 | 21 | use Tests\Support\Models\EntityModel; |
@@ -53,6 +55,62 @@ public function testChunkArray(): void |
53 | 55 | $this->assertSame([2, 2], $numRowsInChunk); |
54 | 56 | } |
55 | 57 |
|
| 58 | + public function testChunkArrayThrowsOnZeroSize(): void |
| 59 | + { |
| 60 | + $this->expectException(InvalidArgumentException::class); |
| 61 | + $this->expectExceptionMessage('chunkArray() requires a positive integer for the $size argument.'); |
| 62 | + |
| 63 | + $this->createModel(UserModel::class)->chunkArray(0, static function ($row): void {}); |
| 64 | + } |
| 65 | + |
| 66 | + public function testChunkThrowsOnNegativeSize(): void |
| 67 | + { |
| 68 | + $this->expectException(InvalidArgumentException::class); |
| 69 | + $this->expectExceptionMessage('chunkArray() requires a positive integer for the $size argument.'); |
| 70 | + |
| 71 | + $this->createModel(UserModel::class)->chunkArray(-1, static function ($row): void {}); |
| 72 | + } |
| 73 | + |
| 74 | + public function testChunkArrayEarlyExit(): void |
| 75 | + { |
| 76 | + $rowCount = 0; |
| 77 | + |
| 78 | + $this->createModel(UserModel::class)->chunkArray(2, static function ($rows) use (&$rowCount): bool { |
| 79 | + $rowCount++; |
| 80 | + |
| 81 | + return false; |
| 82 | + }); |
| 83 | + |
| 84 | + $this->assertSame(1, $rowCount); |
| 85 | + } |
| 86 | + |
| 87 | + public function testChunkArrayDoesNotRunExtraQuery(): void |
| 88 | + { |
| 89 | + $queryCount = 0; |
| 90 | + $listener = static function () use (&$queryCount): void { |
| 91 | + $queryCount++; |
| 92 | + }; |
| 93 | + |
| 94 | + Events::on('DBQuery', $listener); |
| 95 | + $this->createModel(UserModel::class)->chunkArray(4, static function ($rows): void {}); |
| 96 | + Events::removeListener('DBQuery', $listener); |
| 97 | + |
| 98 | + $this->assertSame(2, $queryCount); |
| 99 | + } |
| 100 | + |
| 101 | + public function testChunkArrayEmptyTable(): void |
| 102 | + { |
| 103 | + $this->db->table('user')->truncate(); |
| 104 | + |
| 105 | + $rowCount = 0; |
| 106 | + |
| 107 | + $this->createModel(UserModel::class)->chunkArray(2, static function ($row) use (&$rowCount): void { |
| 108 | + $rowCount++; |
| 109 | + }); |
| 110 | + |
| 111 | + $this->assertSame(0, $rowCount); |
| 112 | + } |
| 113 | + |
56 | 114 | public function testCanCreateAndSaveEntityClasses(): void |
57 | 115 | { |
58 | 116 | $entity = $this->createModel(EntityModel::class)->where('name', 'Developer')->first(); |
|
0 commit comments