Skip to content

Commit 2367af3

Browse files
[12.x] Add missing tests for LazyCollection methods (#55022)
* Add test for collapseWithKeys method in LazyCollection This commit adds test coverage for the collapseWithKeys method, verifying: - Nested arrays collapse correctly with preserved keys - Mixed arrays and collections are handled properly - Empty items are skipped as expected * Add test for containsOneItem method in LazyCollection * Add test for doesntContain method in LazyCollection This commit adds test coverage for the doesntContain method, verifying it correctly returns the inverse of contains method for: - Direct value comparison - Key-value pair matching - Comparison operators - Callback functions * Add test for dot method in LazyCollection This commit adds test coverage for the dot method, verifying it correctly flattens multi-dimensional arrays with dot notation in keys for: - Nested associative arrays - Empty nested arrays - Arrays with numeric keys
1 parent 9a5efbc commit 2367af3

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

tests/Support/SupportLazyCollectionTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,96 @@ public function testShuffle()
355355
$this->assertTrue($shuffled->contains('name', 'Taylor'));
356356
$this->assertTrue($shuffled->contains('name', 'Jeffrey'));
357357
}
358+
359+
public function testCollapseWithKeys()
360+
{
361+
$collection = new LazyCollection([
362+
['a' => 1, 'b' => 2],
363+
['c' => 3, 'd' => 4],
364+
]);
365+
$collapsed = $collection->collapseWithKeys();
366+
367+
$this->assertEquals(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4], $collapsed->all());
368+
369+
$collection = new LazyCollection([
370+
['a' => 1],
371+
new LazyCollection(['b' => 2]),
372+
]);
373+
$collapsed = $collection->collapseWithKeys();
374+
375+
$this->assertEquals(['a' => 1, 'b' => 2], $collapsed->all());
376+
}
377+
378+
public function testContainsOneItem()
379+
{
380+
$collection = new LazyCollection([5]);
381+
$this->assertTrue($collection->containsOneItem());
382+
383+
$emptyCollection = new LazyCollection([]);
384+
$this->assertFalse($emptyCollection->containsOneItem());
385+
386+
$multipleCollection = new LazyCollection([1, 2, 3]);
387+
$this->assertFalse($multipleCollection->containsOneItem());
388+
}
389+
390+
public function testDoesntContain()
391+
{
392+
$collection = new LazyCollection([1, 2, 3, 4, 5]);
393+
394+
$this->assertTrue($collection->doesntContain(10));
395+
$this->assertFalse($collection->doesntContain(3));
396+
$this->assertTrue($collection->doesntContain('value', '>', 10));
397+
$this->assertTrue($collection->doesntContain(function ($value) {
398+
return $value > 10;
399+
}));
400+
401+
$users = new LazyCollection([
402+
[
403+
'name' => 'Taylor',
404+
'role' => 'developer',
405+
],
406+
[
407+
'name' => 'Jeffrey',
408+
'role' => 'designer',
409+
],
410+
]);
411+
412+
$this->assertTrue($users->doesntContain('name', 'Adam'));
413+
$this->assertFalse($users->doesntContain('name', 'Taylor'));
414+
}
415+
416+
public function testDot()
417+
{
418+
$collection = new LazyCollection([
419+
'foo' => [
420+
'bar' => 'baz',
421+
],
422+
'user' => [
423+
'name' => 'Taylor',
424+
'profile' => [
425+
'age' => 30,
426+
],
427+
],
428+
'users' => [
429+
0 => [
430+
'name' => 'Taylor',
431+
],
432+
1 => [
433+
'name' => 'Jeffrey',
434+
],
435+
],
436+
]);
437+
438+
$dotted = $collection->dot();
439+
440+
$expected = [
441+
'foo.bar' => 'baz',
442+
'user.name' => 'Taylor',
443+
'user.profile.age' => 30,
444+
'users.0.name' => 'Taylor',
445+
'users.1.name' => 'Jeffrey',
446+
];
447+
448+
$this->assertEquals($expected, $dotted->all());
449+
}
358450
}

0 commit comments

Comments
 (0)