Skip to content

[12.x] Add comprehensive test coverage for LazyCollection diff methods #55032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions tests/Support/SupportLazyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,119 @@ public function testDot()

$this->assertEquals($expected, $dotted->all());
}

public function testDiff()
{
$collection = new LazyCollection(
[
'Taylor Otwell',
'Jeffrey Way',
'Adam Wathan',
'Caleb Porzio',
'Mohamed Said',
]);

$diff = $collection->diff(
[
'Jeffrey Way',
'Caleb Porzio',
'Nuno Maduro',
]);

$this->assertEquals([0 => 'Taylor Otwell', 2 => 'Adam Wathan', 4 => 'Mohamed Said'], $diff->all());
$this->assertInstanceOf(LazyCollection::class, $diff);
}

public function testDiffUsing()
{
$collection = new LazyCollection(['Taylor', 'Jeffrey', 'Adam']);

$diff1 = $collection->diffUsing(['TAYLOR', 'JEFFREY'], 'strcasecmp');
$this->assertEquals([2 => 'Adam'], $diff1->all());

$numbers = new LazyCollection([10, 20, 30, 40]);
$diff2 = $numbers->diffUsing([5, 10, 15], function ($a, $b) {
return ($a / 10) <=> ($b / 10);
});

$this->assertEquals([1 => 20, 2 => 30, 3 => 40], $diff2->all());
}

public function testDiffAssoc()
{
$collection = new LazyCollection([
'name' => 'Laravel',
'version' => '10.0',
'type' => 'framework',
'license' => 'MIT',
'features' => 'Eloquent',
]);

$diff = $collection->diffAssoc([
'name' => 'Laravel',
'version' => '9.0',
'type' => 'framework',
'author' => 'Taylor Otwell',
'features' => 'Blade',
]);

$this->assertEquals(
[
'version' => '10.0',
'license' => 'MIT',
'features' => 'Eloquent',
],
$diff->all()
);

$this->assertInstanceOf(LazyCollection::class, $diff);
}

public function testDiffKeys()
{
$collection = new LazyCollection([
'one' => 'Laravel',
'two' => 'Livewire',
'three' => 'Blade',
'four' => 'Eloquent',
'five' => 'Forge',
]);

$diff = $collection->diffKeys([
'two' => 'Something else',
'four' => 'Completely different value',
'six' => 'Nova',
]);

$this->assertEquals(
[
'one' => 'Laravel',
'three' => 'Blade',
'five' => 'Forge',
],
$diff->all()
);
$this->assertInstanceOf(LazyCollection::class, $diff);
}

public function testDiffKeysUsing()
{
$collection = new LazyCollection([
100 => 'Route definition',
200 => 'Middleware',
300 => 'Controller',
400 => 'Model',
]);

$diff = $collection->diffKeysUsing([
150 => 'Something',
250 => 'Something else',
350 => 'Another thing',
], function ($a, $b) {
return (int) ($a / 100) - (int) ($b / 100);
});

$this->assertEquals([400 => 'Model'], $diff->all());
$this->assertInstanceOf(LazyCollection::class, $diff);
}
}
Loading