Add full Downloader tests

This commit is contained in:
crazywhalecc
2024-10-05 10:52:53 +08:00
committed by Jerry Ma
parent 948b55026c
commit 357dfc53c9
3 changed files with 127 additions and 14 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace SPC\Tests\store;
use PHPUnit\Framework\TestCase;
use SPC\exception\WrongUsageException;
use SPC\store\Downloader;
/**
@@ -24,23 +25,88 @@ class DownloaderTest extends TestCase
);
}
public function testDownloadGit() {}
public function testDownloadGit()
{
Downloader::downloadGit('setup-static-php', 'https://github.com/static-php/setup-static-php.git', 'main');
$this->assertTrue(true);
public function testDownloadFile() {}
// test keyboard interrupt
try {
Downloader::downloadGit('setup-static-php', 'https://github.com/static-php/setup-static-php.git', 'SIGINT');
} catch (WrongUsageException $e) {
$this->assertStringContainsString('interrupted', $e->getMessage());
return;
}
$this->fail('Expected exception not thrown');
}
public function testLockSource() {}
public function testDownloadFile()
{
Downloader::downloadFile('fake-file', 'https://fakecmd.com/curlDown', 'curlDown.exe');
$this->assertTrue(true);
public function testGetLatestBitbucketTag() {}
// test keyboard interrupt
try {
Downloader::downloadFile('fake-file', 'https://fakecmd.com/curlDown', 'SIGINT');
} catch (WrongUsageException $e) {
$this->assertStringContainsString('interrupted', $e->getMessage());
return;
}
$this->fail('Expected exception not thrown');
}
public function testGetLatestGithubRelease() {}
public function testLockSource()
{
Downloader::lockSource('fake-file', ['source_type' => 'archive', 'filename' => 'fake-file-name', 'move_path' => 'fake-path', 'lock_as' => 'fake-lock-as']);
$this->assertFileExists(DOWNLOAD_PATH . '/.lock.json');
$json = json_decode(file_get_contents(DOWNLOAD_PATH . '/.lock.json'), true);
$this->assertIsArray($json);
$this->assertArrayHasKey('fake-file', $json);
$this->assertArrayHasKey('source_type', $json['fake-file']);
$this->assertArrayHasKey('filename', $json['fake-file']);
$this->assertArrayHasKey('move_path', $json['fake-file']);
$this->assertArrayHasKey('lock_as', $json['fake-file']);
$this->assertEquals('archive', $json['fake-file']['source_type']);
$this->assertEquals('fake-file-name', $json['fake-file']['filename']);
$this->assertEquals('fake-path', $json['fake-file']['move_path']);
$this->assertEquals('fake-lock-as', $json['fake-file']['lock_as']);
}
public function testCurlExec() {}
public function testGetLatestBitbucketTag()
{
$this->assertEquals(
'abc.tar.gz',
Downloader::getLatestBitbucketTag('abc', [
'repo' => 'MATCHED/def',
])[1]
);
$this->assertEquals(
'abc-1.0.0.tar.gz',
Downloader::getLatestBitbucketTag('abc', [
'repo' => 'abc/def',
])[1]
);
}
public function testCurlDown() {}
public function testGetLatestGithubRelease()
{
$this->assertEquals(
'ghreltest.tar.gz',
Downloader::getLatestGithubRelease('ghrel', [
'type' => 'ghrel',
'repo' => 'ghreltest/ghrel',
'match' => 'ghreltest.tar.gz',
])[1]
);
}
public function testDownloadSource() {}
public function testGetFromFileList() {}
public function testDownloadPackage() {}
public function testGetFromFileList()
{
$filelist = Downloader::getFromFileList('fake-filelist', [
'url' => 'https://fakecmd.com/filelist',
'regex' => '/href="(?<file>filelist-(?<version>[^"]+)\\.tar\\.xz)"/',
]);
$this->assertIsArray($filelist);
$this->assertEquals('filelist-4.7.0.tar.xz', $filelist[1]);
}
}