diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 83b5ef3d..b3647c30 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -428,6 +428,8 @@ public function each(Closure $callback, int $pageSize = 1000, bool $isCritical = */ public function chunk(int $pageSize, Closure $callback, bool $isCritical = false, bool $isolate = false): bool { + $this->limit(0); + $start = microtime(true); $chunk = function (Builder $query) use ($pageSize, $callback, $isCritical) { diff --git a/tests/Integration/QueryTest.php b/tests/Integration/QueryTest.php new file mode 100644 index 00000000..f14fe0b0 --- /dev/null +++ b/tests/Integration/QueryTest.php @@ -0,0 +1,67 @@ +setupTestOu(); + } + + public function test_it_can_paginate() + { + foreach (LazyCollection::range(1, 10) as $index) { + $this->makeUser($this->ou)->save(); + } + + $this->assertCount(10, User::paginate(5)); + } + + public function test_it_can_chunk() + { + foreach (LazyCollection::range(1, 10) as $index) { + $this->makeUser($this->ou)->save(); + } + + $pages = 0; + + User::chunk(5, function (Collection $results) use (&$pages) { + $pages++; + + $this->assertCount(5, $results); + }); + + $this->assertEquals(2, $pages); + } + + public function test_it_cannot_override_limit_when_chunking() + { + foreach (LazyCollection::range(1, 10) as $index) { + $this->makeUser($this->ou)->save(); + } + + $pages = 0; + + User::limit(1)->chunk(5, function (Collection $results) use (&$pages) { + $pages++; + + $this->assertCount(5, $results); + }); + + $this->assertEquals(2, $pages); + } +}