2
0

ZipStreamOpenTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use PhpZip\Exception\InvalidArgumentException;
  5. use PhpZip\Exception\ZipException;
  6. use PhpZip\ZipFile;
  7. /**
  8. * Class ZipStreamOpenTest.
  9. *
  10. * @internal
  11. *
  12. * @medium
  13. */
  14. class ZipStreamOpenTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider provideStreams
  18. *
  19. * @param resource $resource
  20. * @param string|null $exceptionClass
  21. * @param string|null $exceptionMessage
  22. *
  23. * @throws ZipException
  24. */
  25. public function testOpenStream($resource, $exceptionClass = null, $exceptionMessage = null)
  26. {
  27. if ($resource === null) {
  28. static::markTestSkipped('skip null resource');
  29. return;
  30. }
  31. if ($exceptionClass !== null) {
  32. $this->setExpectedException(
  33. $exceptionClass,
  34. $exceptionMessage
  35. );
  36. }
  37. static::assertInternalType('resource', $resource);
  38. $zipFile = new ZipFile();
  39. $zipFile->openFromStream($resource);
  40. static::assertTrue(fclose($resource));
  41. }
  42. /**
  43. * @return array|\Generator
  44. */
  45. public function provideStreams()
  46. {
  47. return [
  48. 'file' => yield [fopen(__DIR__ . '/resources/apk.zip', 'rb'), null, null],
  49. 'directory' => yield [
  50. fopen(__DIR__, 'rb'),
  51. InvalidArgumentException::class,
  52. 'Directory stream not supported',
  53. ],
  54. 'temp' => yield [$this->getTempResource('php://temp'), null, null],
  55. 'memory' => yield [$this->getTempResource('php://memory'), null, null],
  56. 'bz' => yield [
  57. $this->getBzResource(),
  58. InvalidArgumentException::class,
  59. 'The stream wrapper type "Unknown" is not supported.',
  60. ],
  61. 'url' => yield [
  62. fopen('https://github.com/Ne-Lexa/php-zip/archive/master.zip', 'rb'),
  63. InvalidArgumentException::class,
  64. 'The stream wrapper type "http" is not supported.',
  65. ],
  66. 'ftp' => yield [
  67. fopen('ftp://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-latest.md5', 'rb'),
  68. InvalidArgumentException::class,
  69. 'The stream wrapper type "ftp" is not supported.',
  70. ],
  71. ];
  72. }
  73. /**
  74. * @param string $filename
  75. *
  76. * @return resource
  77. */
  78. private function getTempResource($filename)
  79. {
  80. $fp = fopen(__DIR__ . '/resources/apk.zip', 'rb');
  81. $stream = fopen($filename, 'r+b');
  82. stream_copy_to_stream($fp, $stream);
  83. fclose($fp);
  84. rewind($stream);
  85. return $stream;
  86. }
  87. /**
  88. * @return resource|null
  89. */
  90. private function getBzResource()
  91. {
  92. if (!\extension_loaded('bz2')) {
  93. return null;
  94. }
  95. $stream = bzopen('php://temp', 'w');
  96. bzwrite($stream, 'some input here');
  97. return $stream;
  98. }
  99. }