ZipRemoteFileTest.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Exception\ZipException;
  4. /**
  5. * Test add remote files to zip archive.
  6. *
  7. * @internal
  8. *
  9. * @small
  10. * @covers
  11. */
  12. class ZipRemoteFileTest extends ZipTestCase
  13. {
  14. /**
  15. * @throws ZipException
  16. */
  17. public function testAddRemoteFileFromStream()
  18. {
  19. $zipFile = new ZipFile();
  20. $outputZip = $this->outputFilename;
  21. $fileUrl = 'https://raw.githubusercontent.com/Ne-Lexa/php-zip/master/README.md';
  22. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  23. $fp = @fopen(
  24. $fileUrl,
  25. 'rb',
  26. false,
  27. stream_context_create(
  28. [
  29. 'http' => [
  30. 'timeout' => 3,
  31. ],
  32. ]
  33. )
  34. );
  35. if ($fp === false) {
  36. static::markTestSkipped(
  37. sprintf(
  38. 'Could not fetch remote file: %s',
  39. $fileUrl
  40. )
  41. );
  42. return;
  43. }
  44. $fileName = 'remote-file-from-http-stream.md';
  45. $zipFile->addFromStream($fp, $fileName);
  46. $zipFile->saveAsFile($outputZip);
  47. $zipFile->close();
  48. $zipFile = new ZipFile();
  49. $zipFile->openFile($outputZip);
  50. $files = $zipFile->getListFiles();
  51. static::assertCount(1, $files);
  52. static::assertSame($fileName, $files[0]);
  53. $zipFile->close();
  54. }
  55. }