2FAuth/tests/Feature/Services/LogoServiceTest.php

127 lines
3.3 KiB
PHP
Raw Normal View History

2022-07-22 16:26:23 +02:00
<?php
namespace Tests\Feature\Services;
use App\Services\LogoService;
use Illuminate\Foundation\Testing\WithoutMiddleware;
2022-12-09 10:52:17 +01:00
use Illuminate\Support\Facades\Http;
2022-12-13 12:07:29 +01:00
use Illuminate\Support\Facades\Storage;
2023-08-01 11:28:27 +02:00
use PHPUnit\Framework\Attributes\CoversClass;
2022-12-09 10:52:17 +01:00
use Tests\Data\HttpRequestTestData;
2022-12-13 12:07:29 +01:00
use Tests\TestCase;
2022-07-22 16:26:23 +02:00
/**
2023-08-01 11:28:27 +02:00
* LogoServiceTest test class
2022-07-22 16:26:23 +02:00
*/
2023-08-01 11:28:27 +02:00
#[CoversClass(LogoService::class)]
2022-07-22 16:26:23 +02:00
class LogoServiceTest extends TestCase
{
use WithoutMiddleware;
/**
* @test
*/
2022-12-13 12:07:29 +01:00
public function setUp() : void
2022-07-22 16:26:23 +02:00
{
parent::setUp();
}
/**
* @test
*/
2022-12-09 10:52:17 +01:00
public function test_getIcon_returns_stored_icon_file_when_logo_exists()
2022-07-22 16:26:23 +02:00
{
2022-12-13 12:07:29 +01:00
$svgLogo = HttpRequestTestData::SVG_LOGO_BODY;
2022-12-09 10:52:17 +01:00
$tfaJsonBody = HttpRequestTestData::TFA_JSON_BODY;
2022-07-22 16:26:23 +02:00
2022-12-09 10:52:17 +01:00
Http::preventStrayRequests();
Http::fake([
'https://raw.githubusercontent.com/2factorauth/twofactorauth/master/img/*' => Http::response($svgLogo, 200),
2022-12-13 12:07:29 +01:00
'https://2fa.directory/api/v3/tfa.json' => Http::response($tfaJsonBody, 200),
2022-12-09 10:52:17 +01:00
]);
Storage::fake('icons');
Storage::fake('logos');
$logoService = new LogoService();
2022-12-13 12:07:29 +01:00
$icon = $logoService->getIcon('twitter');
2022-11-22 15:15:52 +01:00
2022-07-22 16:26:23 +02:00
$this->assertNotNull($icon);
2022-12-09 10:52:17 +01:00
Storage::disk('icons')->assertExists($icon);
}
/**
* @test
*/
public function test_getIcon_returns_null_when_github_request_fails()
{
Http::preventStrayRequests();
Http::fake([
'https://raw.githubusercontent.com/2factorauth/twofactorauth/master/img/*' => Http::response('not found', 404),
]);
Storage::fake('icons');
Storage::fake('logos');
$logoService = new LogoService();
$icon = $logoService->getIcon('twitter');
$this->assertEquals(null, $icon);
}
/**
* @test
*/
public function test_getIcon_returns_null_when_logo_fetching_fails()
{
$tfaJsonBody = HttpRequestTestData::TFA_JSON_BODY;
Http::preventStrayRequests();
Http::fake([
'https://2fa.directory/api/v3/tfa.json' => Http::response($tfaJsonBody, 200),
]);
Storage::fake('icons');
Storage::fake('logos');
$logoService = new LogoService();
$icon = $logoService->getIcon('twitter');
$this->assertEquals(null, $icon);
2022-07-22 16:26:23 +02:00
}
/**
* @test
*/
public function test_getIcon_returns_null_when_no_logo_exists()
{
2022-12-09 10:52:17 +01:00
$logoService = new LogoService();
2022-07-22 16:26:23 +02:00
2022-12-09 10:52:17 +01:00
$icon = $logoService->getIcon('no_logo_should_exists_with_this_name');
2022-11-22 15:15:52 +01:00
2022-07-22 16:26:23 +02:00
$this->assertEquals(null, $icon);
}
2022-12-09 10:52:17 +01:00
/**
* @test
*/
public function test_logoService_loads_empty_collection_when_tfajson_fetching_fails()
{
$svgLogo = HttpRequestTestData::SVG_LOGO_BODY;
Http::preventStrayRequests();
Http::fake([
'https://raw.githubusercontent.com/2factorauth/twofactorauth/master/img/*' => Http::response($svgLogo, 200),
]);
Storage::fake('icons');
Storage::fake('logos');
$logoService = new LogoService();
2022-12-13 12:07:29 +01:00
$icon = $logoService->getIcon('twitter');
2022-12-09 10:52:17 +01:00
$this->assertNull($icon);
Storage::disk('logos')->assertMissing(LogoService::TFA_JSON);
}
2022-11-22 15:15:52 +01:00
}