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 || $resource === false) {
  29. static::markTestSkipped('skip resource');
  30. }
  31. if ($exceptionClass !== null) {
  32. $this->expectException(
  33. $exceptionClass
  34. );
  35. $this->expectExceptionMessage(
  36. $exceptionMessage
  37. );
  38. }
  39. static::assertIsResource($resource);
  40. $zipFile = new ZipFile();
  41. $zipFile->openFromStream($resource);
  42. static::assertTrue(fclose($resource));
  43. }
  44. /**
  45. * @return array
  46. */
  47. public function provideStreams()
  48. {
  49. return [
  50. [@fopen(__DIR__ . '/resources/apk.zip', 'rb'), null, null],
  51. [
  52. @fopen(__DIR__, 'rb'),
  53. InvalidArgumentException::class,
  54. 'Directory stream not supported',
  55. ],
  56. [$this->getTempResource('php://temp'), null, null],
  57. [$this->getTempResource('php://memory'), null, null],
  58. [
  59. @fopen('https://github.com/Ne-Lexa/php-zip/archive/master.zip', 'rb'),
  60. InvalidArgumentException::class,
  61. 'The stream wrapper type "http" is not supported.',
  62. ],
  63. ];
  64. }
  65. /**
  66. * @param string $filename
  67. *
  68. * @return resource
  69. */
  70. private function getTempResource($filename)
  71. {
  72. $fp = fopen(__DIR__ . '/resources/apk.zip', 'rb');
  73. $stream = fopen($filename, 'r+b');
  74. stream_copy_to_stream($fp, $stream);
  75. fclose($fp);
  76. rewind($stream);
  77. return $stream;
  78. }
  79. }