Skip to content

Commit

Permalink
(largest): Add largestByFileSize() to favicons collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
StyxOfDynamite committed Jan 25, 2024
1 parent fef6abd commit 28916f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Collections/FaviconCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ public function largest(): ?Favicon
fn (Favicon $favicon): ?int => $favicon->getIconSize()
)->first();
}

public function largestByFileSize() : ?Favicon
{
return $this->sortByDesc(
fn (Favicon $favicon): int => strlen($favicon->content())
)->first();
}
}
20 changes: 20 additions & 0 deletions tests/Feature/Collections/FaviconCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,24 @@ public function largest_favicon_can_be_retrieved_if_there_are_only_null_sizes():

self::assertSame('https://example.com/favicon/favicon-32x32.png', $largest->getFaviconUrl());
}

/** @test */
public function largest_favicon_can_be_retrieved_based_on_file_size()
{
// mock the favicons to specify file content lengths
$favicon1 = $this->createMock(Favicon::class);
$favicon1->method('getFaviconUrl')->willReturn('https://example.com/favicon/favicon-32x32.png');
$favicon1->method('content')->willReturn('some-short-string');

$favicon2 = $this->createMock(Favicon::class);
$favicon2->method('getFaviconUrl')->willReturn('https://example.com/favicon/favicon-64x64.png');
$favicon2->method('content')->willReturn('some-much-longer-string');

$largest = FaviconCollection::make([
$favicon1,
$favicon2,
])->largestByFileSize();

self::assertSame('https://example.com/favicon/favicon-64x64.png', $largest->getFaviconUrl());
}
}

0 comments on commit 28916f0

Please sign in to comment.