2
0

ZipTestCase.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\Constants\ZipConstants;
  12. use PhpZip\Util\FilesUtil;
  13. /**
  14. * PHPUnit test case and helper methods.
  15. */
  16. abstract class ZipTestCase extends TestCase
  17. {
  18. protected string $outputFilename;
  19. protected string $outputDirname;
  20. /**
  21. * Before test.
  22. *
  23. * @noinspection PhpMissingParentCallCommonInspection
  24. */
  25. protected function setUp(): void
  26. {
  27. $id = uniqid('phpzip', false);
  28. $tempDir = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'phpunit-phpzip';
  29. if (!is_dir($tempDir) && !mkdir($tempDir, 0755, true) && !is_dir($tempDir)) {
  30. throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempDir));
  31. }
  32. $this->outputFilename = $tempDir . \DIRECTORY_SEPARATOR . $id . '.zip';
  33. $this->outputDirname = $tempDir . \DIRECTORY_SEPARATOR . $id;
  34. }
  35. /**
  36. * After test.
  37. */
  38. protected function tearDown(): void
  39. {
  40. parent::tearDown();
  41. if ($this->outputFilename !== null && file_exists($this->outputFilename)) {
  42. unlink($this->outputFilename);
  43. }
  44. if ($this->outputDirname !== null && is_dir($this->outputDirname)) {
  45. FilesUtil::removeDir($this->outputDirname);
  46. }
  47. }
  48. /**
  49. * Assert correct zip archive.
  50. *
  51. * @param ?string $password
  52. */
  53. public static function assertCorrectZipArchive(string $filename, ?string $password = null): void
  54. {
  55. if (self::existsProgram('7z')) {
  56. self::assertCorrectZipArchiveFrom7z($filename, $password);
  57. } elseif (self::existsProgram('unzip')) {
  58. self::assertCorrectZipArchiveFromUnzip($filename, $password);
  59. } else {
  60. fwrite(\STDERR, 'Skipped testing the zip archive for errors using third-party utilities.' . \PHP_EOL);
  61. fwrite(\STDERR, 'To fix this, install 7-zip or unzip.' . \PHP_EOL);
  62. fwrite(\STDERR, \PHP_EOL);
  63. fwrite(\STDERR, 'Install on Ubuntu: sudo apt-get install p7zip-full unzip' . \PHP_EOL);
  64. fwrite(\STDERR, \PHP_EOL);
  65. fwrite(\STDERR, 'Install on Windows:' . \PHP_EOL);
  66. fwrite(\STDERR, ' * 7-zip - https://www.7-zip.org/download.html' . \PHP_EOL);
  67. fwrite(\STDERR, ' * unzip - http://gnuwin32.sourceforge.net/packages/unzip.htm' . \PHP_EOL);
  68. fwrite(\STDERR, \PHP_EOL);
  69. }
  70. }
  71. private static function assertCorrectZipArchiveFrom7z(string $filename, ?string $password = null): void
  72. {
  73. $command = '7z t';
  74. if ($password !== null) {
  75. $command .= ' -p' . escapeshellarg($password);
  76. }
  77. $command .= ' ' . escapeshellarg($filename) . ' 2>&1';
  78. exec($command, $outputLines, $returnCode);
  79. $output = implode(\PHP_EOL, $outputLines);
  80. static::assertSame($returnCode, 0);
  81. static::assertStringNotContainsString(' Errors', $output);
  82. static::assertStringContainsString(' Ok', $output);
  83. }
  84. private static function assertCorrectZipArchiveFromUnzip(string $filename, ?string $password = null): void
  85. {
  86. $command = 'unzip';
  87. if ($password !== null) {
  88. $command .= ' -P ' . escapeshellarg($password);
  89. }
  90. $command .= ' -t ' . escapeshellarg($filename) . ' 2>&1';
  91. exec($command, $outputLines, $returnCode);
  92. $output = implode(\PHP_EOL, $outputLines);
  93. if ($password !== null && $returnCode === 81) {
  94. fwrite(\STDERR, 'Program unzip cannot support this function.' . \PHP_EOL);
  95. fwrite(\STDERR, 'You have to install 7-zip to complete this test.' . \PHP_EOL);
  96. fwrite(\STDERR, 'Install 7-Zip on Ubuntu: sudo apt-get install p7zip-full' . \PHP_EOL);
  97. fwrite(\STDERR, 'Install 7-Zip on Windows: https://www.7-zip.org/download.html' . \PHP_EOL);
  98. return;
  99. }
  100. static::assertSame($returnCode, 0, $output);
  101. static::assertStringNotContainsString('incorrect password', $output);
  102. static::assertStringContainsString(' OK', $output);
  103. static::assertStringContainsString('No errors', $output);
  104. }
  105. protected static function existsProgram(string $program, array $successCodes = [0]): bool
  106. {
  107. $command = \DIRECTORY_SEPARATOR === '\\'
  108. ? escapeshellarg($program)
  109. : 'command -v ' . escapeshellarg($program);
  110. $command .= ' 2>&1';
  111. exec($command, $output, $returnCode);
  112. return \in_array($returnCode, $successCodes, true);
  113. }
  114. /**
  115. * Assert correct empty zip archive.
  116. *
  117. * @param $filename
  118. */
  119. public static function assertCorrectEmptyZip($filename): void
  120. {
  121. if (self::existsProgram('zipinfo')) {
  122. exec('zipinfo ' . escapeshellarg($filename), $outputLines, $returnCode);
  123. $output = implode(\PHP_EOL, $outputLines);
  124. static::assertStringContainsString('Empty zipfile', $output);
  125. }
  126. $actualEmptyZipData = pack('VVVVVv', ZipConstants::END_CD, 0, 0, 0, 0, 0);
  127. static::assertStringEqualsFile($filename, $actualEmptyZipData);
  128. }
  129. public static function skipTestForRootUser(): void
  130. {
  131. /** @noinspection PhpComposerExtensionStubsInspection */
  132. if (\extension_loaded('posix') && posix_getuid() === 0) {
  133. static::markTestSkipped('Skip the test for a user with root privileges');
  134. }
  135. }
  136. public static function skipTestForWindows(): void
  137. {
  138. if (\DIRECTORY_SEPARATOR === '\\') {
  139. static::markTestSkipped('Skip on Windows');
  140. }
  141. }
  142. }