Files
Zonneplan/tests/Feature/Api/HistoryApiTest.php
T

180 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 HistoryApiTest 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_history_endpoint_returns_electricity_and_gas_data(): void
{
$date = Carbon::today();
$electricityResponse = collect([
$this->createElectricityArray($date->copy()->setHour(0), 15.0),
$this->createElectricityArray($date->copy()->setHour(1), 16.0),
]);
$gasResponse = collect([
$this->createGasArray($date, 5.0),
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPricesForDate')
->once()
->andReturn($electricityResponse);
$client->shouldReceive('getGasPricesForDate')
->once()
->andReturn($gasResponse);
$this->app->bind(PriceClient::class, fn () => $client);
$response = $this->getJson('/api/history?date=' . $date->format('Y-m-d'));
$response->assertOk()
->assertJsonStructure([
'electricity' => [
'*' => [
'startDate',
'endDate',
'period',
'totalPriceTaxIncluded',
],
],
'gas' => [
'*' => [
'startDate',
'endDate',
'period',
'totalPriceTaxIncluded',
],
],
]);
}
public function test_history_endpoint_returns_correct_data(): void
{
$date = Carbon::today();
$electricityResponse = collect([
$this->createElectricityArray($date->copy()->setHour(0), 15.0),
$this->createElectricityArray($date->copy()->setHour(1), 16.0),
]);
$gasResponse = collect([
$this->createGasArray($date, 5.0),
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPricesForDate')
->once()
->andReturn($electricityResponse);
$client->shouldReceive('getGasPricesForDate')
->once()
->andReturn($gasResponse);
$this->app->bind(PriceClient::class, fn () => $client);
$response = $this->getJson('/api/history?date=' . $date->format('Y-m-d'));
$response->assertOk();
$data = $response->json();
$this->assertCount(2, $data['electricity']);
$this->assertCount(1, $data['gas']);
$this->assertEquals(15.0, $data['electricity'][0]['totalPriceTaxIncluded']);
$this->assertEquals(5.0, $data['gas'][0]['totalPriceTaxIncluded']);
}
public function test_history_endpoint_defaults_to_today(): void
{
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPricesForDate')
->once()
->andReturn(collect());
$client->shouldReceive('getGasPricesForDate')
->once()
->andReturn(collect());
$this->app->bind(PriceClient::class, fn () => $client);
$response = $this->getJson('/api/history');
$response->assertOk();
}
public function test_history_endpoint_handles_empty_data(): void
{
$date = Carbon::today();
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPricesForDate')
->once()
->andReturn(collect());
$client->shouldReceive('getGasPricesForDate')
->once()
->andReturn(collect());
$this->app->bind(PriceClient::class, fn () => $client);
$response = $this->getJson('/api/history?date=' . $date->format('Y-m-d'));
$response->assertOk();
$data = $response->json();
$this->assertEmpty($data['electricity']);
$this->assertEmpty($data['gas']);
}
private function createElectricityArray(Carbon $startDate, float $price): 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' => 80,
'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'),
];
}
}