144 lines
5.3 KiB
PHP
144 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Clients;
|
|
|
|
use App\Clients\ZonneplanPriceClient;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Client\Factory;
|
|
use Illuminate\Http\Client\RequestException;
|
|
use Illuminate\Http\Response;
|
|
use Mockery;
|
|
use Tests\TestCase;
|
|
|
|
class ZonneplanPriceClientTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
Mockery::close();
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_get_electricity_prices_returns_collection(): void
|
|
{
|
|
$response = Mockery::mock();
|
|
$response->shouldReceive('throw')->once();
|
|
$response->shouldReceive('json')
|
|
->once()
|
|
->with('data', [])
|
|
->andReturn([
|
|
['start_date' => Carbon::now()->format('Y-m-d H:i:s'), 'total_price_tax_included' => 15.0],
|
|
]);
|
|
$response->shouldReceive('status')->once()->andReturn(200);
|
|
|
|
$http = Mockery::mock(Factory::class);
|
|
$http->shouldReceive('baseUrl')->once()->andReturnSelf();
|
|
$http->shouldReceive('withQueryParameters')->once()->andReturnSelf();
|
|
$http->shouldReceive('timeout')->once()->andReturnSelf();
|
|
$http->shouldReceive('retry')->once()->andReturnSelf();
|
|
$http->shouldReceive('get')->once()->andReturn($response);
|
|
|
|
$client = new ZonneplanPriceClient($http);
|
|
$result = $client->getElectricityPrices();
|
|
|
|
$this->assertCount(1, $result);
|
|
}
|
|
|
|
public function test_get_gas_prices_returns_collection(): void
|
|
{
|
|
$response = Mockery::mock();
|
|
$response->shouldReceive('throw')->once();
|
|
$response->shouldReceive('json')
|
|
->once()
|
|
->with('data', [])
|
|
->andReturn([
|
|
['start_date' => Carbon::now()->format('Y-m-d H:i:s'), 'total_price_tax_included' => 5.0],
|
|
]);
|
|
$response->shouldReceive('status')->once()->andReturn(200);
|
|
|
|
$http = Mockery::mock(Factory::class);
|
|
$http->shouldReceive('baseUrl')->once()->andReturnSelf();
|
|
$http->shouldReceive('withQueryParameters')->once()->andReturnSelf();
|
|
$http->shouldReceive('timeout')->once()->andReturnSelf();
|
|
$http->shouldReceive('retry')->once()->andReturnSelf();
|
|
$http->shouldReceive('get')->once()->andReturn($response);
|
|
|
|
$client = new ZonneplanPriceClient($http);
|
|
$result = $client->getGasPrices();
|
|
|
|
$this->assertCount(1, $result);
|
|
}
|
|
|
|
public function test_get_electricity_prices_for_date_returns_collection(): void
|
|
{
|
|
$date = Carbon::today();
|
|
$response = Mockery::mock();
|
|
$response->shouldReceive('throw')->once();
|
|
$response->shouldReceive('json')
|
|
->once()
|
|
->with('data', [])
|
|
->andReturn([
|
|
['start_date' => $date->format('Y-m-d H:i:s'), 'total_price_tax_included' => 15.0],
|
|
]);
|
|
$response->shouldReceive('status')->once()->andReturn(200);
|
|
|
|
$http = Mockery::mock(Factory::class);
|
|
$http->shouldReceive('baseUrl')->once()->andReturnSelf();
|
|
$http->shouldReceive('withQueryParameters')->once()->andReturnSelf();
|
|
$http->shouldReceive('timeout')->once()->andReturnSelf();
|
|
$http->shouldReceive('retry')->once()->andReturnSelf();
|
|
$http->shouldReceive('get')->once()->andReturn($response);
|
|
|
|
$client = new ZonneplanPriceClient($http);
|
|
$result = $client->getElectricityPricesForDate($date);
|
|
|
|
$this->assertCount(1, $result);
|
|
}
|
|
|
|
public function test_get_gas_prices_for_date_returns_collection(): void
|
|
{
|
|
$date = Carbon::today();
|
|
$response = Mockery::mock();
|
|
$response->shouldReceive('throw')->once();
|
|
$response->shouldReceive('json')
|
|
->once()
|
|
->with('data', [])
|
|
->andReturn([
|
|
['start_date' => $date->format('Y-m-d H:i:s'), 'total_price_tax_included' => 5.0],
|
|
]);
|
|
$response->shouldReceive('status')->once()->andReturn(200);
|
|
|
|
$http = Mockery::mock(Factory::class);
|
|
$http->shouldReceive('baseUrl')->once()->andReturnSelf();
|
|
$http->shouldReceive('withQueryParameters')->once()->andReturnSelf();
|
|
$http->shouldReceive('timeout')->once()->andReturnSelf();
|
|
$http->shouldReceive('retry')->once()->andReturnSelf();
|
|
$http->shouldReceive('get')->once()->andReturn($response);
|
|
|
|
$client = new ZonneplanPriceClient($http);
|
|
$result = $client->getGasPricesForDate($date);
|
|
|
|
$this->assertCount(1, $result);
|
|
}
|
|
|
|
public function test_client_throws_exception_on_api_failure(): void
|
|
{
|
|
$this->expectException(\Exception::class);
|
|
$this->expectExceptionMessage('Kon energieprijzen niet ophalen');
|
|
|
|
$response = Mockery::mock();
|
|
$response->shouldReceive('throw')->once()->andThrow(
|
|
new \Illuminate\Http\Client\ConnectionException('API Error')
|
|
);
|
|
|
|
$http = Mockery::mock(Factory::class);
|
|
$http->shouldReceive('baseUrl')->once()->andReturnSelf();
|
|
$http->shouldReceive('withQueryParameters')->once()->andReturnSelf();
|
|
$http->shouldReceive('timeout')->once()->andReturnSelf();
|
|
$http->shouldReceive('retry')->once()->andReturnSelf();
|
|
$http->shouldReceive('get')->once()->andReturn($response);
|
|
|
|
$client = new ZonneplanPriceClient($http);
|
|
$client->getElectricityPrices();
|
|
}
|
|
}
|