chore: complete update
This commit is contained in:
121
tests/Framework/Filesystem/LazyLoadingTest.php
Normal file
121
tests/Framework/Filesystem/LazyLoadingTest.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Framework\Filesystem;
|
||||
|
||||
use App\Framework\Filesystem\File;
|
||||
use App\Framework\Filesystem\Directory;
|
||||
use App\Framework\Filesystem\FilesystemFactory;
|
||||
use App\Framework\Filesystem\InMemoryStorage;
|
||||
use App\Framework\Filesystem\FileStorage;
|
||||
|
||||
it('lädt File-Properties erst bei Bedarf', function() {
|
||||
// Test-Storage mit Instrumentierung
|
||||
$storage = new class extends InMemoryStorage {
|
||||
public array $accessed = [];
|
||||
|
||||
public function get(string $path): string {
|
||||
$this->accessed[] = "get:{$path}";
|
||||
return parent::get($path);
|
||||
}
|
||||
|
||||
public function size(string $path): int {
|
||||
$this->accessed[] = "size:{$path}";
|
||||
return parent::size($path);
|
||||
}
|
||||
};
|
||||
|
||||
// Testdatei hinzufügen
|
||||
$storage->addFile('/test.txt', 'Testinhalt');
|
||||
|
||||
// Lazy File erstellen
|
||||
$file = $storage->file('/test.txt');
|
||||
|
||||
// Sollte noch keine Storage-Methoden aufgerufen haben
|
||||
expect($storage->accessed)->toBeEmpty();
|
||||
|
||||
// Zugriff auf path sollte kein Laden auslösen
|
||||
$path = $file->path;
|
||||
expect($storage->accessed)->toBeEmpty();
|
||||
|
||||
// Zugriff auf contents sollte get() auslösen
|
||||
$contents = $file->contents;
|
||||
expect($storage->accessed)->toContain('get:/test.txt');
|
||||
expect($contents)->toBe('Testinhalt');
|
||||
|
||||
// Zugriff auf size sollte size() auslösen
|
||||
$size = $file->size;
|
||||
expect($storage->accessed)->toContain('size:/test.txt');
|
||||
expect($size)->toBe(10); // Länge von 'Testinhalt'
|
||||
});
|
||||
|
||||
it('lädt Directory-Properties erst bei Bedarf', function() {
|
||||
// Test-Storage mit Instrumentierung
|
||||
$storage = new class extends InMemoryStorage {
|
||||
public array $accessed = [];
|
||||
|
||||
public function listDirectory(string $directory): array {
|
||||
$this->accessed[] = "list:{$directory}";
|
||||
return parent::listDirectory($directory);
|
||||
}
|
||||
};
|
||||
|
||||
// Testverzeichnis erstellen
|
||||
$storage->createDirectory('/test-dir');
|
||||
$storage->addFile('/test-dir/file1.txt', 'Datei 1');
|
||||
$storage->addFile('/test-dir/file2.txt', 'Datei 2');
|
||||
|
||||
// Lazy Directory erstellen
|
||||
$dir = $storage->directory('/test-dir');
|
||||
|
||||
// Sollte noch keine Storage-Methoden aufgerufen haben
|
||||
expect($storage->accessed)->toBeEmpty();
|
||||
|
||||
// Zugriff auf path sollte kein Laden auslösen
|
||||
$path = $dir->path;
|
||||
expect($storage->accessed)->toBeEmpty();
|
||||
|
||||
// Zugriff auf contents sollte listDirectory() auslösen
|
||||
$contents = $dir->contents;
|
||||
expect($storage->accessed)->toContain('list:/test-dir');
|
||||
expect($contents)->toHaveCount(2);
|
||||
});
|
||||
|
||||
it('kann mit echtem FileStorage arbeiten', function() {
|
||||
// Dieser Test kann übersprungen werden, wenn keine Schreibrechte im Temp-Verzeichnis vorhanden sind
|
||||
$tempDir = sys_get_temp_dir() . '/php-lazy-test-' . uniqid();
|
||||
@mkdir($tempDir, 0777, true);
|
||||
if (!is_dir($tempDir) || !is_writable($tempDir)) {
|
||||
$this->markTestSkipped('Kein Schreibzugriff im Temp-Verzeichnis');
|
||||
}
|
||||
|
||||
try {
|
||||
// Echten FileStorage verwenden
|
||||
$storage = new FileStorage();
|
||||
|
||||
// Testdatei erstellen
|
||||
$testFile = $tempDir . '/test.txt';
|
||||
file_put_contents($testFile, 'Lazy Loading Test');
|
||||
|
||||
// Lazy File erstellen
|
||||
$file = $storage->file($testFile);
|
||||
|
||||
// Properties testen
|
||||
expect($file->path)->toBe($testFile);
|
||||
expect($file->contents)->toBe('Lazy Loading Test');
|
||||
expect($file->size)->toBe(17); // Länge von 'Lazy Loading Test'
|
||||
expect($file->lastModified)->toBeGreaterThan(time() - 10);
|
||||
|
||||
// Directory testen
|
||||
$dir = $storage->directory($tempDir);
|
||||
expect($dir->exists())->toBeTrue();
|
||||
|
||||
$files = $dir->getFiles();
|
||||
expect($files)->toHaveCount(1);
|
||||
expect($files[0]->path)->toEndWith('/test.txt');
|
||||
} finally {
|
||||
// Aufräumen
|
||||
@unlink($testFile);
|
||||
@rmdir($tempDir);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user