generated from vormkracht10/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
199 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ parameters: | |
level: 8 | ||
paths: | ||
- src | ||
- tests | ||
tmpDir: build/phpstan | ||
parallel: | ||
maximumNumberOfProcesses: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]); | ||
}); |