feat: FuelPriceService with OAuth token caching
Also extend Pest TestCase to Unit tests and guard MySQL-only migration DDL (composite PK + PARTITION BY) behind a driver check so in-memory SQLite tests can run migrations cleanly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
28
app/Services/FuelPriceService.php
Normal file
28
app/Services/FuelPriceService.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class FuelPriceService
|
||||
{
|
||||
private const TOKEN_CACHE_KEY = 'fuel_finder_access_token';
|
||||
|
||||
public function __construct(
|
||||
private readonly StationTaggingService $taggingService,
|
||||
) {}
|
||||
|
||||
public function getAccessToken(): string
|
||||
{
|
||||
return Cache::remember(self::TOKEN_CACHE_KEY, 3540, function (): string {
|
||||
$response = Http::timeout(10)
|
||||
->post(config('services.fuel_finder.base_url').'/oauth/generate_access_token', [
|
||||
'client_id' => config('services.fuel_finder.client_id'),
|
||||
'client_secret' => config('services.fuel_finder.client_secret'),
|
||||
]);
|
||||
|
||||
return $response->json('data.access_token');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,9 @@ return new class extends Migration
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('station_prices', function (Blueprint $table): void {
|
||||
$isMysql = DB::getDriverName() === 'mysql';
|
||||
|
||||
Schema::create('station_prices', function (Blueprint $table) use ($isMysql): void {
|
||||
$table->bigIncrements('id');
|
||||
$table->string('station_id', 64);
|
||||
$table->string('fuel_type', 20);
|
||||
@@ -21,13 +23,16 @@ return new class extends Migration
|
||||
$table->dateTime('price_reported_at');
|
||||
$table->dateTime('recorded_at');
|
||||
|
||||
// Composite PK required for MySQL range partitioning
|
||||
// Composite PK required for MySQL range partitioning (not supported by SQLite)
|
||||
if ($isMysql) {
|
||||
$table->primary(['id', 'price_effective_at']);
|
||||
}
|
||||
$table->index(['station_id', 'fuel_type', 'price_effective_at']);
|
||||
$table->index('price_effective_at');
|
||||
});
|
||||
|
||||
// Monthly partitions 2026–2027 + MAXVALUE catch-all
|
||||
// Monthly partitions 2026–2027 + MAXVALUE catch-all (MySQL only)
|
||||
if ($isMysql) {
|
||||
DB::statement("ALTER TABLE station_prices
|
||||
PARTITION BY RANGE (YEAR(price_effective_at) * 100 + MONTH(price_effective_at)) (
|
||||
PARTITION p202601 VALUES LESS THAN (202602),
|
||||
@@ -57,6 +62,7 @@ return new class extends Migration
|
||||
PARTITION pFuture VALUES LESS THAN MAXVALUE
|
||||
)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
|
||||
@@ -16,7 +16,7 @@ use Tests\TestCase;
|
||||
|
||||
pest()->extend(TestCase::class)
|
||||
// ->use(RefreshDatabase::class)
|
||||
->in('Feature');
|
||||
->in('Feature', 'Unit');
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
||||
40
tests/Unit/Services/FuelPriceServiceTest.php
Normal file
40
tests/Unit/Services/FuelPriceServiceTest.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use App\Services\FuelPriceService;
|
||||
use App\Services\StationTaggingService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->service = new FuelPriceService(new StationTaggingService());
|
||||
});
|
||||
|
||||
it('fetches and caches an access token', function (): void {
|
||||
Http::fake([
|
||||
'*/oauth/generate_access_token' => Http::response([
|
||||
'data' => [
|
||||
'access_token' => 'test-token-abc',
|
||||
'expires_in' => 3600,
|
||||
],
|
||||
]),
|
||||
]);
|
||||
|
||||
$token = $this->service->getAccessToken();
|
||||
|
||||
expect($token)->toBe('test-token-abc');
|
||||
expect(Cache::get('fuel_finder_access_token'))->toBe('test-token-abc');
|
||||
});
|
||||
|
||||
it('returns cached token without hitting API', function (): void {
|
||||
Cache::put('fuel_finder_access_token', 'cached-token', 3540);
|
||||
|
||||
Http::fake();
|
||||
|
||||
$token = $this->service->getAccessToken();
|
||||
|
||||
expect($token)->toBe('cached-token');
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
Reference in New Issue
Block a user