ZipStreamOpenTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /** @noinspection PhpUsageOfSilenceOperatorInspection */
  3. namespace PhpZip\Tests;
  4. use PHPUnit\Framework\TestCase;
  5. use PhpZip\Exception\InvalidArgumentException;
  6. use PhpZip\Exception\ZipException;
  7. use PhpZip\ZipFile;
  8. /**
  9. * Class ZipStreamOpenTest.
  10. *
  11. * @internal
  12. *
  13. * @medium
  14. */
  15. class ZipStreamOpenTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider provideStreams
  19. *
  20. * @param resource $resource
  21. * @param string|null $exceptionClass
  22. * @param string|null $exceptionMessage
  23. *
  24. * @throws ZipException
  25. */
  26. public function testOpenStream($resource, $exceptionClass = null, $exceptionMessage = null)
  27. {
  28. if ($resource === null) {
  29. static::markTestSkipped('skip null resource');
  30. return;
  31. }
  32. if ($exceptionClass !== null) {
  33. $this->setExpectedException(
  34. $exceptionClass,
  35. $exceptionMessage
  36. );
  37. }
  38. static::assertInternalType('resource', $resource);
  39. $zipFile = new ZipFile();
  40. $zipFile->openFromStream($resource);
  41. static::assertTrue(fclose($resource));
  42. }
  43. /**
  44. * @return array
  45. */
  46. public function provideStreams()
  47. {
  48. return [
  49. [@fopen(__DIR__ . '/resources/apk.zip', 'rb'), null, null],
  50. [
  51. @fopen(__DIR__, 'rb'),
  52. InvalidArgumentException::class,
  53. 'Directory stream not supported',
  54. ],
  55. [$this->getTempResource('php://temp'), null, null],
  56. [$this->getTempResource('php://memory'), null, null],
  57. [
  58. @fopen('https://github.com/Ne-Lexa/php-zip/archive/master.zip', 'rb'),
  59. InvalidArgumentException::class,
  60. 'The stream wrapper type "http" is not supported.',
  61. ],
  62. ];
  63. }
  64. /**
  65. * @param string $filename
  66. *
  67. * @return resource
  68. */
  69. private function getTempResource($filename)
  70. {
  71. $fp = fopen(__DIR__ . '/resources/apk.zip', 'rb');
  72. $stream = fopen($filename, 'r+b');
  73. stream_copy_to_stream($fp, $stream);
  74. fclose($fp);
  75. rewind($stream);
  76. return $stream;
  77. }
  78. }