static-php-cli/tests/mock/SPC_store.php

77 lines
2.5 KiB
PHP
Raw Normal View History

2024-10-03 14:52:46 +08:00
<?php
declare(strict_types=1);
// mock global functions
namespace SPC\store;
use SPC\exception\InterruptException;
use SPC\exception\SPCInternalException;
2024-10-05 10:52:53 +08:00
2024-10-03 14:52:46 +08:00
function f_exec(string $command, mixed &$output, mixed &$result_code): bool
{
2024-10-05 10:52:53 +08:00
$result_code = 0;
2024-10-03 14:52:46 +08:00
if (str_contains($command, 'https://api.github.com/repos/AOMediaCodec/libavif/releases')) {
$output = explode("\n", gzdecode(file_get_contents(__DIR__ . '/../assets/github_api_AOMediaCodec_libavif_releases.json.gz')));
return true;
}
if (str_contains($command, 'https://api.github.com/repos/AOMediaCodec/libavif/tarball/v1.1.1')) {
$output = explode("\n", "HTTP/1.1 200 OK\r\nContent-Disposition: attachment; filename=AOMediaCodec-libavif-v1.1.1-0-gbb24db0.tar.gz\r\n\r\n");
2024-10-05 10:52:53 +08:00
return true;
}
if (str_contains($command, 'https://api.bitbucket.org/2.0/repositories/')) {
$output = explode("\n", json_encode(['values' => [['name' => '1.0.0']], 'tag_name' => '1.0.0']));
return true;
}
if (str_contains($command, 'https://bitbucket.org/')) {
$output = explode("\n", str_contains($command, 'MATCHED') ? "HTTP/2 200 OK\r\ncontent-disposition: attachment; filename=abc.tar.gz\r\n\r\n" : "HTTP/2 200 OK\r\n\r\n");
return true;
}
if (str_contains($command, 'ghreltest/ghrel')) {
$output = explode("\n", json_encode([[
'prerelease' => false,
'assets' => [
[
'name' => 'ghreltest.tar.gz',
'browser_download_url' => 'https://fakecmd.com/ghreltest.tar.gz',
],
],
]]));
return true;
}
if (str_contains($command, 'filelist')) {
$output = explode("\n", gzdecode(file_get_contents(__DIR__ . '/../assets/filelist.gz')));
2024-10-03 14:52:46 +08:00
return true;
}
$result_code = -2;
$output = null;
return false;
}
2024-10-05 10:52:53 +08:00
function f_passthru(string $cmd): bool
{
if (str_starts_with($cmd, 'git')) {
if (str_contains($cmd, '--branch "SIGINT"')) {
throw new InterruptException('interrupted', 2);
2024-10-05 10:52:53 +08:00
}
return true;
}
if (str_contains($cmd, 'https://fakecmd.com/curlDown')) {
if (str_contains($cmd, 'SIGINT')) {
throw new InterruptException('interrupted', 2);
2024-10-05 10:52:53 +08:00
}
return true;
}
2024-10-05 10:55:53 +08:00
// allowed commands
$allowed = ['cp', 'copy', 'xcopy'];
foreach ($allowed as $a) {
if (str_starts_with($cmd, $a)) {
\f_passthru($cmd);
return true;
}
}
throw new SPCInternalException('Invalid tests');
2024-10-05 10:52:53 +08:00
}