refactor(dashboard,history): replace ViewModels with Pinia stores and improve backend

This commit is contained in:
2026-08-05 09:23:18 +02:00
parent a0387126de
commit 90930cd223
29 changed files with 1372 additions and 237 deletions
@@ -0,0 +1,129 @@
<?php
namespace Tests\Unit\Services;
use App\Contracts\PriceRepository;
use App\Data\ElectricityData;
use App\Data\GasData;
use App\Features\Dashboard\Services\DashboardService;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Mockery;
use Tests\TestCase;
class DashboardServiceTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function test_get_dashboard_data_returns_current_cheapest_sustainable_and_expensive_prices(): void
{
$now = Carbon::now();
$electricityData = collect([
$this->createElectricityData($now->copy()->subMinutes(30), 15.0, 80),
$this->createElectricityData($now->copy()->addHour(), 12.0, 90),
$this->createElectricityData($now->copy()->addHours(2), 20.0, 60),
$this->createElectricityData($now->copy()->addHours(3), 10.0, 95),
]);
$gasData = collect([
$this->createGasData($now->copy()->startOfDay(), 5.0),
$this->createGasData($now->copy()->addDay(), 4.0),
$this->createGasData($now->copy()->addDays(2), 6.0),
]);
$repository = Mockery::mock(PriceRepository::class);
$repository->shouldReceive('retrieveElectricity')->once()->andReturn($electricityData);
$repository->shouldReceive('retrieveGas')->once()->andReturn($gasData);
$service = new DashboardService($repository);
$result = $service->getDashboardData();
$this->assertEquals(15.0, $result->electricity->totalPriceTaxIncluded);
$this->assertEquals(10.0, $result->electricityCheapest->totalPriceTaxIncluded);
$this->assertEquals(95, $result->electricityDurable->sustainabilityScore);
$this->assertEquals(20.0, $result->electricityPriciest->totalPriceTaxIncluded);
$this->assertEquals(5.0, $result->gas->totalPriceTaxIncluded);
$this->assertEquals(4.0, $result->gasCheapest->totalPriceTaxIncluded);
$this->assertEquals(6.0, $result->gasPriciest->totalPriceTaxIncluded);
}
public function test_get_dashboard_data_returns_null_for_missing_current_price(): void
{
$now = Carbon::now();
$electricityData = collect([
$this->createElectricityData($now->copy()->addHours(5), 15.0, 80),
]);
$gasData = collect([
$this->createGasData($now->copy()->addDays(5), 5.0),
]);
$repository = Mockery::mock(PriceRepository::class);
$repository->shouldReceive('retrieveElectricity')->once()->andReturn($electricityData);
$repository->shouldReceive('retrieveGas')->once()->andReturn($gasData);
$service = new DashboardService($repository);
$result = $service->getDashboardData();
$this->assertNull($result->electricity);
$this->assertNull($result->gas);
$this->assertNotNull($result->electricityCheapest);
$this->assertNotNull($result->gasCheapest);
}
public function test_get_dashboard_data_with_empty_collections(): void
{
$repository = Mockery::mock(PriceRepository::class);
$repository->shouldReceive('retrieveElectricity')->once()->andReturn(collect());
$repository->shouldReceive('retrieveGas')->once()->andReturn(collect());
$service = new DashboardService($repository);
$result = $service->getDashboardData();
$this->assertNull($result->electricity);
$this->assertNull($result->electricityCheapest);
$this->assertNull($result->electricityDurable);
$this->assertNull($result->electricityPriciest);
$this->assertNull($result->gas);
$this->assertNull($result->gasCheapest);
$this->assertNull($result->gasPriciest);
}
private function createElectricityData(Carbon $startDate, float $price, int $sustainabilityScore): ElectricityData
{
return new ElectricityData(
startDate: $startDate,
endDate: $startDate->copy()->addHour(),
period: 'elec_hour',
marketPrice: (int) ($price * 100),
totalPriceTaxIncluded: $price,
priceInclHandlingVat: (int) ($price * 100),
priceTaxWithVat: (int) ($price * 20),
pricingProfile: 'dynamic',
carbonFootprintInGram: 500,
sustainabilityScore: $sustainabilityScore,
startDateDatetime: $startDate,
);
}
private function createGasData(Carbon $startDate, float $price): GasData
{
return new GasData(
startDate: $startDate,
endDate: $startDate->copy()->addDay(),
period: 'gas_day',
marketPrice: (int) ($price * 100),
totalPriceTaxIncluded: $price,
priceInclHandlingVat: (int) ($price * 100),
priceTaxWithVat: (int) ($price * 20),
startDateDatetime: $startDate,
);
}
}
+103
View File
@@ -0,0 +1,103 @@
<?php
namespace Tests\Unit\Services;
use App\Contracts\PriceRepository;
use App\Data\ElectricityData;
use App\Data\GasData;
use App\Features\History\Services\HistoryService;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Mockery;
use Tests\TestCase;
class HistoryServiceTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function test_get_history_data_returns_electricity_and_gas_for_date(): void
{
$date = Carbon::today();
$electricityData = collect([
$this->createElectricityData($date->copy()->setHour(0), 15.0),
$this->createElectricityData($date->copy()->setHour(1), 16.0),
]);
$gasData = collect([
$this->createGasData($date, 5.0),
]);
$repository = Mockery::mock(PriceRepository::class);
$repository->shouldReceive('retrieveElectricityForDate')
->once()
->with($date)
->andReturn($electricityData);
$repository->shouldReceive('retrieveGasForDate')
->once()
->with($date)
->andReturn($gasData);
$service = new HistoryService($repository);
$result = $service->getHistoryData($date);
$this->assertCount(2, $result->electricity);
$this->assertCount(1, $result->gas);
$this->assertEquals(15.0, $result->electricity->first()->totalPriceTaxIncluded);
$this->assertEquals(5.0, $result->gas->first()->totalPriceTaxIncluded);
}
public function test_get_history_data_with_empty_collections(): void
{
$date = Carbon::today();
$repository = Mockery::mock(PriceRepository::class);
$repository->shouldReceive('retrieveElectricityForDate')
->once()
->andReturn(collect());
$repository->shouldReceive('retrieveGasForDate')
->once()
->andReturn(collect());
$service = new HistoryService($repository);
$result = $service->getHistoryData($date);
$this->assertCount(0, $result->electricity);
$this->assertCount(0, $result->gas);
}
private function createElectricityData(Carbon $startDate, float $price): ElectricityData
{
return new ElectricityData(
startDate: $startDate,
endDate: $startDate->copy()->addHour(),
period: 'elec_hour',
marketPrice: (int) ($price * 100),
totalPriceTaxIncluded: $price,
priceInclHandlingVat: (int) ($price * 100),
priceTaxWithVat: (int) ($price * 20),
pricingProfile: 'dynamic',
carbonFootprintInGram: 500,
sustainabilityScore: 80,
startDateDatetime: $startDate,
);
}
private function createGasData(Carbon $startDate, float $price): GasData
{
return new GasData(
startDate: $startDate,
endDate: $startDate->copy()->addDay(),
period: 'gas_day',
marketPrice: (int) ($price * 100),
totalPriceTaxIncluded: $price,
priceInclHandlingVat: (int) ($price * 100),
priceTaxWithVat: (int) ($price * 20),
startDateDatetime: $startDate,
);
}
}