64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Weerscherm\Tests;
|
|
|
|
use Weerscherm\WeatherSource;
|
|
|
|
/** Testdubbel: geeft per methode een array terug of gooit de ingestelde exception, en telt de aanroepen. */
|
|
final class FakeSource implements WeatherSource
|
|
{
|
|
/** @var array<string, array|\Throwable> */
|
|
public array $responses = [];
|
|
|
|
/** @var array<string, int> */
|
|
public array $calls = ['stations' => 0, 'measure' => 0, 'pressure' => 0, 'forecast' => 0];
|
|
|
|
/** @var array<string, array> */
|
|
public array $lastArgs = [];
|
|
|
|
public static function fromFixtures(): self
|
|
{
|
|
$s = new self();
|
|
$s->responses = [
|
|
'stations' => json_decode((string) file_get_contents(__DIR__ . '/fixtures/getstationsdata.json'), true)['body'],
|
|
'measure' => json_decode((string) file_get_contents(__DIR__ . '/fixtures/getmeasure.json'), true)['body'],
|
|
'pressure' => json_decode((string) file_get_contents(__DIR__ . '/fixtures/getmeasure-pressure.json'), true)['body'],
|
|
'forecast' => json_decode((string) file_get_contents(__DIR__ . '/fixtures/forecast.json'), true)['daily'],
|
|
];
|
|
return $s;
|
|
}
|
|
|
|
public function stationsData(): array
|
|
{
|
|
return $this->answer('stations', []);
|
|
}
|
|
|
|
public function measure(string $deviceId, string $moduleId, int $begin, int $end): array
|
|
{
|
|
return $this->answer('measure', func_get_args());
|
|
}
|
|
|
|
public function pressureHistory(string $deviceId, int $begin, int $end): array
|
|
{
|
|
return $this->answer('pressure', func_get_args());
|
|
}
|
|
|
|
public function forecast(float $lat, float $lon, string $timezone): array
|
|
{
|
|
return $this->answer('forecast', func_get_args());
|
|
}
|
|
|
|
private function answer(string $key, array $args): array
|
|
{
|
|
$this->calls[$key]++;
|
|
$this->lastArgs[$key] = $args;
|
|
$r = $this->responses[$key] ?? new \RuntimeException("geen response voor $key");
|
|
if ($r instanceof \Throwable) {
|
|
throw $r;
|
|
}
|
|
return $r;
|
|
}
|
|
}
|