feat: project skeleton with Config and exceptions

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-09-11 14:34:39 +02:00
co-authored by Claude Fable 5.1
parent 0bf3527115
commit b38b7a3ac4
11 changed files with 2007 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
{"version":2,"defects":[],"times":{"Weerscherm\\Tests\\ConfigTest::testDefaultsApplyWhenKeysAreMissing":0.001,"Weerscherm\\Tests\\ConfigTest::testFileValuesOverrideDefaults":0,"Weerscherm\\Tests\\ConfigTest::testUnknownKeyReturnsGivenDefault":0,"Weerscherm\\Tests\\ConfigTest::testFileMustReturnArray":0}}
+26
View File
@@ -0,0 +1,26 @@
{
"name": "pensare/weerscherm",
"description": "Weerscherm voor een iPad mini: Netatmo-stationsdata en Open-Meteo-verwachting",
"type": "project",
"license": "proprietary",
"require": {
"php": ">=8.3",
"ext-curl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^11.5"
},
"autoload": {
"psr-4": { "Weerscherm\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Weerscherm\\Tests\\": "tests/" }
},
"scripts": {
"test": "phpunit"
},
"config": {
"sort-packages": true
}
}
Generated
+1787
View File
File diff suppressed because it is too large Load Diff
+38
View File
@@ -0,0 +1,38 @@
<?php
/**
* Kopieer dit bestand naar config.php en vul de waarden in.
* config.php staat in .gitignore en mag nooit in git komen.
*/
return [
// 'live' haalt data op bij Netatmo en Open-Meteo, 'fixture' levert vaste voorbeelddata.
'data_source' => 'fixture',
// Uit de app op dev.netatmo.com.
'netatmo_client_id' => '',
'netatmo_client_secret' => '',
// Optioneel: het _id (MAC-adres) van het basisstation. Leeg = eerste station.
'netatmo_device_id' => null,
// Fallback als het station geen tijdzone meldt.
'timezone' => 'Europe/Berlin',
// Optioneel: locatie voor de verwachting. Leeg = locatie van het station.
'forecast_lat' => null,
'forecast_lon' => null,
// Optioneel: volgorde en labels van de binnenruimtes, op basis van het _id van de module.
// Voorbeeld: ['70:ee:50:00:00:01' => 'Woonkamer', '03:00:00:00:00:02' => 'Slaapkamer']
'indoor_modules' => [],
// Cache per bron, in seconden.
'cache_ttl' => [
'stations' => 300,
'measure' => 900,
'forecast' => 3600,
],
// Map voor tokens en cache. Moet schrijfbaar zijn voor de webserver en buiten de docroot liggen.
'storage_path' => __DIR__ . '/storage',
];
@@ -57,7 +57,7 @@ src/
RainCalculator.php # getmeasure-body -> today/previous_days/month_total
ForecastMapper.php # open-meteo daily -> 5 dagen
DashboardService.php # orkestratie, cache, stale-fallback, errors
Exceptions.php # UpstreamException, NetatmoAuthException
UpstreamException.php, NetatmoAuthException.php
bin/netatmo-init.php # slaat refresh token op in storage/netatmo-token.json
tests/
bootstrap.php
+18
View File
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true">
<testsuites>
<testsuite name="weerscherm">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
+52
View File
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Weerscherm;
final class Config
{
private const DEFAULTS = [
'data_source' => 'fixture',
'netatmo_client_id' => '',
'netatmo_client_secret' => '',
'netatmo_device_id' => null,
'timezone' => 'Europe/Berlin',
'forecast_lat' => null,
'forecast_lon' => null,
'indoor_modules' => [],
'cache_ttl' => [
'stations' => 300,
'measure' => 900,
'forecast' => 3600,
],
'storage_path' => null,
];
/** @param array<string, mixed> $values */
private function __construct(private readonly array $values)
{
}
public static function load(string $file): self
{
if (!is_file($file)) {
throw new \RuntimeException("Configuratiebestand ontbreekt: $file");
}
$raw = require $file;
if (!is_array($raw)) {
throw new \RuntimeException("Configuratiebestand moet een array teruggeven: $file");
}
$values = array_replace(self::DEFAULTS, $raw);
$values['cache_ttl'] = array_replace(self::DEFAULTS['cache_ttl'], is_array($raw['cache_ttl'] ?? null) ? $raw['cache_ttl'] : []);
$values['storage_path'] ??= dirname($file) . '/storage';
return new self($values);
}
public function get(string $key, mixed $default = null): mixed
{
return array_key_exists($key, $this->values) ? $this->values[$key] : $default;
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Weerscherm;
/** De Netatmo-koppeling is verlopen: de refresh token is ongeldig. Herstel via bin/netatmo-init.php. */
final class NetatmoAuthException extends UpstreamException
{
}
+10
View File
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Weerscherm;
/** Een upstream-bron (Netatmo, Open-Meteo) is niet bereikbaar of gaf een onbruikbaar antwoord. */
class UpstreamException extends \RuntimeException
{
}
+57
View File
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Weerscherm\Tests;
use PHPUnit\Framework\TestCase;
use Weerscherm\Config;
final class ConfigTest extends TestCase
{
private string $file;
protected function setUp(): void
{
$this->file = tempnam(sys_get_temp_dir(), 'cfg');
}
protected function tearDown(): void
{
@unlink($this->file);
}
public function testDefaultsApplyWhenKeysAreMissing(): void
{
file_put_contents($this->file, '<?php return [];');
$config = Config::load($this->file);
self::assertSame('Europe/Berlin', $config->get('timezone'));
self::assertSame('fixture', $config->get('data_source'));
self::assertSame(['stations' => 300, 'measure' => 900, 'forecast' => 3600], $config->get('cache_ttl'));
self::assertSame([], $config->get('indoor_modules'));
self::assertNull($config->get('netatmo_device_id'));
}
public function testFileValuesOverrideDefaults(): void
{
file_put_contents($this->file, '<?php return ["timezone" => "Europe/Amsterdam", "cache_ttl" => ["stations" => 60]];');
$config = Config::load($this->file);
self::assertSame('Europe/Amsterdam', $config->get('timezone'));
self::assertSame(['stations' => 60, 'measure' => 900, 'forecast' => 3600], $config->get('cache_ttl'));
}
public function testUnknownKeyReturnsGivenDefault(): void
{
file_put_contents($this->file, '<?php return [];');
self::assertSame('x', Config::load($this->file)->get('nope', 'x'));
}
public function testFileMustReturnArray(): void
{
file_put_contents($this->file, '<?php return "nee";');
$this->expectException(\RuntimeException::class);
Config::load($this->file);
}
}
+7
View File
@@ -0,0 +1,7 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
date_default_timezone_set('UTC');