ZipRemoteFileTest.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PhpZip\Exception\ZipException;
  4. use PhpZip\ZipFile;
  5. /**
  6. * Test add remote files to zip archive.
  7. *
  8. * @internal
  9. *
  10. * @small
  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. }