ZipRemoteFileTest.php 1.4 KB

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