Steuerkram/tests/43_KiConnectionFallbackTest.php

54 lines
2.2 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\Database;
use Steuerkram\Models\Beleg;
class KiConnectionFallbackTest extends TestCase
{
protected string $testName = 'Test 43: KI-Verbindungsfehler-Fallback';
public function run(): void
{
$belegId = $this->createTestBeleg();
// [] = "verarbeitet, kein Ergebnis" – verhindert endlose Auto-Retry-Schleife
Beleg::saveKiVorschlag($belegId, []);
$beleg = Beleg::find($belegId);
$this->assertNotEmpty($beleg['ki_vorschlag'], 'Leeres Array muss als JSON gespeichert werden, nicht als SQL NULL');
$decoded = json_decode($beleg['ki_vorschlag'], true);
$this->assert(is_array($decoded) && count($decoded) === 0, 'Gespeicherter Wert muss leeres Array sein');
// null = "noch nicht verarbeitet" – löst Auto-Retry auf der Beleg-Seite aus
Beleg::saveKiVorschlag($belegId, null);
$beleg = Beleg::find($belegId);
$this->assert($beleg['ki_vorschlag'] === null, 'saveKiVorschlag(null) muss SQL NULL ergeben');
// Nicht-leeres Array wird korrekt gespeichert und zurückgelesen
$vorschlag = [[
'buchungstyp' => 'Ausgabe',
'kategorie_konto_id' => 5,
'zahlungskonto_id' => 2,
'ust_satz' => 19,
'betrag_netto' => 99.00,
'buchungstext' => 'Fallback-Test',
]];
Beleg::saveKiVorschlag($belegId, $vorschlag);
$beleg = Beleg::find($belegId);
$decoded = json_decode($beleg['ki_vorschlag'], true);
$this->assert(is_array($decoded) && count($decoded) === 1, 'Vorschlag-Array korrekt gespeichert');
$this->assertEquals('Ausgabe', $decoded[0]['buchungstyp'], 'buchungstyp korrekt');
}
private function createTestBeleg(): int
{
Database::execute(
"INSERT INTO belege (beleg_nr, filename, storage_path, mime_type, file_hash, belegtyp)
VALUES ('TEST-KI-FALLBACK-001', 'test.pdf', 'static/uploads/test/test.pdf', 'application/pdf',
SHA2('fallback-test', 256), 'Eingangsrechnung')"
);
return (int)Database::lastInsertId();
}
}