Files
XC_VM/tests/Unit/GitHubReleasesTest.php
T
Divarion_D 567641137d test: fix stale update-channel tests and define BIN_PATH in bootstrap
Three unit tests were failing against correct production code:

- FfmpegPathsTest errored on an undefined BIN_PATH constant. The test
  bootstrap defined FFMPEG_BIN_* but not BIN_PATH, so FfmpegPaths::binary()
  faulted before it could fall back. Define BIN_PATH to a fixture-less path
  so version lookups miss and the FFMPEG_BIN_40 fallback is exercised.
- GitHubReleasesTest expected the old 'unstable' channel name; the channel
  was renamed to 'beta' ('unstable' kept as a legacy alias). Update the
  cache-switch test to expect 'beta' (and assert the alias normalizes), and
  make the reject-invalid test use a genuinely unknown channel ('nightly').

Full suite now green (461 passing).
2026-08-31 22:22:34 +03:00

91 lines
3.3 KiB
PHP

<?php
use XcVm\Core\Updates\GitHubReleases;
use PHPUnit\Framework\TestCase;
final class GitHubReleasesTest extends TestCase {
public function testIsValidVersionAcceptsSemanticVersion() {
$this->assertTrue(GitHubReleases::isValidVersion('1.2.3'));
}
public function testIsValidVersionRejectsLeadingZeros() {
$this->assertFalse(GitHubReleases::isValidVersion('01.2.3'));
}
public function testIsValidVersionThrowsForTooLongInput() {
$this->expectException(InvalidArgumentException::class);
GitHubReleases::isValidVersion(str_repeat('1', 21));
}
public function testGetLatestVersionReturnsNullWhenAlreadyUpToDate() {
$mock = $this->getMockBuilder(GitHubReleases::class)
->setConstructorArgs(array('Vateron-Media', 'XC_VM', 'stable'))
->onlyMethods(array('getReleases'))
->getMock();
$mock->method('getReleases')->willReturn(array('1.2.3', '1.2.2', '1.2.1'));
$this->assertNull($mock->getLatestVersion('1.2.3'));
}
public function testGetLatestVersionReturnsNewestVersion() {
$mock = $this->getMockBuilder(GitHubReleases::class)
->setConstructorArgs(array('Vateron-Media', 'XC_VM', 'stable'))
->onlyMethods(array('getReleases'))
->getMock();
$mock->method('getReleases')->willReturn(array('1.4.0', '1.3.9', '1.3.0'));
$this->assertSame('1.4.0', $mock->getLatestVersion('1.3.9'));
}
public function testGetUpdateFileBuildsMainArchiveUrlAndHash() {
$mock = $this->getMockBuilder(GitHubReleases::class)
->setConstructorArgs(array('Vateron-Media', 'XC_VM', 'stable'))
->onlyMethods(array('getLatestVersion', 'getAssetHash'))
->getMock();
$mock->method('getLatestVersion')->with('1.0.0')->willReturn('1.1.0');
$mock->method('getAssetHash')->with('1.1.0', 'xc_vm.tar.gz')->willReturn('md5-hash-value');
$result = $mock->getUpdateFile('main', '1.0.0');
$this->assertSame('https://github.com/Vateron-Media/XC_VM/releases/download/1.1.0/xc_vm.tar.gz', $result['url']);
$this->assertSame('md5-hash-value', $result['md5']);
}
public function testSetTimeoutKeepsMinimumOneSecond() {
$instance = new GitHubReleases('Vateron-Media', 'XC_VM', 'stable');
$instance->setTimeout(0);
$reflection = new ReflectionClass($instance);
$timeout = $reflection->getProperty('timeout');
$timeout->setAccessible(true);
$this->assertSame(1, $timeout->getValue($instance));
}
public function testSetChannelUpdatesCacheFileAndClearsOldCache() {
$instance = new GitHubReleases('Vateron-Media', 'XC_VM', 'stable');
$reflection = new ReflectionClass($instance);
$cacheProperty = $reflection->getProperty('cache_file');
$cacheProperty->setAccessible(true);
$channelProperty = $reflection->getProperty('channel');
$channelProperty->setAccessible(true);
$basePath = sys_get_temp_dir() . '/gitapi_test_' . uniqid('', true);
$cacheProperty->setValue($instance, $basePath . '_stable');
file_put_contents($basePath . '_stable', 'cache-data');
$instance->setChannel('unstable'); // legacy alias, normalized to 'beta'
$this->assertSame('beta', $channelProperty->getValue($instance));
$this->assertSame($basePath . '_beta', $cacheProperty->getValue($instance));
$this->assertFalse(file_exists($basePath . '_stable'));
}
public function testSetChannelRejectsInvalidValue() {
$instance = new GitHubReleases('Vateron-Media', 'XC_VM', 'stable');
$this->expectException(InvalidArgumentException::class);
$instance->setChannel('nightly');
}
}