DownloaderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. namespace SPC\Tests\store;
  4. use PHPUnit\Framework\TestCase;
  5. use SPC\exception\WrongUsageException;
  6. use SPC\store\Downloader;
  7. /**
  8. * @internal
  9. * TODO: Test all methods
  10. */
  11. class DownloaderTest extends TestCase
  12. {
  13. public function testGetLatestGithubTarball()
  14. {
  15. $this->assertEquals(
  16. 'https://api.github.com/repos/AOMediaCodec/libavif/tarball/v1.1.1',
  17. Downloader::getLatestGithubTarball('libavif', [
  18. 'type' => 'ghtar',
  19. 'repo' => 'AOMediaCodec/libavif',
  20. ])[0]
  21. );
  22. }
  23. public function testDownloadGit()
  24. {
  25. Downloader::downloadGit('setup-static-php', 'https://github.com/static-php/setup-static-php.git', 'main');
  26. $this->assertTrue(true);
  27. // test keyboard interrupt
  28. try {
  29. Downloader::downloadGit('setup-static-php', 'https://github.com/static-php/setup-static-php.git', 'SIGINT');
  30. } catch (WrongUsageException $e) {
  31. $this->assertStringContainsString('interrupted', $e->getMessage());
  32. return;
  33. }
  34. $this->fail('Expected exception not thrown');
  35. }
  36. public function testDownloadFile()
  37. {
  38. Downloader::downloadFile('fake-file', 'https://fakecmd.com/curlDown', 'curlDown.exe');
  39. $this->assertTrue(true);
  40. // test keyboard interrupt
  41. try {
  42. Downloader::downloadFile('fake-file', 'https://fakecmd.com/curlDown', 'SIGINT');
  43. } catch (WrongUsageException $e) {
  44. $this->assertStringContainsString('interrupted', $e->getMessage());
  45. return;
  46. }
  47. $this->fail('Expected exception not thrown');
  48. }
  49. public function testLockSource()
  50. {
  51. Downloader::lockSource('fake-file', ['source_type' => 'archive', 'filename' => 'fake-file-name', 'move_path' => 'fake-path', 'lock_as' => 'fake-lock-as']);
  52. $this->assertFileExists(DOWNLOAD_PATH . '/.lock.json');
  53. $json = json_decode(file_get_contents(DOWNLOAD_PATH . '/.lock.json'), true);
  54. $this->assertIsArray($json);
  55. $this->assertArrayHasKey('fake-file', $json);
  56. $this->assertArrayHasKey('source_type', $json['fake-file']);
  57. $this->assertArrayHasKey('filename', $json['fake-file']);
  58. $this->assertArrayHasKey('move_path', $json['fake-file']);
  59. $this->assertArrayHasKey('lock_as', $json['fake-file']);
  60. $this->assertEquals('archive', $json['fake-file']['source_type']);
  61. $this->assertEquals('fake-file-name', $json['fake-file']['filename']);
  62. $this->assertEquals('fake-path', $json['fake-file']['move_path']);
  63. $this->assertEquals('fake-lock-as', $json['fake-file']['lock_as']);
  64. }
  65. public function testGetLatestBitbucketTag()
  66. {
  67. $this->assertEquals(
  68. 'abc.tar.gz',
  69. Downloader::getLatestBitbucketTag('abc', [
  70. 'repo' => 'MATCHED/def',
  71. ])[1]
  72. );
  73. $this->assertEquals(
  74. 'abc-1.0.0.tar.gz',
  75. Downloader::getLatestBitbucketTag('abc', [
  76. 'repo' => 'abc/def',
  77. ])[1]
  78. );
  79. }
  80. public function testGetLatestGithubRelease()
  81. {
  82. $this->assertEquals(
  83. 'ghreltest.tar.gz',
  84. Downloader::getLatestGithubRelease('ghrel', [
  85. 'type' => 'ghrel',
  86. 'repo' => 'ghreltest/ghrel',
  87. 'match' => 'ghreltest.tar.gz',
  88. ])[1]
  89. );
  90. }
  91. public function testGetFromFileList()
  92. {
  93. $filelist = Downloader::getFromFileList('fake-filelist', [
  94. 'url' => 'https://fakecmd.com/filelist',
  95. 'regex' => '/href="(?<file>filelist-(?<version>[^"]+)\.tar\.xz)"/',
  96. ]);
  97. $this->assertIsArray($filelist);
  98. $this->assertEquals('filelist-4.7.0.tar.xz', $filelist[1]);
  99. }
  100. }