130 lines
4.8 KiB
PHP
130 lines
4.8 KiB
PHP
<?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,
|
|
);
|
|
}
|
|
}
|