Skip to content

Commit 9eb34c1

Browse files
authored
Merge pull request #762 from DirectoryTree/bug-761
Bug 761 - Properly override query limit to `0` when chunking
2 parents ccb0f20 + 18fedbb commit 9eb34c1

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Query/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,8 @@ public function each(Closure $callback, int $pageSize = 1000, bool $isCritical =
428428
*/
429429
public function chunk(int $pageSize, Closure $callback, bool $isCritical = false, bool $isolate = false): bool
430430
{
431+
$this->limit(0);
432+
431433
$start = microtime(true);
432434

433435
$chunk = function (Builder $query) use ($pageSize, $callback, $isCritical) {

tests/Integration/QueryTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace LdapRecord\Tests\Integration;
4+
5+
use Illuminate\Support\LazyCollection;
6+
use LdapRecord\Models\OpenLDAP\User;
7+
use LdapRecord\Query\Collection;
8+
use LdapRecord\Tests\Integration\Concerns\MakesUsers;
9+
use LdapRecord\Tests\Integration\Concerns\SetupTestConnection;
10+
use LdapRecord\Tests\Integration\Concerns\SetupTestOu;
11+
12+
class QueryTest extends TestCase
13+
{
14+
use MakesUsers;
15+
use SetupTestConnection;
16+
use SetupTestOu;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->setupTestOu();
23+
}
24+
25+
public function test_it_can_paginate()
26+
{
27+
foreach (LazyCollection::range(1, 10) as $index) {
28+
$this->makeUser($this->ou)->save();
29+
}
30+
31+
$this->assertCount(10, User::paginate(5));
32+
}
33+
34+
public function test_it_can_chunk()
35+
{
36+
foreach (LazyCollection::range(1, 10) as $index) {
37+
$this->makeUser($this->ou)->save();
38+
}
39+
40+
$pages = 0;
41+
42+
User::chunk(5, function (Collection $results) use (&$pages) {
43+
$pages++;
44+
45+
$this->assertCount(5, $results);
46+
});
47+
48+
$this->assertEquals(2, $pages);
49+
}
50+
51+
public function test_it_cannot_override_limit_when_chunking()
52+
{
53+
foreach (LazyCollection::range(1, 10) as $index) {
54+
$this->makeUser($this->ou)->save();
55+
}
56+
57+
$pages = 0;
58+
59+
User::limit(1)->chunk(5, function (Collection $results) use (&$pages) {
60+
$pages++;
61+
62+
$this->assertCount(5, $results);
63+
});
64+
65+
$this->assertEquals(2, $pages);
66+
}
67+
}

0 commit comments

Comments
 (0)