126 lines
4.1 KiB
PHP
126 lines
4.1 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);
|
|
}
|
|
}
|
|
|
|
if (!function_exists(__NAMESPACE__ . '\\json_encode')) {
|
|
function json_encode($value, int $flags = 0): string|false
|
|
{
|
|
if (!empty($GLOBALS['version_fetch_json_encode_false']) && is_array($value) && array_key_exists('build', $value)) {
|
|
return false;
|
|
}
|
|
|
|
return \json_encode($value, $flags);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
$GLOBALS['version_fetch_json_encode_false'] = false;
|
|
$originalPath = $GLOBALS['version_fetch_path'] ?? null;
|
|
if ($originalPath !== null) {
|
|
putenv("PATH={$originalPath}");
|
|
$_ENV['PATH'] = $originalPath;
|
|
$_SERVER['PATH'] = $originalPath;
|
|
unset($GLOBALS['version_fetch_path']);
|
|
}
|
|
}
|
|
}
|
|
|
|
it('fetches build count and syncs composer metadata', function (): void {
|
|
withComposerBackupForFetch(function (): void {
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(0);
|
|
|
|
$build = Setting::where('key', 'build')->value('value');
|
|
expect(is_numeric($build))->toBeTrue();
|
|
});
|
|
});
|
|
|
|
it('fails when build count cannot be resolved', function (): void {
|
|
withComposerBackupForFetch(function (): void {
|
|
$GLOBALS['version_fetch_path'] = getenv('PATH') ?: '';
|
|
putenv('PATH=/nope');
|
|
$_ENV['PATH'] = '/nope';
|
|
$_SERVER['PATH'] = '/nope';
|
|
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
|
|
it('fails when composer.json cannot be decoded', function (): void {
|
|
withComposerBackupForFetch(function (string $path): void {
|
|
file_put_contents($path, 'not-json');
|
|
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
|
|
it('fails when composer.json is not readable', function (): void {
|
|
withComposerBackupForFetch(function (string $path): void {
|
|
chmod($path, 0000);
|
|
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
|
|
chmod($path, 0644);
|
|
});
|
|
});
|
|
|
|
it('fails when file_get_contents returns false', function (): void {
|
|
withComposerBackupForFetch(function (): void {
|
|
$GLOBALS['version_fetch_file_get_contents_false'] = true;
|
|
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
|
|
it('fails when json_encode returns false', function (): void {
|
|
withComposerBackupForFetch(function (): void {
|
|
$GLOBALS['version_fetch_json_encode_false'] = true;
|
|
|
|
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
|
|
|
|
$exitCode = Artisan::call('version:fetch');
|
|
expect($exitCode)->toBe(1);
|
|
});
|
|
});
|
|
}
|