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