ZipStreamOpenTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests;
  10. use PHPUnit\Framework\TestCase;
  11. use PhpZip\Exception\InvalidArgumentException;
  12. use PhpZip\Exception\ZipException;
  13. use PhpZip\ZipFile;
  14. /**
  15. * Class ZipStreamOpenTest.
  16. *
  17. * @internal
  18. *
  19. * @medium
  20. */
  21. class ZipStreamOpenTest extends TestCase
  22. {
  23. /**
  24. * @dataProvider provideStreams
  25. *
  26. * @param resource $resource
  27. * @param ?string $exceptionClass
  28. * @param ?string $exceptionMessage
  29. *
  30. * @throws ZipException
  31. */
  32. public function testOpenStream($resource, ?string $exceptionClass = null, ?string $exceptionMessage = null): void
  33. {
  34. if ($resource === null || $resource === false) {
  35. static::markTestSkipped('skip resource');
  36. }
  37. if ($exceptionClass !== null) {
  38. $this->expectException($exceptionClass);
  39. $this->expectExceptionMessage($exceptionMessage);
  40. }
  41. static::assertIsResource($resource);
  42. $zipFile = new ZipFile();
  43. $zipFile->openFromStream($resource);
  44. static::assertTrue(fclose($resource));
  45. }
  46. public function provideStreams(): array
  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. * @return resource
  66. */
  67. private function getTempResource(string $filename)
  68. {
  69. $fp = fopen(__DIR__ . '/resources/apk.zip', 'rb');
  70. $stream = fopen($filename, 'r+b');
  71. stream_copy_to_stream($fp, $stream);
  72. fclose($fp);
  73. rewind($stream);
  74. return $stream;
  75. }
  76. }