init
This commit is contained in:
21 files changed
+1394
No files matched your search
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\File;
|
||||
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.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:deploy',
|
||||
'ftp:generate-sync-state',
|
||||
])
|
||||
->and(Route::has('ftp-deploy.generate-sync-state'))->toBeTrue();
|
||||
});
|
||||
|
||||
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 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): string
|
||||
{
|
||||
return json_encode([
|
||||
'description' => 'test sync state',
|
||||
'version' => '1.0.0',
|
||||
'generatedTime' => 1,
|
||||
'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);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
use MeghdadFadaee\LaravelFtpDeploy\Tests\TestCase;
|
||||
|
||||
pest()->extend(TestCase::class)->in('Feature');
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace MeghdadFadaee\LaravelFtpDeploy\Tests;
|
||||
|
||||
use MeghdadFadaee\LaravelFtpDeploy\LaravelFtpDeployServiceProvider;
|
||||
use Orchestra\Testbench\TestCase as Orchestra;
|
||||
|
||||
abstract class TestCase extends Orchestra
|
||||
{
|
||||
/**
|
||||
* @return array<int, class-string>
|
||||
*/
|
||||
protected function getPackageProviders($app): array
|
||||
{
|
||||
return [
|
||||
LaravelFtpDeployServiceProvider::class,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user