@@ -44,15 +44,15 @@ public function testChunk(): void
4444 public function testChunkThrowsOnZeroSize (): void
4545 {
4646 $ this ->expectException (InvalidArgumentException::class);
47- $ this ->expectExceptionMessage ('chunk() requires a positive integer for the $size argument . ' );
47+ $ this ->expectExceptionMessage ('$size must be a positive integer. ' );
4848
4949 $ this ->createModel (UserModel::class)->chunk (0 , static function ($ row ): void {});
5050 }
5151
5252 public function testChunkThrowsOnNegativeSize (): void
5353 {
5454 $ this ->expectException (InvalidArgumentException::class);
55- $ this ->expectExceptionMessage ('chunk() requires a positive integer for the $size argument . ' );
55+ $ this ->expectExceptionMessage ('$size must be a positive integer. ' );
5656
5757 $ this ->createModel (UserModel::class)->chunk (-1 , static function ($ row ): void {});
5858 }
@@ -97,6 +97,76 @@ public function testChunkEmptyTable(): void
9797 $ this ->assertSame (0 , $ rowCount );
9898 }
9999
100+ public function testChunkRows (): void
101+ {
102+ $ chunkCount = 0 ;
103+ $ numRowsInChunk = [];
104+
105+ $ this ->createModel (UserModel::class)->chunkRows (2 , static function ($ rows ) use (&$ chunkCount , &$ numRowsInChunk ): void {
106+ $ chunkCount ++;
107+ $ numRowsInChunk [] = count ($ rows );
108+ });
109+
110+ $ this ->assertSame (2 , $ chunkCount );
111+ $ this ->assertSame ([2 , 2 ], $ numRowsInChunk );
112+ }
113+
114+ public function testChunkRowsThrowsOnZeroSize (): void
115+ {
116+ $ this ->expectException (InvalidArgumentException::class);
117+ $ this ->expectExceptionMessage ('$size must be a positive integer. ' );
118+
119+ $ this ->createModel (UserModel::class)->chunkRows (0 , static function ($ row ): void {});
120+ }
121+
122+ public function testChunkRowsThrowsOnNegativeSize (): void
123+ {
124+ $ this ->expectException (InvalidArgumentException::class);
125+ $ this ->expectExceptionMessage ('$size must be a positive integer. ' );
126+
127+ $ this ->createModel (UserModel::class)->chunkRows (-1 , static function ($ row ): void {});
128+ }
129+
130+ public function testChunkRowsEarlyExit (): void
131+ {
132+ $ rowCount = 0 ;
133+
134+ $ this ->createModel (UserModel::class)->chunkRows (2 , static function ($ rows ) use (&$ rowCount ): bool {
135+ $ rowCount ++;
136+
137+ return false ;
138+ });
139+
140+ $ this ->assertSame (1 , $ rowCount );
141+ }
142+
143+ public function testChunkRowsDoesNotRunExtraQuery (): void
144+ {
145+ $ queryCount = 0 ;
146+ $ listener = static function () use (&$ queryCount ): void {
147+ $ queryCount ++;
148+ };
149+
150+ Events::on ('DBQuery ' , $ listener );
151+ $ this ->createModel (UserModel::class)->chunkRows (4 , static function ($ rows ): void {});
152+ Events::removeListener ('DBQuery ' , $ listener );
153+
154+ $ this ->assertSame (2 , $ queryCount );
155+ }
156+
157+ public function testChunkRowsEmptyTable (): void
158+ {
159+ $ this ->db ->table ('user ' )->truncate ();
160+
161+ $ rowCount = 0 ;
162+
163+ $ this ->createModel (UserModel::class)->chunkRows (2 , static function ($ row ) use (&$ rowCount ): void {
164+ $ rowCount ++;
165+ });
166+
167+ $ this ->assertSame (0 , $ rowCount );
168+ }
169+
100170 public function testCanCreateAndSaveEntityClasses (): void
101171 {
102172 $ entity = $ this ->createModel (EntityModel::class)->where ('name ' , 'Developer ' )->first ();
0 commit comments