Files

157 lines
5.6 KiB
PHP

<?php
namespace Tests\Feature\Api;
use App\Contracts\PriceClient;
use App\Data\ElectricityData;
use App\Data\GasData;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Mockery;
use Tests\TestCase;
class DashboardApiTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['cache_prices.ttl' => 60]);
config(['cache_prices.prefix' => 'test:']);
}
protected function tearDown(): void
{
Mockery::close();
\Illuminate\Support\Facades\Cache::flush();
parent::tearDown();
}
public function test_dashboard_endpoint_returns_electricity_and_gas_data(): void
{
$now = Carbon::now();
$electricityResponse = collect([
$this->createElectricityArray($now->copy()->subMinutes(30), 15.0, 80),
$this->createElectricityArray($now->copy()->addHour(), 12.0, 90),
$this->createElectricityArray($now->copy()->addHours(2), 20.0, 60),
$this->createElectricityArray($now->copy()->addHours(3), 10.0, 95),
]);
$gasResponse = collect([
$this->createGasArray($now->copy()->startOfDay(), 5.0),
$this->createGasArray($now->copy()->addDay(), 4.0),
$this->createGasArray($now->copy()->addDays(2), 6.0),
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPrices')->once()->andReturn($electricityResponse);
$client->shouldReceive('getGasPrices')->once()->andReturn($gasResponse);
$this->app->bind(PriceClient::class, fn () => $client);
$response = $this->getJson('/api/energy-prices');
$response->assertOk()
->assertJsonStructure([
'electricity' => [
'startDate',
'endDate',
'period',
'totalPriceTaxIncluded',
],
'electricityCheapest',
'electricityDurable',
'electricityPriciest',
'gas' => [
'startDate',
'endDate',
'period',
'totalPriceTaxIncluded',
],
'gasCheapest',
'gasPriciest',
]);
}
public function test_dashboard_endpoint_returns_correct_prices(): void
{
$now = Carbon::now();
$electricityResponse = collect([
$this->createElectricityArray($now->copy()->subMinutes(30), 15.0, 80),
$this->createElectricityArray($now->copy()->addHour(), 12.0, 90),
$this->createElectricityArray($now->copy()->addHours(2), 20.0, 60),
]);
$gasResponse = collect([
$this->createGasArray($now->copy()->startOfDay(), 5.0),
$this->createGasArray($now->copy()->addDay(), 4.0),
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPrices')->once()->andReturn($electricityResponse);
$client->shouldReceive('getGasPrices')->once()->andReturn($gasResponse);
$this->app->bind(PriceClient::class, fn () => $client);
$response = $this->getJson('/api/energy-prices');
$response->assertOk();
$data = $response->json();
$this->assertEquals(15.0, $data['electricity']['totalPriceTaxIncluded']);
$this->assertEquals(12.0, $data['electricityCheapest']['totalPriceTaxIncluded']);
$this->assertEquals(20.0, $data['electricityPriciest']['totalPriceTaxIncluded']);
$this->assertEquals(5.0, $data['gas']['totalPriceTaxIncluded']);
$this->assertEquals(4.0, $data['gasCheapest']['totalPriceTaxIncluded']);
}
public function test_dashboard_endpoint_handles_empty_data(): void
{
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPrices')->once()->andReturn(collect());
$client->shouldReceive('getGasPrices')->once()->andReturn(collect());
$this->app->bind(PriceClient::class, fn () => $client);
$response = $this->getJson('/api/energy-prices');
$response->assertOk();
$data = $response->json();
$this->assertNull($data['electricity'] ?? null);
$this->assertNull($data['gas'] ?? null);
}
private function createElectricityArray(Carbon $startDate, float $price, int $sustainabilityScore): array
{
return [
'start_date' => $startDate->format('Y-m-d H:i:s'),
'end_date' => $startDate->copy()->addHour()->format('Y-m-d H:i:s'),
'period' => 'elec_hour',
'market_price' => (int) ($price * 100),
'total_price_tax_included' => $price,
'price_incl_handling_vat' => (int) ($price * 100),
'price_tax_with_vat' => (int) ($price * 20),
'pricing_profile' => 'dynamic',
'carbon_footprint_in_gram' => 500,
'sustainability_score' => $sustainabilityScore,
'start_date_datetime' => $startDate->format('Y-m-d H:i:s'),
];
}
private function createGasArray(Carbon $startDate, float $price): array
{
return [
'start_date' => $startDate->format('Y-m-d H:i:s'),
'end_date' => $startDate->copy()->addDay()->format('Y-m-d H:i:s'),
'period' => 'gas_day',
'market_price' => (int) ($price * 100),
'total_price_tax_included' => $price,
'price_incl_handling_vat' => (int) ($price * 100),
'price_tax_with_vat' => (int) ($price * 20),
'start_date_datetime' => $startDate->format('Y-m-d H:i:s'),
];
}
}