78 lines
2.8 KiB
PHP
78 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands {
|
|
if (!function_exists(__NAMESPACE__ . '\\file_get_contents')) {
|
|
function file_get_contents($path): string|false
|
|
{
|
|
if (!empty($GLOBALS['version_fetch_file_get_contents_false']) && str_ends_with($path, 'composer.json')) {
|
|
return false;
|
|
}
|
|
|
|
return \file_get_contents($path);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
use App\Models\Setting;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
function withComposerBackupForFetch(callable $callback): void
|
|
{
|
|
$path = base_path('composer.json');
|
|
$original = file_get_contents($path);
|
|
|
|
try {
|
|
$callback($path, $original);
|
|
} finally {
|
|
if ($original !== false) {
|
|
file_put_contents($path, $original);
|
|
}
|
|
$GLOBALS['version_fetch_file_get_contents_false'] = false;
|
|
}
|
|
}
|
|
|
|
it('syncs version and build from composer metadata', function (): void {
|
|
withComposerBackupForFetch(function (): void {
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '0.0.0']);
|
|
Setting::updateOrCreate(['key' => 'build'], ['value' => '0']);
|
|
|
|
$composer = json_decode((string) file_get_contents(base_path('composer.json')), true);
|
|
$expectedVersion = (string) ($composer['version'] ?? '');
|
|
$expectedBuild = (string) ($composer['build'] ?? '');
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(0);
|
|
expect(Setting::where('key', 'version')->value('value'))->toBe($expectedVersion);
|
|
expect(Setting::where('key', 'build')->value('value'))->toBe($expectedBuild);
|
|
});
|
|
});
|
|
|
|
it('fails when composer.json cannot be decoded', function (): void {
|
|
withComposerBackupForFetch(function (string $path): void {
|
|
file_put_contents($path, 'not-json');
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
|
|
it('fails when composer.json is missing build', function (): void {
|
|
withComposerBackupForFetch(function (string $path): void {
|
|
$data = json_decode((string) file_get_contents($path), true);
|
|
unset($data['build']);
|
|
file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL);
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
|
|
it('fails when file_get_contents returns false', function (): void {
|
|
withComposerBackupForFetch(function (): void {
|
|
$GLOBALS['version_fetch_file_get_contents_false'] = true;
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
}
|