Laravel 13 support

This commit is contained in:
DariusIII
2026-03-26 10:48:12 +01:00
parent 925f2d5383
commit 6aee2ef718
39 changed files with 1741 additions and 1050 deletions
+32 -10
View File
@@ -25,10 +25,21 @@ class AdminContentControllerTest extends TestCase
{
private string $databasePath;
/**
* @var array<string, string|false>
*/
private array $originalEnvironment = [];
public function createApplication()
{
$this->databasePath = sys_get_temp_dir().'/nntmux-admin-content-test.sqlite';
$this->originalEnvironment = [
'APP_ENV' => getenv('APP_ENV'),
'DB_CONNECTION' => getenv('DB_CONNECTION'),
'DB_DATABASE' => getenv('DB_DATABASE'),
];
if (file_exists($this->databasePath)) {
unlink($this->databasePath);
}
@@ -41,16 +52,9 @@ class AdminContentControllerTest extends TestCase
('title', 'NNTmux Test'),
('home_link', '/')");
putenv('APP_ENV=testing');
putenv('DB_CONNECTION=sqlite');
putenv('DB_DATABASE='.$this->databasePath);
$_ENV['APP_ENV'] = 'testing';
$_ENV['DB_CONNECTION'] = 'sqlite';
$_ENV['DB_DATABASE'] = $this->databasePath;
$_SERVER['APP_ENV'] = 'testing';
$_SERVER['DB_CONNECTION'] = 'sqlite';
$_SERVER['DB_DATABASE'] = $this->databasePath;
$this->setEnvironmentValue('APP_ENV', 'testing');
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
$app = require __DIR__.'/../../bootstrap/app.php';
@@ -90,6 +94,24 @@ class AdminContentControllerTest extends TestCase
}
parent::tearDown();
foreach ($this->originalEnvironment as $key => $value) {
$this->setEnvironmentValue($key, $value === false ? null : $value);
}
}
private function setEnvironmentValue(string $key, ?string $value): void
{
if ($value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);
return;
}
putenv($key.'='.$value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
public function test_admin_can_create_content_without_a_title(): void
@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Services\ConsoleService;
use App\Services\IGDB\Models\Game;
use App\Services\IGDBService;
use App\Services\ReleaseImageService;
use Mockery;
use ReflectionClass;
use Tests\TestCase;
class ConsoleServiceIgdbDelegationTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function test_fetch_igdb_properties_delegates_to_igdb_service_and_maps_console_genre_id(): void
{
$igdbService = Mockery::mock(IGDBService::class);
$game = new Game(['id' => 42, 'name' => 'Halo']);
$igdbService->shouldReceive('isConfigured')->once()->andReturn(true);
$igdbService->shouldReceive('searchConsole')->once()->with('Halo', 'Xbox 360')->andReturn($game);
$igdbService->shouldReceive('buildConsoleData')->once()->with($game, 'Xbox 360')->andReturn([
'title' => 'Halo',
'asin' => '42',
'review' => 'Sci-fi shooter',
'coverurl' => 'https://images.example.test/halo.jpg',
'releasedate' => '2001-11-15',
'esrb' => '94%',
'url' => 'https://www.igdb.com/games/halo',
'publisher' => 'Microsoft',
'platform' => 'Xbox 360',
'consolegenre' => 'Action',
'salesrank' => '',
]);
/** @var ConsoleServiceTestDouble $service */
$service = (new ReflectionClass(ConsoleServiceTestDouble::class))->newInstanceWithoutConstructor();
$service->initialize($igdbService);
$result = $service->fetchIGDBProperties('Halo', 'X360');
$this->assertIsArray($result);
$this->assertSame('Xbox 360', $result['platform']);
$this->assertSame('Action', $result['consolegenre']);
$this->assertSame(7, $result['consolegenreid']);
}
public function test_parse_title_no_longer_returns_legacy_browse_node(): void
{
/** @var ConsoleServiceTestDouble $service */
$service = (new ReflectionClass(ConsoleServiceTestDouble::class))->newInstanceWithoutConstructor();
$result = $service->parseTitle('Halo.3.X360-PROPER');
$this->assertIsArray($result);
$this->assertSame('Halo 3', $result['title']);
$this->assertSame('X360', $result['platform']);
$this->assertArrayNotHasKey('node', $result);
}
}
class ConsoleServiceTestDouble extends ConsoleService
{
public function initialize(IGDBService $igdbService): void
{
$this->echoOutput = false;
$this->gameQty = 0;
$this->lookupThrottleMs = 0;
$this->imgSavePath = '';
$this->renamed = false;
$this->failCache = [];
$this->igdbService = $igdbService;
$this->imageService = new ReleaseImageService;
}
protected function loadGenres(): array
{
return [7 => 'action'];
}
}
+143
View File
@@ -0,0 +1,143 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Services\IGDB\Models\Game;
use App\Services\IGDBService;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class IgdbQueryBuilderTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Cache::flush();
config([
'igdb.base_url' => 'https://api.igdb.test/v4',
'igdb.token_url' => 'https://id.twitch.test/oauth2/token',
'igdb.credentials.client_id' => 'client-id',
'igdb.credentials.client_secret' => 'client-secret',
'igdb.cache_lifetime' => 0,
]);
}
public function test_game_query_builder_uses_local_client_and_hydrates_nested_results(): void
{
Http::fake([
'https://id.twitch.test/oauth2/token' => Http::response([
'access_token' => 'test-token',
'expires_in' => 3600,
]),
'https://api.igdb.test/v4/games' => Http::response([
[
'id' => 42,
'name' => 'Halo',
'first_release_date' => 978307200,
'cover' => [
'url' => '//images.igdb.com/igdb/image/upload/t_cover_big/test.jpg',
],
'themes' => [
['name' => 'Sci-Fi'],
],
'platforms' => [6, 14],
],
]),
]);
$results = Game::search('Halo')
->whereIn('platforms', [6, 14])
->with([
'cover' => ['url'],
'themes' => ['name'],
])
->orderByDesc('aggregated_rating_count')
->limit(10)
->get();
$this->assertCount(1, $results);
$game = $results->first();
$this->assertInstanceOf(Game::class, $game);
$this->assertSame('Halo', $game->name);
$this->assertSame('//images.igdb.com/igdb/image/upload/t_cover_big/test.jpg', $game->cover->url);
$this->assertSame('Sci-Fi', $game->themes[0]->name);
$this->assertSame('2001-01-01', $game->first_release_date->format('Y-m-d'));
Http::assertSent(function (Request $request): bool {
if ($request->url() !== 'https://api.igdb.test/v4/games') {
return false;
}
$body = $request->body();
return $request->hasHeader('Client-ID', 'client-id')
&& $request->hasHeader('Authorization', 'Bearer test-token')
&& str_contains($body, 'fields *,cover.url,themes.name;')
&& str_contains($body, 'search "Halo";')
&& str_contains($body, 'where platforms = (6,14);')
&& str_contains($body, 'sort aggregated_rating_count desc;')
&& str_contains($body, 'limit 10;');
});
}
public function test_igdb_service_reads_the_local_igdb_configuration(): void
{
$service = new IGDBService;
$this->assertTrue($service->isConfigured());
config([
'igdb.credentials.client_id' => '',
]);
$this->assertFalse($service->isConfigured());
}
public function test_igdb_service_can_build_console_data_using_platform_hint_matching(): void
{
Http::fake([
'https://id.twitch.test/oauth2/token' => Http::response([
'access_token' => 'test-token',
'expires_in' => 3600,
]),
'https://api.igdb.test/v4/games' => Http::response([
[
'id' => 42,
'name' => 'Halo',
'summary' => 'Sci-fi shooter',
'aggregated_rating' => 94.2,
'first_release_date' => 1005782400,
'cover' => [
'image_id' => 'halo-cover',
],
'themes' => [
['name' => 'Action'],
],
'platforms' => [
['name' => 'Xbox 360', 'abbreviation' => 'X360'],
['name' => 'PC (Microsoft Windows)', 'abbreviation' => 'PC'],
],
],
]),
]);
$service = new IGDBService;
$game = $service->searchConsole('Halo', 'X360');
$this->assertNotNull($game);
$consoleData = $service->buildConsoleData($game, 'Xbox 360');
$this->assertSame('42', $consoleData['asin']);
$this->assertSame('Xbox 360', $consoleData['platform']);
$this->assertSame('Action', $consoleData['consolegenre']);
$this->assertSame('94%', $consoleData['esrb']);
$this->assertSame('https://images.igdb.com/igdb/image/upload/t_cover_big/halo-cover.jpg', $consoleData['coverurl']);
$this->assertSame('2001-11-15', $consoleData['releasedate']);
}
}