ZipRemoteFileTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests;
  10. use PhpZip\Exception\ZipException;
  11. use PhpZip\ZipFile;
  12. /**
  13. * Test add remote files to zip archive.
  14. *
  15. * @internal
  16. *
  17. * @small
  18. */
  19. class ZipRemoteFileTest extends ZipTestCase
  20. {
  21. /**
  22. * @throws ZipException
  23. */
  24. public function testAddRemoteFileFromStream(): void
  25. {
  26. $zipFile = new ZipFile();
  27. $outputZip = $this->outputFilename;
  28. $fileUrl = 'https://raw.githubusercontent.com/Ne-Lexa/php-zip/master/README.md';
  29. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  30. $fp = @fopen(
  31. $fileUrl,
  32. 'rb',
  33. false,
  34. stream_context_create(
  35. [
  36. 'http' => [
  37. 'timeout' => 3,
  38. ],
  39. ]
  40. )
  41. );
  42. if ($fp === false) {
  43. static::markTestSkipped(
  44. sprintf(
  45. 'Could not fetch remote file: %s',
  46. $fileUrl
  47. )
  48. );
  49. }
  50. $fileName = 'remote-file-from-http-stream.md';
  51. $zipFile->addFromStream($fp, $fileName);
  52. $zipFile->saveAsFile($outputZip);
  53. $zipFile->close();
  54. $zipFile = new ZipFile();
  55. $zipFile->openFile($outputZip);
  56. $files = $zipFile->getListFiles();
  57. static::assertCount(1, $files);
  58. static::assertSame($fileName, $files[0]);
  59. $zipFile->close();
  60. }
  61. }