refactor(dashboard,history): replace ViewModels with Pinia stores and improve backend
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
<?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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user