Fix & Complete tests

This commit is contained in:
Bubka 2022-07-22 16:26:23 +02:00
parent 5aec206f99
commit c25aaa3371
3 changed files with 78 additions and 4 deletions

View File

@ -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));
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace Tests\Feature\Services;
use App\Services\LogoService;
use Tests\FeatureTestCase;
use Tests\TestCase;
use Mockery\MockInterface;
use Illuminate\Foundation\Testing\WithoutMiddleware;
/**
* @covers \App\Services\LogoService
*/
class LogoServiceTest extends TestCase
{
use WithoutMiddleware;
/**
* @test
*/
public function setUp() : void
{
parent::setUp();
}
/**
* @test
*/
public function test_getIcon_returns_iconFilename_when_logo_exists()
{
$logoServiceMock = $this->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);
}
}

View File

@ -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));