ZipTestCase.php 5.6 KB

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