netgescon-day0/tests/Unit/AmministratoreArchiveSyncTest.php

66 lines
2.1 KiB
PHP
Executable File

<?php
use App\Models\Amministratore;
use App\Services\AmministratoreArchiveSyncService;
uses(Tests\TestCase::class);
function cleanupDirectory(string $path): void
{
if (!is_dir($path)) {
return;
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $item) {
if ($item->isDir()) {
@rmdir($item->getPathname());
} else {
@unlink($item->getPathname());
}
}
@rmdir($path);
}
afterEach(function (): void {
\Mockery::close();
});
test('legacy archive is copied into amministratore storage', function () {
$service = app(AmministratoreArchiveSyncService::class);
$legacyRoot = storage_path('app/testing/legacy_sources');
$legacyCode = '0010';
$sourcePath = $legacyRoot . DIRECTORY_SEPARATOR . $legacyCode;
cleanupDirectory($legacyRoot);
mkdir($sourcePath, 0755, true);
file_put_contents($sourcePath . DIRECTORY_SEPARATOR . 'singolo_anno.mdb', 'dummy');
file_put_contents($sourcePath . DIRECTORY_SEPARATOR . 'info.txt', 'hello world');
$archiveBase = storage_path('app/testing/amministratori/TESTADM');
cleanupDirectory($archiveBase);
$amministratore = \Mockery::mock(Amministratore::class);
$amministratore->shouldReceive('provisionArchiveIfMissing')->once();
$amministratore->shouldReceive('getArchivePath')->andReturn($archiveBase);
$result = $service->sync($amministratore, $legacyCode, $legacyRoot, true);
$targetPath = $archiveBase . DIRECTORY_SEPARATOR . 'legacy' . DIRECTORY_SEPARATOR . $legacyCode;
$targetFile = $targetPath . DIRECTORY_SEPARATOR . 'info.txt';
expect(is_dir($targetPath))->toBeTrue();
expect(is_file($targetFile))->toBeTrue();
expect(trim((string) file_get_contents($targetFile)))->toBe('hello world');
expect($result['legacy_code'])->toBe($legacyCode);
expect($result['files_count'])->toBeGreaterThan(0);
cleanupDirectory($legacyRoot);
cleanupDirectory($archiveBase);
});