From c25aaa3371bb560044743129b05c1335ac091c49 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Fri, 22 Jul 2022 16:26:23 +0200 Subject: [PATCH] Fix & Complete tests --- app/Services/LogoService.php | 17 +++++- tests/Feature/Services/LogoServiceTest.php | 61 +++++++++++++++++++ tests/Unit/Listeners/CleanIconStorageTest.php | 4 +- 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 tests/Feature/Services/LogoServiceTest.php diff --git a/app/Services/LogoService.php b/app/Services/LogoService.php index 478dd547..07ca532a 100644 --- a/app/Services/LogoService.php +++ b/app/Services/LogoService.php @@ -38,8 +38,8 @@ class LogoService $logoFilename = $this->getLogo(strval($serviceName)); if ($logoFilename) { - $newFilename = Str::random(40).'.svg'; - return Storage::disk('icons')->put($newFilename, Storage::disk('logos')->get($logoFilename)) ? $newFilename : null; + $iconFilename = Str::random(40).'.svg'; + return $this->copyToIcons($logoFilename, $iconFilename) ? $iconFilename : null; } else return null; } @@ -149,4 +149,17 @@ class LogoService { return strtolower(str_replace(['+'], ['plus'], $domain)); } + + + /** + * Copy a logo file to the icons disk with a new name + * + * @param string $logoFilename + * @param string $iconFilename + * @return bool Weither the copy succed or not + */ + protected function copyToIcons($logoFilename, $iconFilename) : bool + { + return Storage::disk('icons')->put($iconFilename, Storage::disk('logos')->get($logoFilename)); + } } \ No newline at end of file diff --git a/tests/Feature/Services/LogoServiceTest.php b/tests/Feature/Services/LogoServiceTest.php new file mode 100644 index 00000000..c529c585 --- /dev/null +++ b/tests/Feature/Services/LogoServiceTest.php @@ -0,0 +1,61 @@ +partialMock(LogoService::class, function (MockInterface $mock) { + $mock->shouldAllowMockingProtectedMethods(); + $mock->shouldReceive('getLogo', 'copyToIcons') + ->once() + ->andReturn('service.svg', true); + }); + + $icon = $logoServiceMock->getIcon('service'); + + $this->assertNotNull($icon); + } + + + /** + * @test + */ + public function test_getIcon_returns_null_when_no_logo_exists() + { + $logoServiceMock = $this->partialMock(LogoService::class, function (MockInterface $mock) { + $mock->shouldReceive('getLogo') + ->once() + ->andReturn(null); + }); + + $icon = $logoServiceMock->getIcon('no_logo_should_exists_with_this_name'); + + $this->assertEquals(null, $icon); + } + +} \ No newline at end of file diff --git a/tests/Unit/Listeners/CleanIconStorageTest.php b/tests/Unit/Listeners/CleanIconStorageTest.php index 3a92ba40..42e217de 100644 --- a/tests/Unit/Listeners/CleanIconStorageTest.php +++ b/tests/Unit/Listeners/CleanIconStorageTest.php @@ -25,8 +25,8 @@ class CleanIconStorageTest extends TestCase $event = new TwoFAccountDeleted($twofaccount); $listener = new CleanIconStorage(); - Storage::shouldReceive('delete') - ->with('public/icons/' . $event->twofaccount->icon) + Storage::shouldReceive('disk->delete') + ->with($event->twofaccount->icon) ->andReturn(true); $this->assertNull($listener->handle($event));