2
0

ZipStreamOpenTest.php 2.2 KB

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