112 lines
3.6 KiB
PHP
112 lines
3.6 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_set_file_get_contents_false']) && str_ends_with($path, 'composer.json')) {
|
|
return false;
|
|
}
|
|
|
|
return \file_get_contents($path);
|
|
}
|
|
}
|
|
|
|
if (!function_exists(__NAMESPACE__ . '\\json_encode')) {
|
|
function json_encode($value, int $flags = 0): string|false
|
|
{
|
|
if (!empty($GLOBALS['version_set_json_encode_false'])) {
|
|
return false;
|
|
}
|
|
|
|
return \json_encode($value, $flags);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
use App\Models\Setting;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
function withComposerBackupForSet(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_set_file_get_contents_false'] = false;
|
|
$GLOBALS['version_set_json_encode_false'] = false;
|
|
}
|
|
}
|
|
|
|
it('sets version when no current version', function (): void {
|
|
withComposerBackupForSet(function (string $path): void {
|
|
Setting::where('key', 'version')->delete();
|
|
|
|
$exitCode = Artisan::call('version:set', ['version' => '2.3.4']);
|
|
expect($exitCode)->toBe(0);
|
|
|
|
$setting = Setting::where('key', 'version')->value('value');
|
|
expect($setting)->toBe('2.3.4');
|
|
|
|
$data = json_decode((string) file_get_contents($path), true);
|
|
expect($data['version'] ?? null)->toBe('2.3.4');
|
|
});
|
|
});
|
|
|
|
it('updates version when current exists', function (): void {
|
|
withComposerBackupForSet(function (): void {
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.0.0']);
|
|
|
|
$exitCode = Artisan::call('version:set', ['version' => '1.0.1']);
|
|
expect($exitCode)->toBe(0);
|
|
|
|
$setting = Setting::where('key', 'version')->value('value');
|
|
expect($setting)->toBe('1.0.1');
|
|
});
|
|
});
|
|
|
|
it('fails when composer.json cannot be read', function (): void {
|
|
withComposerBackupForSet(function (string $path): void {
|
|
chmod($path, 0000);
|
|
|
|
$exitCode = Artisan::call('version:set', ['version' => '2.0.0']);
|
|
expect($exitCode)->toBe(1);
|
|
|
|
chmod($path, 0644);
|
|
});
|
|
});
|
|
|
|
it('fails when composer.json cannot be decoded', function (): void {
|
|
withComposerBackupForSet(function (string $path): void {
|
|
file_put_contents($path, 'not-json');
|
|
|
|
$exitCode = Artisan::call('version:set', ['version' => '2.0.0']);
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
|
|
it('fails when file_get_contents returns false', function (): void {
|
|
withComposerBackupForSet(function (): void {
|
|
$GLOBALS['version_set_file_get_contents_false'] = true;
|
|
|
|
$exitCode = Artisan::call('version:set', ['version' => '2.0.0']);
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
|
|
it('fails when json_encode returns false', function (): void {
|
|
withComposerBackupForSet(function (): void {
|
|
$GLOBALS['version_set_json_encode_false'] = true;
|
|
|
|
$exitCode = Artisan::call('version:set', ['version' => '2.0.0']);
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
}
|