Steuerkram/tests/57_BankSourceLastBalanceTest.php

61 lines
2.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
use Steuerkram\Config;
use Steuerkram\Models\BankSource;
/**
* Test 57: BankSource::updateLastFetched persistiert optional den beim Abruf
* ermittelten Kontostand (last_balance) und lässt ihn unangetastet, wenn kein
* neuer Kontostand geliefert wurde.
*/
class BankSourceLastBalanceTest extends TestCase
{
protected string $testName = 'Test 57: BankSource – Kontostand nach Direktabruf';
public function run(): void
{
$oldKey = $_ENV['APP_KEY'] ?? null;
if (empty(Config::get('app_key', ''))) {
$_ENV['APP_KEY'] = str_repeat('c3', 32);
Config::load();
}
try {
$id = BankSource::create([
'type' => 'fints',
'bezeichnung' => 'TestFinTS-Balance',
'blz' => '12030000',
'benutzerkennung' => 'kundennummer',
'pin' => 'g3h31m',
]);
$this->assertGreaterThan(0, $id, 'FinTS-Quelle wurde angelegt');
$fresh = BankSource::find($id);
$this->assert($fresh['last_balance'] === null, 'Kontostand ist vor dem ersten Abruf leer');
BankSource::updateLastFetched($id, 1234.56);
$after1 = BankSource::find($id);
$this->assertNotEmpty($after1['last_fetched_at'], 'last_fetched_at wurde gesetzt');
$this->assertEquals(1234.56, (float)$after1['last_balance'], 'Kontostand wurde übernommen');
// Ein Abruf ohne ermittelten Kontostand (z.B. Fehler bei der Kontoabfrage)
// darf den zuletzt bekannten Stand nicht löschen.
BankSource::updateLastFetched($id, null);
$after2 = BankSource::find($id);
$this->assertEquals(1234.56, (float)$after2['last_balance'], 'Kontostand bleibt bei fehlendem neuen Wert erhalten');
BankSource::updateLastFetched($id, -42.10);
$after3 = BankSource::find($id);
$this->assertEquals(-42.10, (float)$after3['last_balance'], 'Negativer Kontostand (Dispo) wird korrekt übernommen');
} finally {
if ($oldKey === null) {
unset($_ENV['APP_KEY']);
} else {
$_ENV['APP_KEY'] = $oldKey;
}
Config::load();
}
}
}