268 lines
9.9 KiB
PHP
268 lines
9.9 KiB
PHP
<?php
|
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
beforeEach(function (): void {
|
|
$this->localRoot = storage_path('framework/testing/ftp-deploy-local');
|
|
$this->statePath = storage_path('framework/testing/ftp-deploy-state.json');
|
|
|
|
File::deleteDirectory($this->localRoot);
|
|
File::ensureDirectoryExists($this->localRoot);
|
|
File::delete($this->statePath);
|
|
|
|
config([
|
|
'services.ftp_deploy.disk' => 'ftp_deploy',
|
|
'services.ftp_deploy.local_root' => $this->localRoot,
|
|
'services.ftp_deploy.state_path' => 'storage/framework/testing/ftp-deploy-state.json',
|
|
'services.ftp_deploy.api_token' => 'secret-token',
|
|
'services.ftp_deploy.api_url' => 'https://example.test/ftp/generate-sync-state',
|
|
'services.ftp_deploy.api_timeout' => 10,
|
|
'services.ftp_deploy.exclude' => ['ignored', '*.tmp'],
|
|
]);
|
|
|
|
Storage::fake('ftp_deploy');
|
|
});
|
|
|
|
afterEach(function (): void {
|
|
File::deleteDirectory($this->localRoot);
|
|
File::delete($this->statePath);
|
|
});
|
|
|
|
test('package registers default config commands and route', function () {
|
|
expect(config('filesystems.disks.ftp_deploy.driver'))->toBe('ftp')
|
|
->and(config('services.ftp_deploy.disk'))->toBe('ftp_deploy')
|
|
->and(Artisan::all())->toHaveKeys([
|
|
'ftp:api-summary',
|
|
'ftp:deploy',
|
|
'ftp:generate-sync-state',
|
|
])
|
|
->and(Route::has('ftp-deploy.generate-sync-state'))->toBeTrue();
|
|
});
|
|
|
|
test('api summary command calls configured api and prints the returned summary', function () {
|
|
Http::fake([
|
|
'https://example.test/ftp/generate-sync-state' => Http::response([
|
|
'success' => true,
|
|
'path' => '/var/www/storage/app/.ftp-deploy-sync-state.json',
|
|
'files' => 12,
|
|
'folders' => 4,
|
|
'generated_time' => 1782361358583,
|
|
'state' => [
|
|
'generatedTime' => 1782361358583,
|
|
'data' => [],
|
|
],
|
|
]),
|
|
]);
|
|
|
|
$this->artisan('ftp:api-summary')
|
|
->expectsOutputToContain('Files')
|
|
->assertExitCode(0);
|
|
|
|
Http::assertSent(fn ($request): bool => $request->url() === 'https://example.test/ftp/generate-sync-state'
|
|
&& $request->hasHeader('Authorization', 'Bearer secret-token'));
|
|
});
|
|
|
|
test('api summary command fails when api returns an error status', function () {
|
|
Http::fake([
|
|
'https://example.test/ftp/generate-sync-state' => Http::response([
|
|
'message' => 'Invalid FTP deploy API token.',
|
|
], 403),
|
|
]);
|
|
|
|
$this->artisan('ftp:api-summary')
|
|
->assertExitCode(1);
|
|
});
|
|
|
|
test('generate sync state command writes state and honors excludes', function () {
|
|
ftpDeployWriteLocalFile($this->localRoot, 'keep.txt', 'keep contents');
|
|
ftpDeployWriteLocalFile($this->localRoot, 'ignored/file.txt', 'ignored contents');
|
|
ftpDeployWriteLocalFile($this->localRoot, 'nested/cache.tmp', 'ignored contents');
|
|
|
|
$outputPath = "{$this->localRoot}/storage/app/.ftp-deploy-sync-state.json";
|
|
|
|
$this->artisan('ftp:generate-sync-state', [
|
|
'path' => $this->localRoot,
|
|
'--output' => $outputPath,
|
|
])->assertExitCode(0);
|
|
|
|
$state = json_decode(File::get($outputPath), true, flags: JSON_THROW_ON_ERROR);
|
|
$fileNames = collect($state['data'])
|
|
->where('type', 'file')
|
|
->pluck('name')
|
|
->all();
|
|
|
|
expect($fileNames)->toEqualCanonicalizing(['keep.txt']);
|
|
});
|
|
|
|
test('json route requires a configured token', function () {
|
|
$this->getJson('/ftp/generate-sync-state')
|
|
->assertForbidden();
|
|
|
|
$this->withHeader('Authorization', 'Bearer wrong-token')
|
|
->getJson('/ftp/generate-sync-state')
|
|
->assertForbidden();
|
|
});
|
|
|
|
test('json route persists and returns the current sync state with a valid token', function () {
|
|
ftpDeployWriteLocalFile($this->localRoot, 'keep.txt', 'keep contents');
|
|
ftpDeployWriteLocalFile($this->localRoot, 'ignored/file.txt', 'ignored contents');
|
|
|
|
$response = $this->withHeader('Authorization', 'Bearer secret-token')
|
|
->getJson('/ftp/generate-sync-state')
|
|
->assertSuccessful()
|
|
->assertJsonPath('success', true)
|
|
->assertJsonPath('files', 1);
|
|
|
|
expect(File::exists($this->statePath))->toBeTrue()
|
|
->and($response->json('state.data.0.name'))->toBe('keep.txt');
|
|
});
|
|
|
|
test('ftp deploy uploads only files missing or changed from the remote sync state', function () {
|
|
ftpDeployWriteLocalFile($this->localRoot, 'same.txt', 'same contents');
|
|
ftpDeployWriteLocalFile($this->localRoot, 'changed.txt', 'local changed contents');
|
|
ftpDeployWriteLocalFile($this->localRoot, 'nested/new.txt', 'new contents');
|
|
ftpDeployWriteLocalFile($this->localRoot, 'ignored/cache.tmp', 'ignored contents');
|
|
|
|
Storage::disk('ftp_deploy')->put('same.txt', 'same contents');
|
|
Storage::disk('ftp_deploy')->put('changed.txt', 'remote old contents');
|
|
Storage::disk('ftp_deploy')->put('storage/framework/testing/ftp-deploy-state.json', ftpDeploySyncState([
|
|
'same.txt' => 'same contents',
|
|
'changed.txt' => 'remote old contents',
|
|
]));
|
|
|
|
$this->artisan('ftp:deploy', ['--yes' => true])
|
|
->assertExitCode(0);
|
|
|
|
expect(Storage::disk('ftp_deploy')->get('same.txt'))->toBe('same contents')
|
|
->and(Storage::disk('ftp_deploy')->get('changed.txt'))->toBe('local changed contents')
|
|
->and(Storage::disk('ftp_deploy')->get('nested/new.txt'))->toBe('new contents')
|
|
->and(Storage::disk('ftp_deploy')->exists('ignored/cache.tmp'))->toBeFalse();
|
|
|
|
$updatedState = json_decode(
|
|
Storage::disk('ftp_deploy')->get('storage/framework/testing/ftp-deploy-state.json'),
|
|
true,
|
|
flags: JSON_THROW_ON_ERROR,
|
|
);
|
|
|
|
$fileNames = collect($updatedState['data'])
|
|
->where('type', 'file')
|
|
->pluck('name')
|
|
->all();
|
|
|
|
expect($fileNames)->toEqualCanonicalizing([
|
|
'changed.txt',
|
|
'nested/new.txt',
|
|
'same.txt',
|
|
]);
|
|
});
|
|
|
|
test('ftp deploy dry run leaves remote files and state untouched', function () {
|
|
ftpDeployWriteLocalFile($this->localRoot, 'changed.txt', 'local changed contents');
|
|
|
|
$remoteState = ftpDeploySyncState([
|
|
'changed.txt' => 'remote old contents',
|
|
]);
|
|
|
|
Storage::disk('ftp_deploy')->put('changed.txt', 'remote old contents');
|
|
Storage::disk('ftp_deploy')->put('storage/framework/testing/ftp-deploy-state.json', $remoteState);
|
|
|
|
$this->artisan('ftp:deploy', ['--dry-run' => true])
|
|
->assertExitCode(0);
|
|
|
|
expect(Storage::disk('ftp_deploy')->get('changed.txt'))->toBe('remote old contents')
|
|
->and(Storage::disk('ftp_deploy')->get('storage/framework/testing/ftp-deploy-state.json'))->toBe($remoteState);
|
|
});
|
|
|
|
test('ftp deploy can refresh remote sync state through api before comparing files', function () {
|
|
ftpDeployWriteLocalFile($this->localRoot, 'same.txt', 'same contents');
|
|
|
|
Storage::disk('ftp_deploy')->put('same.txt', 'same contents');
|
|
Storage::disk('ftp_deploy')->put('storage/framework/testing/ftp-deploy-state.json', ftpDeploySyncState([]));
|
|
|
|
$refreshedState = ftpDeploySyncState([
|
|
'same.txt' => 'same contents',
|
|
], generatedTime: 123);
|
|
|
|
Http::fake(function () use ($refreshedState) {
|
|
Storage::disk('ftp_deploy')->put('storage/framework/testing/ftp-deploy-state.json', $refreshedState);
|
|
|
|
return Http::response([
|
|
'success' => true,
|
|
'path' => '/var/www/storage/app/.ftp-deploy-sync-state.json',
|
|
'files' => 1,
|
|
'folders' => 0,
|
|
'generated_time' => 123,
|
|
'state' => json_decode($refreshedState, true, flags: JSON_THROW_ON_ERROR),
|
|
]);
|
|
});
|
|
|
|
$this->artisan('ftp:deploy', [
|
|
'--refresh-remote-state' => true,
|
|
'--yes' => true,
|
|
])->assertExitCode(0);
|
|
|
|
$state = json_decode(
|
|
Storage::disk('ftp_deploy')->get('storage/framework/testing/ftp-deploy-state.json'),
|
|
true,
|
|
flags: JSON_THROW_ON_ERROR,
|
|
);
|
|
|
|
expect($state['generatedTime'])->toBe(123);
|
|
|
|
Http::assertSent(fn ($request): bool => $request->url() === 'https://example.test/ftp/generate-sync-state'
|
|
&& $request->hasHeader('Authorization', 'Bearer secret-token'));
|
|
});
|
|
|
|
test('ftp deploy force uploads every included file', function () {
|
|
ftpDeployWriteLocalFile($this->localRoot, 'same.txt', 'same contents');
|
|
ftpDeployWriteLocalFile($this->localRoot, 'other.txt', 'other contents');
|
|
|
|
Storage::disk('ftp_deploy')->put('same.txt', 'remote contents');
|
|
Storage::disk('ftp_deploy')->put('other.txt', 'remote contents');
|
|
Storage::disk('ftp_deploy')->put('storage/framework/testing/ftp-deploy-state.json', ftpDeploySyncState([
|
|
'same.txt' => 'same contents',
|
|
'other.txt' => 'other contents',
|
|
]));
|
|
|
|
$this->artisan('ftp:deploy', [
|
|
'--force' => true,
|
|
'--yes' => true,
|
|
])->assertExitCode(0);
|
|
|
|
expect(Storage::disk('ftp_deploy')->get('same.txt'))->toBe('same contents')
|
|
->and(Storage::disk('ftp_deploy')->get('other.txt'))->toBe('other contents');
|
|
});
|
|
|
|
function ftpDeployWriteLocalFile(string $root, string $path, string $contents): void
|
|
{
|
|
$fullPath = "{$root}/{$path}";
|
|
|
|
File::ensureDirectoryExists(dirname($fullPath));
|
|
File::put($fullPath, $contents);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, string> $files
|
|
*/
|
|
function ftpDeploySyncState(array $files, int $generatedTime = 1): string
|
|
{
|
|
return json_encode([
|
|
'description' => 'test sync state',
|
|
'version' => '1.0.0',
|
|
'generatedTime' => $generatedTime,
|
|
'data' => collect($files)
|
|
->map(fn (string $contents, string $name): array => [
|
|
'type' => 'file',
|
|
'name' => $name,
|
|
'size' => strlen($contents),
|
|
'hash' => hash('sha256', $contents),
|
|
])
|
|
->values()
|
|
->all(),
|
|
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
|
}
|