Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ public function toBase64(): string
return $this->image->encodeUsingFormat(Format::PNG)->toDataUri();
}

$this->buildInitial();

$key = $this->cacheKeyPrefix.$this->cacheKey();
Comment on lines +205 to 207

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added regression test it_uses_computed_initials_in_cache_key_for_to_base64 in tests/AvatarPhpTest.php. It uses reflection to compute the expected cache key with FooGenerator-produced initials ('foo'), then mocks the cache repository to assert that get() is called with exactly that key — ensuring buildInitial() runs before cacheKey() in toBase64().


// Check if the image is in the cache
Expand Down
28 changes: 28 additions & 0 deletions tests/AvatarPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,34 @@ public function it_can_generate_base64_from_cache()
$this->assertEquals($cachedAvatar, $result);
}

#[Test]
public function it_uses_computed_initials_in_cache_key_for_to_base64()
{
$name = 'John Doe';

// Compute the expected cache key by building initials with FooGenerator first
$helperAvatar = new \Laravolt\Avatar\Avatar;
$helperAvatar->setGenerator(new FooGenerator);
$helperAvatar->create($name)->setDimension(5, 5);
$reflection = new \ReflectionClass($helperAvatar);
$buildInitial = $reflection->getMethod('buildInitial');
$buildInitial->setAccessible(true);
$buildInitial->invoke($helperAvatar);
$cacheKeyMethod = $reflection->getMethod('cacheKey');
$cacheKeyMethod->setAccessible(true);
$expectedKey = 'avatar_'.$cacheKeyMethod->invoke($helperAvatar);

// Assert that toBase64() calls cache->get() with the initials-based key,
// confirming buildInitial() runs before cacheKey() in toBase64()
$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');
$cache->shouldReceive('get')->with($expectedKey)->once()->andReturn(null);
$cache->shouldReceive('put')->andReturn(true);

$avatar = new \Laravolt\Avatar\Avatar([], $cache);
$avatar->setGenerator(new FooGenerator);
$avatar->create($name)->setDimension(5, 5)->toBase64();
}

#[Test]
public function it_can_generate_file()
{
Expand Down