DateBasedInvoiceStorageTest.php
Pfad: tests/Rechnung/DateBasedInvoiceStorageTest.php
Ext: php
Größe: 1953 Bytes
Geändert: 2026-07-16 16:57:10+02
<?php
declare(strict_types=1);
namespace Tests\Rechnung;
use Demo\Infrastructure\Rechnung\DateBasedInvoiceStorage;
use PHPUnit\Framework\TestCase;
use RuntimeException;
/**
* Prueft PDF-Integritaet und Zielkollisionen.
*/
final class DateBasedInvoiceStorageTest extends TestCase
{
private string $directory;
/** Erzeugt ein isoliertes Ablageverzeichnis. */
protected function setUp(): void { $this->directory = sys_get_temp_dir() . '/invoice-storage-' . bin2hex(random_bytes(8)); }
/** Entfernt die erzeugte Testdatei und Verzeichnisse. */
protected function tearDown(): void
{
$files = glob($this->directory . '/*/*/*.pdf') ?: [];
foreach ($files as $file) { @unlink($file); }
foreach (array_reverse(glob($this->directory . '/*/*', GLOB_ONLYDIR) ?: []) as $dir) { @rmdir($dir); }
foreach (array_reverse(glob($this->directory . '/*', GLOB_ONLYDIR) ?: []) as $dir) { @rmdir($dir); }
@rmdir($this->directory);
}
/** Identischer Inhalt ist idempotent, abweichender Inhalt wird blockiert. */
public function testHashkollisionWirdNichtUeberschrieben(): void
{
$storage = new DateBasedInvoiceStorage($this->directory);
$path = $storage->buildTargetPath('2026-07-04', 'WEtell GmbH', 'source');
$first = '%PDF-' . str_repeat('A', 100);
$storage->persistDownloadedPdf($first, $path);
$storage->persistDownloadedPdf($first, $path);
$this->expectException(RuntimeException::class);
$storage->persistDownloadedPdf('%PDF-' . str_repeat('B', 100), $path);
}
/** HTML mit PDF-Dateiname wird abgewiesen. */
public function testNichtPdfWirdAbgewiesen(): void
{
$storage = new DateBasedInvoiceStorage($this->directory);
$this->expectException(RuntimeException::class);
$storage->persistDownloadedPdf('<html>' . str_repeat('x', 100) . '</html>', $this->directory . '/fake.pdf');
}
}