131 lines
4.7 KiB
PHP
131 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace UbsCsvTransformer\Tests;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use UbsCsvTransformer\ConfigurationLoader;
|
|
use UbsCsvTransformer\TransformerEngine;
|
|
|
|
/**
|
|
* Integrations-Tests für Konfigurationsdateien
|
|
*
|
|
* Prüft, dass jede Konfigurationsdatei mit ihrem Fixture-Input
|
|
* die erwartete CSV-Ausgabe produziert (Golden-File-Test).
|
|
*
|
|
* Fixtures liegen unter tests/fixtures/<config-name>/:
|
|
* - input.csv → minimaler CSV-Input passend zur Konfiguration
|
|
* - expected.csv → erwartete Ausgabe nach Transformation
|
|
*
|
|
* Neue Fixtures hinzufügen:
|
|
* 1. Verzeichnis tests/fixtures/<config-name>/ anlegen
|
|
* 2. input.csv und expected.csv ablegen
|
|
* 3. sicherstellen dass config/<config-name>.json existiert
|
|
*/
|
|
class ConfigIntegrationTest extends TestCase
|
|
{
|
|
/** @var string Temporäres Output-Verzeichnis für diesen Test-Lauf */
|
|
private string $tempOutputDir = '';
|
|
|
|
/** @var string Temporäre Konfigurationsdatei mit überschriebenem Output-Pfad */
|
|
private string $tempConfigFile = '';
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->tempOutputDir = sys_get_temp_dir() . '/ubscsv_test_' . uniqid('', true);
|
|
mkdir($this->tempOutputDir, 0755, true);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if ($this->tempOutputDir !== '' && is_dir($this->tempOutputDir)) {
|
|
foreach (glob($this->tempOutputDir . '/*') ?: [] as $file) {
|
|
unlink($file);
|
|
}
|
|
rmdir($this->tempOutputDir);
|
|
}
|
|
|
|
if ($this->tempConfigFile !== '' && file_exists($this->tempConfigFile)) {
|
|
unlink($this->tempConfigFile);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Liefert alle Fixture-Paare als DataProvider
|
|
*
|
|
* Entdeckt automatisch alle Unterverzeichnisse in tests/fixtures/
|
|
* und erwartet für jedes eine passende config/<name>.json.
|
|
*
|
|
* @return array<string, array{configName: string, fixtureDir: string}>
|
|
*/
|
|
public static function fixtureProvider(): array
|
|
{
|
|
$fixtureBase = __DIR__ . '/fixtures';
|
|
$dirs = glob($fixtureBase . '/*', GLOB_ONLYDIR);
|
|
$cases = [];
|
|
|
|
foreach ($dirs ?: [] as $dir) {
|
|
$configName = basename($dir);
|
|
$cases[$configName] = [
|
|
'configName' => $configName,
|
|
'fixtureDir' => $dir,
|
|
];
|
|
}
|
|
|
|
return $cases;
|
|
}
|
|
|
|
/**
|
|
* Prüft dass eine Konfigurationsdatei mit dem Fixture-Input die erwartete Ausgabe erzeugt
|
|
*
|
|
* @dataProvider fixtureProvider
|
|
*/
|
|
public function testConfigProducesExpectedOutput(string $configName, string $fixtureDir): void
|
|
{
|
|
$realConfigPath = __DIR__ . '/../config/' . $configName . '.json';
|
|
$inputFile = $fixtureDir . '/input.csv';
|
|
$expectedFile = $fixtureDir . '/expected.csv';
|
|
|
|
$this->assertFileExists($realConfigPath, "Config-Datei nicht gefunden: {$realConfigPath}");
|
|
$this->assertFileExists($inputFile, "Fixture input.csv fehlt: {$inputFile}");
|
|
$this->assertFileExists($expectedFile, "Fixture expected.csv fehlt: {$expectedFile}");
|
|
|
|
// Temporäre Konfiguration mit überschriebenem Output-Verzeichnis erstellen
|
|
$rawConfig = file_get_contents($realConfigPath);
|
|
$this->assertNotFalse($rawConfig, "Konfigurationsdatei konnte nicht gelesen werden");
|
|
|
|
/** @var array<string, mixed> $configData */
|
|
$configData = json_decode((string) $rawConfig, true);
|
|
$this->assertIsArray($configData, "Ungültiges JSON in Konfigurationsdatei");
|
|
|
|
$configData['directories']['output'] = $this->tempOutputDir;
|
|
|
|
$this->tempConfigFile = sys_get_temp_dir() . '/ubscsv_config_' . uniqid('', true) . '.json';
|
|
file_put_contents($this->tempConfigFile, json_encode($configData, JSON_UNESCAPED_UNICODE));
|
|
|
|
// Transformation ausführen
|
|
$loader = new ConfigurationLoader($this->tempConfigFile);
|
|
$loader->load();
|
|
$engine = new TransformerEngine($loader);
|
|
$result = $engine->transform($inputFile);
|
|
|
|
$this->assertTrue(
|
|
$result['success'],
|
|
"Transformation fehlgeschlagen: " . ($result['error'] ?? 'unbekannter Fehler')
|
|
);
|
|
|
|
$outputFile = $result['outputFile'];
|
|
$this->assertFileExists($outputFile, "Output-Datei wurde nicht erstellt: {$outputFile}");
|
|
|
|
$actual = rtrim((string) file_get_contents($outputFile));
|
|
$expected = rtrim((string) file_get_contents($expectedFile));
|
|
|
|
$this->assertSame(
|
|
$expected,
|
|
$actual,
|
|
"CSV-Output stimmt nicht mit expected.csv überein.\n" .
|
|
"Zum Aktualisieren: php bin/transformer.php transform {$inputFile} {$realConfigPath} " .
|
|
"(Output nach {$expectedFile} kopieren)"
|
|
);
|
|
}
|
|
}
|