Skip to content

Commit

Permalink
Use Guzzle and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Aug 26, 2024
1 parent 21afdef commit 6c7038e
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 27 deletions.
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
],
"require": {
"php": "^8.1",
"guzzlehttp/guzzle": "^7.9",
"illuminate/http": "^11.21",
"illuminate/support": "^11.21"
"guzzlehttp/guzzle": "^7.9"
},
"require-dev": {
"pestphp/pest": "^2.0",
"laravel/pint": "^1.2",
"pestphp/pest": "^1.20",
"phpstan/phpstan": "^1.11",
"spatie/ray": "^1.28"
"phpstan/phpstan": "^1.11"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ parameters:
level: 8
paths:
- src
- tests
tmpDir: build/phpstan
parallel:
maximumNumberOfProcesses: 1
41 changes: 22 additions & 19 deletions src/EnergyZero.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
namespace Baspa\EnergyZero;

use DateTime;
use DateTimeZone;
use Exception;
use DateTimeZone;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;

class EnergyZero
{
private bool $vat = true;

private int $requestTimeout = 10;

private string $baseUri = 'https://api.energyzero.nl/v1/';
private ClientInterface $client;

public function __construct(ClientInterface $client = null)
{
$this->client = $client ?? new Client([
'base_uri' => $this->baseUri,
'timeout' => $this->requestTimeout,
'headers' => [
'Accept' => 'application/json, text/plain',
'User-Agent' => 'PHPEnergyZero/1.0',
],
]);
}

/**
* @param array<string, mixed> $params
Expand All @@ -26,23 +37,15 @@ class EnergyZero
public function request(string $uri, array $params = []): ?array
{
try {
/** @var PendingRequest $request */
$request = Http::withHeaders([
'Accept' => 'application/json, text/plain',
'User-Agent' => 'PHPEnergyZero/1.0',
]);

$response = $request->timeout($this->requestTimeout)
->get($this->baseUri.$uri, $params);
$response = $this->client->request('GET', $uri, ['query' => $params]);

if ($response->successful()) {
return $response->json();
if ($response->getStatusCode() === 200) {
return json_decode($response->getBody()->getContents(), true);
} else {
throw new Exception('Unexpected response status: '.$response->status());
throw new Exception('Unexpected response status: ' . $response->getStatusCode());
}
} catch (RequestException $e) {
error_log('Error: '.$e->getMessage());

error_log('Error: ' . $e->getMessage());
return null;
}
}
Expand Down Expand Up @@ -189,4 +192,4 @@ public function getValleyHours(string $startDate, string $endDate, int $topN = 5

return array_slice($prices, 0, $topN);
}
}
}
175 changes: 174 additions & 1 deletion tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1 +1,174 @@
<?php
<?php

use Baspa\EnergyZero\EnergyZero;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;

beforeEach(function () {
$this->mockHandler = new MockHandler();
$handlerStack = HandlerStack::create($this->mockHandler);
$client = new Client(['handler' => $handlerStack]);
$this->energyZero = new EnergyZero($client);
});

it('can make a request', function () {
$this->mockHandler->append(new Response(200, [], json_encode(['data' => 'test'])));

$result = $this->energyZero->request('test');

expect($result)->toBe(['data' => 'test']);
});

it('handles failed requests', function () {
$this->mockHandler->append(new Response(500));

$result = $this->energyZero->request('test');

expect($result)->toBeNull();
});

it('can get energy prices', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
],
'average' => 0.15,
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->energyPrices('2023-05-01', '2023-05-02');

expect($result)->toBe($mockData);
});

it('can get average price for period', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
],
'average' => 0.15,
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->getAveragePriceForPeriod('2023-05-01', '2023-05-02');

expect($result)->toBe(0.15);
});

it('can get lowest price for period', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
],
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->getLowestPriceForPeriod('2023-05-01', '2023-05-02');

expect($result)->toBe([
'price' => 0.1,
'datetime' => '2023-05-01T00:00:00',
]);
});

it('can get highest price for period', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
],
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->getHighestPriceForPeriod('2023-05-01', '2023-05-02');

expect($result)->toBe([
'price' => 0.2,
'datetime' => '2023-05-01T01:00:00',
]);
});

it('can get prices above threshold', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
['readingDate' => '2023-05-01T02:00:00', 'price' => 0.3],
],
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->getPricesAboveThreshold('2023-05-01', '2023-05-02', 0.15);

expect($result)->toBe([
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
['readingDate' => '2023-05-01T02:00:00', 'price' => 0.3],
]);
});

it('can get prices below threshold', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
['readingDate' => '2023-05-01T02:00:00', 'price' => 0.3],
],
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->getPricesBelowThreshold('2023-05-01', '2023-05-02', 0.25);

expect($result)->toBe([
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.2],
]);
});

it('can get peak hours', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.3],
['readingDate' => '2023-05-01T02:00:00', 'price' => 0.2],
],
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->getPeakHours('2023-05-01', '2023-05-02', 2);

expect($result)->toBe([
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.3],
['readingDate' => '2023-05-01T02:00:00', 'price' => 0.2],
]);
});

it('can get valley hours', function () {
$mockData = [
'Prices' => [
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T01:00:00', 'price' => 0.3],
['readingDate' => '2023-05-01T02:00:00', 'price' => 0.2],
],
];

$this->mockHandler->append(new Response(200, [], json_encode($mockData)));

$result = $this->energyZero->getValleyHours('2023-05-01', '2023-05-02', 2);

expect($result)->toBe([
['readingDate' => '2023-05-01T00:00:00', 'price' => 0.1],
['readingDate' => '2023-05-01T02:00:00', 'price' => 0.2],
]);
});

0 comments on commit 6c7038e

Please sign in to comment.