Skip to content

Commit 2b02414

Browse files
Add comprehensive tests for LazyCollection diff methods
This commit adds thorough tests for three key diff methods in the LazyCollection class: 1. testDiffAssoc: Tests the diffAssoc method which returns items that exist in the original collection but not in the given collection, comparing both keys and values. 2. testDiffKeys: Tests the diffKeys method which returns items from the original collection whose keys don't exist in the given collection. 3. testDiffKeysUsing: Tests the diffKeysUsing method with a custom comparison callback that groups keys by hundreds, demonstrating how to implement custom key comparison logic. These tests ensure proper functionality of collection difference operations and verify that each method returns a new LazyCollection instance.
1 parent 44a18fc commit 2b02414

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

tests/Support/SupportLazyCollectionTest.php

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,14 @@ public function testDiff()
456456
'Jeffrey Way',
457457
'Adam Wathan',
458458
'Caleb Porzio',
459-
'Mohamed Said'
459+
'Mohamed Said',
460460
]);
461461

462462
$diff = $collection->diff(
463463
[
464464
'Jeffrey Way',
465465
'Caleb Porzio',
466-
'Nuno Maduro'
466+
'Nuno Maduro',
467467
]);
468468

469469
$this->assertEquals([0 => 'Taylor Otwell', 2 => 'Adam Wathan', 4 => 'Mohamed Said'], $diff->all());
@@ -484,4 +484,82 @@ public function testDiffUsing()
484484

485485
$this->assertEquals([1 => 20, 2 => 30, 3 => 40], $diff2->all());
486486
}
487+
488+
public function testDiffAssoc()
489+
{
490+
$collection = new LazyCollection([
491+
'name' => 'Laravel',
492+
'version' => '10.0',
493+
'type' => 'framework',
494+
'license' => 'MIT',
495+
'features' => 'Eloquent',
496+
]);
497+
498+
$diff = $collection->diffAssoc([
499+
'name' => 'Laravel',
500+
'version' => '9.0',
501+
'type' => 'framework',
502+
'author' => 'Taylor Otwell',
503+
'features' => 'Blade',
504+
]);
505+
506+
$this->assertEquals(
507+
[
508+
'version' => '10.0',
509+
'license' => 'MIT',
510+
'features' => 'Eloquent',
511+
],
512+
$diff->all()
513+
);
514+
515+
$this->assertInstanceOf(LazyCollection::class, $diff);
516+
}
517+
518+
public function testDiffKeys()
519+
{
520+
$collection = new LazyCollection([
521+
'one' => 'Laravel',
522+
'two' => 'Livewire',
523+
'three' => 'Blade',
524+
'four' => 'Eloquent',
525+
'five' => 'Forge',
526+
]);
527+
528+
$diff = $collection->diffKeys([
529+
'two' => 'Something else',
530+
'four' => 'Completely different value',
531+
'six' => 'Nova',
532+
]);
533+
534+
$this->assertEquals(
535+
[
536+
'one' => 'Laravel',
537+
'three' => 'Blade',
538+
'five' => 'Forge',
539+
],
540+
$diff->all()
541+
);
542+
$this->assertInstanceOf(LazyCollection::class, $diff);
543+
}
544+
545+
public function testDiffKeysUsing()
546+
{
547+
$collection = new LazyCollection([
548+
100 => 'Route definition',
549+
200 => 'Middleware',
550+
300 => 'Controller',
551+
400 => 'Model',
552+
]);
553+
554+
$diff = $collection->diffKeysUsing([
555+
150 => 'Something',
556+
250 => 'Something else',
557+
350 => 'Another thing',
558+
], function ($a, $b) {
559+
return (int) ($a / 100) - (int) ($b / 100);
560+
});
561+
562+
$this->assertEquals([400 => 'Model'], $diff->all());
563+
$this->assertInstanceOf(LazyCollection::class, $diff);
564+
}
487565
}

0 commit comments

Comments
 (0)