113 lines
3.8 KiB
PHP
113 lines
3.8 KiB
PHP
<?php
|
|
|
|
use App\Models\Station;
|
|
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();
|
|
});
|
|
|
|
it('upserts stations from API batch response', function (): void {
|
|
$apiStations = [
|
|
[
|
|
'node_id' => 'abc123',
|
|
'trading_name' => 'Village Garage',
|
|
'brand_name' => 'Village Garage',
|
|
'is_same_trading_and_brand_name' => true,
|
|
'is_motorway_service_station' => false,
|
|
'is_supermarket_service_station' => false,
|
|
'temporary_closure' => false,
|
|
'permanent_closure' => false,
|
|
'permanent_closure_date' => null,
|
|
'public_phone_number' => null,
|
|
'location' => [
|
|
'address_line_1' => '1 High Street',
|
|
'address_line_2' => null,
|
|
'city' => 'London',
|
|
'county' => null,
|
|
'country' => 'England',
|
|
'postcode' => 'SW1A 1AA',
|
|
'latitude' => 51.5,
|
|
'longitude' => -0.1,
|
|
],
|
|
'amenities' => [],
|
|
'opening_times'=> null,
|
|
'fuel_types' => ['E10', 'E5'],
|
|
],
|
|
];
|
|
|
|
$this->service->upsertStations($apiStations);
|
|
|
|
$station = Station::find('abc123');
|
|
expect($station)->not->toBeNull()
|
|
->and($station->trading_name)->toBe('Village Garage')
|
|
->and($station->postcode)->toBe('SW1A 1AA')
|
|
->and((float) $station->lat)->toBe(51.5)
|
|
->and($station->is_supermarket)->toBeFalse();
|
|
});
|
|
|
|
it('tags supermarket stations during upsert', function (): void {
|
|
$apiStations = [[
|
|
'node_id' => 'tesco1',
|
|
'trading_name' => 'TESCO',
|
|
'brand_name' => 'TESCO',
|
|
'is_same_trading_and_brand_name' => true,
|
|
'is_motorway_service_station' => false,
|
|
'is_supermarket_service_station' => true,
|
|
'temporary_closure' => false,
|
|
'permanent_closure' => false,
|
|
'permanent_closure_date' => null,
|
|
'public_phone_number' => null,
|
|
'location' => [
|
|
'address_line_1' => '1 Tesco Way',
|
|
'address_line_2' => null,
|
|
'city' => 'Bristol',
|
|
'county' => null,
|
|
'country' => 'England',
|
|
'postcode' => 'BS1 1AA',
|
|
'latitude' => 51.45,
|
|
'longitude' => -2.6,
|
|
],
|
|
'amenities' => [],
|
|
'opening_times'=> null,
|
|
'fuel_types' => ['E10'],
|
|
]];
|
|
|
|
$this->service->upsertStations($apiStations);
|
|
|
|
expect(Station::find('tesco1')->is_supermarket)->toBeTrue();
|
|
});
|