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,189 @@
<?php
namespace Tests\Unit\Repositories;
use App\Contracts\PriceClient;
use App\Data\ElectricityData;
use App\Data\GasData;
use App\Repositories\ZonneplanPriceRepository;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Mockery;
use Tests\TestCase;
class ZonneplanPriceRepositoryTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['cache_prices.ttl' => 60]);
config(['cache_prices.prefix' => 'test:']);
}
protected function tearDown(): void
{
Mockery::close();
Cache::flush();
parent::tearDown();
}
public function test_retrieve_electricity_returns_collection_of_electricity_data(): void
{
$now = Carbon::now();
$electricityResponse = collect([
[
'start_date' => $now->format('Y-m-d H:i:s'),
'end_date' => $now->copy()->addHour()->format('Y-m-d H:i:s'),
'period' => 'elec_hour',
'market_price' => 1500,
'total_price_tax_included' => 15.50,
'price_incl_handling_vat' => 1550,
'price_tax_with_vat' => 300,
'pricing_profile' => 'dynamic',
'carbon_footprint_in_gram' => 500,
'sustainability_score' => 80,
'start_date_datetime' => $now->format('Y-m-d H:i:s'),
],
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPrices')
->once()
->andReturn($electricityResponse);
$repository = new ZonneplanPriceRepository($client);
$result = $repository->retrieveElectricity();
$this->assertInstanceOf(Collection::class, $result);
$this->assertCount(1, $result);
$this->assertInstanceOf(ElectricityData::class, $result->first());
}
public function test_retrieve_gas_returns_collection_of_gas_data(): void
{
$now = Carbon::now();
$gasResponse = collect([
[
'start_date' => $now->format('Y-m-d H:i:s'),
'end_date' => $now->copy()->addDay()->format('Y-m-d H:i:s'),
'period' => 'gas_day',
'market_price' => 500,
'total_price_tax_included' => 5.50,
'price_incl_handling_vat' => 550,
'price_tax_with_vat' => 100,
'start_date_datetime' => $now->format('Y-m-d H:i:s'),
],
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getGasPrices')
->once()
->andReturn($gasResponse);
$repository = new ZonneplanPriceRepository($client);
$result = $repository->retrieveGas();
$this->assertInstanceOf(Collection::class, $result);
$this->assertCount(1, $result);
$this->assertInstanceOf(GasData::class, $result->first());
}
public function test_retrieve_electricity_for_date_returns_collection(): void
{
$date = Carbon::today();
$electricityResponse = collect([
[
'start_date' => $date->copy()->setHour(0)->format('Y-m-d H:i:s'),
'end_date' => $date->copy()->setHour(1)->format('Y-m-d H:i:s'),
'period' => 'elec_hour',
'market_price' => 1500,
'total_price_tax_included' => 15.50,
'price_incl_handling_vat' => 1550,
'price_tax_with_vat' => 300,
'pricing_profile' => 'dynamic',
'carbon_footprint_in_gram' => 500,
'sustainability_score' => 80,
'start_date_datetime' => $date->copy()->setHour(0)->format('Y-m-d H:i:s'),
],
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPricesForDate')
->once()
->with($date)
->andReturn($electricityResponse);
$repository = new ZonneplanPriceRepository($client);
$result = $repository->retrieveElectricityForDate($date);
$this->assertInstanceOf(Collection::class, $result);
$this->assertCount(1, $result);
$this->assertInstanceOf(ElectricityData::class, $result->first());
}
public function test_retrieve_gas_for_date_returns_collection(): void
{
$date = Carbon::today();
$gasResponse = collect([
[
'start_date' => $date->format('Y-m-d H:i:s'),
'end_date' => $date->copy()->addDay()->format('Y-m-d H:i:s'),
'period' => 'gas_day',
'market_price' => 500,
'total_price_tax_included' => 5.50,
'price_incl_handling_vat' => 550,
'price_tax_with_vat' => 100,
'start_date_datetime' => $date->format('Y-m-d H:i:s'),
],
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getGasPricesForDate')
->once()
->with($date)
->andReturn($gasResponse);
$repository = new ZonneplanPriceRepository($client);
$result = $repository->retrieveGasForDate($date);
$this->assertInstanceOf(Collection::class, $result);
$this->assertCount(1, $result);
$this->assertInstanceOf(GasData::class, $result->first());
}
public function test_retrieve_electricity_caches_result(): void
{
$now = Carbon::now();
$electricityResponse = collect([
[
'start_date' => $now->format('Y-m-d H:i:s'),
'end_date' => $now->copy()->addHour()->format('Y-m-d H:i:s'),
'period' => 'elec_hour',
'market_price' => 1500,
'total_price_tax_included' => 15.50,
'price_incl_handling_vat' => 1550,
'price_tax_with_vat' => 300,
'pricing_profile' => 'dynamic',
'carbon_footprint_in_gram' => 500,
'sustainability_score' => 80,
'start_date_datetime' => $now->format('Y-m-d H:i:s'),
],
]);
$client = Mockery::mock(PriceClient::class);
$client->shouldReceive('getElectricityPrices')
->once()
->andReturn($electricityResponse);
$repository = new ZonneplanPriceRepository($client);
$result1 = $repository->retrieveElectricity();
$result2 = $repository->retrieveElectricity();
$this->assertEquals(
$result1->first()->totalPriceTaxIncluded,
$result2->first()->totalPriceTaxIncluded
);
}
}