From d735be8bab2a0e7c8a56f65d62f1aab92e12e177 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 10 Jun 2026 18:05:05 +0000 Subject: [PATCH 1/4] Initial plan From cddaf61d7654c9045f77e7f8780e7b2dcd03629f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 10 Jun 2026 18:06:17 +0000 Subject: [PATCH 2/4] fix: call buildInitial() before cacheKey() in toBase64() to prevent extra cache entries --- src/Avatar.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Avatar.php b/src/Avatar.php index a8c20f2..4bb7286 100644 --- a/src/Avatar.php +++ b/src/Avatar.php @@ -202,6 +202,8 @@ public function toBase64(): string return $this->image->encodeUsingFormat(Format::PNG)->toDataUri(); } + $this->buildInitial(); + $key = $this->cacheKeyPrefix.$this->cacheKey(); // Check if the image is in the cache From 456cc24637f5bce9b4f6052a8b32eb23f54f0716 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Jun 2026 03:46:59 +0000 Subject: [PATCH 3/4] test: add regression test for buildInitial() before cacheKey() in toBase64() --- tests/AvatarPhpTest.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/AvatarPhpTest.php b/tests/AvatarPhpTest.php index 213adfb..55a505e 100644 --- a/tests/AvatarPhpTest.php +++ b/tests/AvatarPhpTest.php @@ -205,6 +205,39 @@ 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); + + // The expected key must embed the generator-produced initials ('foo'), not an empty string + $emptyInitialsAvatar = new \Laravolt\Avatar\Avatar; + $emptyInitialsAvatar->create($name)->setDimension(5, 5); + $keyWithEmptyInitials = 'avatar_'.$cacheKeyMethod->invoke($emptyInitialsAvatar); + $this->assertNotEquals($expectedKey, $keyWithEmptyInitials, 'Cache keys with and without initials should differ'); + + // Assert that toBase64() calls cache->get() with the initials-based key + $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() { From eedaccf4bcc6566bb41cf92e8f2b5e50f607ab6a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Jun 2026 03:47:47 +0000 Subject: [PATCH 4/4] test: simplify regression test by removing redundant assertNotEquals --- tests/AvatarPhpTest.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/AvatarPhpTest.php b/tests/AvatarPhpTest.php index 55a505e..1a2e28f 100644 --- a/tests/AvatarPhpTest.php +++ b/tests/AvatarPhpTest.php @@ -222,13 +222,8 @@ public function it_uses_computed_initials_in_cache_key_for_to_base64() $cacheKeyMethod->setAccessible(true); $expectedKey = 'avatar_'.$cacheKeyMethod->invoke($helperAvatar); - // The expected key must embed the generator-produced initials ('foo'), not an empty string - $emptyInitialsAvatar = new \Laravolt\Avatar\Avatar; - $emptyInitialsAvatar->create($name)->setDimension(5, 5); - $keyWithEmptyInitials = 'avatar_'.$cacheKeyMethod->invoke($emptyInitialsAvatar); - $this->assertNotEquals($expectedKey, $keyWithEmptyInitials, 'Cache keys with and without initials should differ'); - - // Assert that toBase64() calls cache->get() with the initials-based key + // 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);