OpenNIT

Quelltext des aktuellen Standes

Fassung 1.1.1

AssetManagerTest.php

2 KB
<?php

declare(strict_types=1);

use OpenNIT\Assets\AssetManager;

/**
 * Deckt die (DB-freie) reine Logik der aus dem AssetController extrahierten
 * Datenschicht ab: die Standort-Pfad-Berechnung.
 */
return static function (): void {

    $ref = new ReflectionClass(AssetManager::class);
    /** @var AssetManager $mgr */
    $mgr = $ref->newInstanceWithoutConstructor(); // buildLocationPaths nutzt kein $this->db

    $locations = [
        ['id' => 1, 'name' => 'HQ',        'parent_id' => null],
        ['id' => 2, 'name' => 'Etage 1',   'parent_id' => 1],
        ['id' => 3, 'name' => 'Raum 101',  'parent_id' => 2],
        ['id' => 4, 'name' => 'Aussenlager','parent_id' => null],
    ];

    test('buildLocationPaths bildet vollständige Pfade', function () use ($mgr, $locations) {
        $paths = $mgr->buildLocationPaths($locations);
        $byId = [];
        foreach ($paths as $p) { $byId[(int)$p['id']] = $p['full_path']; }
        assertSame('HQ', $byId[1]);
        assertSame('HQ › Etage 1', $byId[2]);
        assertSame('HQ › Etage 1 › Raum 101', $byId[3]);
        assertSame('Aussenlager', $byId[4]);
    });

    test('buildLocationPaths ist alphabetisch nach Pfad sortiert', function () use ($mgr, $locations) {
        $paths = $mgr->buildLocationPaths($locations);
        $order = array_map(static fn($p) => $p['full_path'], $paths);
        $sorted = $order;
        sort($sorted, SORT_STRING);
        assertSame($sorted, $order);
    });

    test('buildLocationPaths filtert nach Teilstring (case-insensitive)', function () use ($mgr, $locations) {
        $paths = $mgr->buildLocationPaths($locations, 'raum 101');
        assertSame(1, count($paths));
        assertSame('HQ › Etage 1 › Raum 101', $paths[0]['full_path']);
    });

    test('buildLocationPaths bricht bei zyklischen parent_id nicht ab', function () use ($mgr) {
        $cyclic = [
            ['id' => 1, 'name' => 'A', 'parent_id' => 2],
            ['id' => 2, 'name' => 'B', 'parent_id' => 1],
        ];
        $paths = $mgr->buildLocationPaths($cyclic);
        assertSame(2, count($paths)); // kein Endlos-Loop
    });
};