Skip to content

Commit 79fdf0c

Browse files
[12.x] Add test for untested methods in LazyCollection (#54996)
* test: Implement comprehensive test cases for before() method This commit adds test coverage for the before() method including: - Non-strict comparison - Strict comparison - Callback function usage * test: Implement comprehensive test cases for shuffle() method This commit adds test coverage for the shuffle() method including:
1 parent 4701ef4 commit 79fdf0c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/Support/SupportLazyCollectionTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,47 @@ public function testAfter()
312312

313313
$this->assertSame(['name' => 'Mohamed', 'age' => 35], $result);
314314
}
315+
316+
public function testBefore()
317+
{
318+
// Test finding item before value with non-strict comparison
319+
$data = new LazyCollection([1, 2, '3', 4]);
320+
$result = $data->before(2);
321+
$this->assertSame(1, $result);
322+
323+
// Test finding item before value with strict comparison
324+
$result = $data->before(4, true);
325+
$this->assertSame('3', $result);
326+
327+
// Test finding item before the one that matches a callback condition
328+
$users = new LazyCollection([
329+
['name' => 'Taylor', 'age' => 35],
330+
['name' => 'Jeffrey', 'age' => 45],
331+
['name' => 'Mohamed', 'age' => 35],
332+
]);
333+
$result = $users->before(function ($user) {
334+
return $user['name'] === 'Jeffrey';
335+
});
336+
$this->assertSame(['name' => 'Taylor', 'age' => 35], $result);
337+
}
338+
339+
public function testShuffle()
340+
{
341+
$data = new LazyCollection([1, 2, 3, 4, 5]);
342+
$shuffled = $data->shuffle();
343+
344+
$this->assertCount(5, $shuffled);
345+
$this->assertEquals([1, 2, 3, 4, 5], $shuffled->sort()->values()->all());
346+
347+
// Test shuffling associative array maintains key-value pairs
348+
$users = new LazyCollection([
349+
'first' => ['name' => 'Taylor'],
350+
'second' => ['name' => 'Jeffrey'],
351+
]);
352+
$shuffled = $users->shuffle();
353+
354+
$this->assertCount(2, $shuffled);
355+
$this->assertTrue($shuffled->contains('name', 'Taylor'));
356+
$this->assertTrue($shuffled->contains('name', 'Jeffrey'));
357+
}
315358
}

0 commit comments

Comments
 (0)