ZipTestCase.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. public static function assertCorrectZipArchive(string $filename, ?string $password = null): void
  50. {
  51. if (self::existsProgram('7z')) {
  52. self::assertCorrectZipArchiveFrom7z($filename, $password);
  53. } elseif (self::existsProgram('unzip')) {
  54. self::assertCorrectZipArchiveFromUnzip($filename, $password);
  55. } else {
  56. fwrite(\STDERR, 'Skipped testing the zip archive for errors using third-party utilities.' . \PHP_EOL);
  57. fwrite(\STDERR, 'To fix this, install 7-zip or unzip.' . \PHP_EOL);
  58. fwrite(\STDERR, \PHP_EOL);
  59. fwrite(\STDERR, 'Install on Ubuntu: sudo apt-get install p7zip-full unzip' . \PHP_EOL);
  60. fwrite(\STDERR, \PHP_EOL);
  61. fwrite(\STDERR, 'Install on Windows:' . \PHP_EOL);
  62. fwrite(\STDERR, ' * 7-zip - https://www.7-zip.org/download.html' . \PHP_EOL);
  63. fwrite(\STDERR, ' * unzip - http://gnuwin32.sourceforge.net/packages/unzip.htm' . \PHP_EOL);
  64. fwrite(\STDERR, \PHP_EOL);
  65. }
  66. }
  67. private static function assertCorrectZipArchiveFrom7z(string $filename, ?string $password = null): void
  68. {
  69. $command = '7z t';
  70. if ($password !== null) {
  71. $command .= ' -p' . escapeshellarg($password);
  72. }
  73. $command .= ' ' . escapeshellarg($filename) . ' 2>&1';
  74. exec($command, $outputLines, $returnCode);
  75. $output = implode(\PHP_EOL, $outputLines);
  76. static::assertSame($returnCode, 0);
  77. static::assertStringNotContainsString(' Errors', $output);
  78. static::assertStringContainsString(' Ok', $output);
  79. }
  80. private static function assertCorrectZipArchiveFromUnzip(string $filename, ?string $password = null): void
  81. {
  82. $command = 'unzip';
  83. if ($password !== null) {
  84. $command .= ' -P ' . escapeshellarg($password);
  85. }
  86. $command .= ' -t ' . escapeshellarg($filename) . ' 2>&1';
  87. exec($command, $outputLines, $returnCode);
  88. $output = implode(\PHP_EOL, $outputLines);
  89. if ($password !== null && $returnCode === 81) {
  90. fwrite(\STDERR, 'Program unzip cannot support this function.' . \PHP_EOL);
  91. fwrite(\STDERR, 'You have to install 7-zip to complete this test.' . \PHP_EOL);
  92. fwrite(\STDERR, 'Install 7-Zip on Ubuntu: sudo apt-get install p7zip-full' . \PHP_EOL);
  93. fwrite(\STDERR, 'Install 7-Zip on Windows: https://www.7-zip.org/download.html' . \PHP_EOL);
  94. return;
  95. }
  96. static::assertSame($returnCode, 0, $output);
  97. static::assertStringNotContainsString('incorrect password', $output);
  98. static::assertStringContainsString(' OK', $output);
  99. static::assertStringContainsString('No errors', $output);
  100. }
  101. protected static function existsProgram(string $program, array $successCodes = [0]): bool
  102. {
  103. $command = \DIRECTORY_SEPARATOR === '\\'
  104. ? escapeshellarg($program)
  105. : 'command -v ' . escapeshellarg($program);
  106. $command .= ' 2>&1';
  107. exec($command, $output, $returnCode);
  108. return \in_array($returnCode, $successCodes, true);
  109. }
  110. /**
  111. * Assert correct empty zip archive.
  112. *
  113. * @param mixed $filename
  114. */
  115. public static function assertCorrectEmptyZip($filename): void
  116. {
  117. if (self::existsProgram('zipinfo')) {
  118. exec('zipinfo ' . escapeshellarg($filename), $outputLines, $returnCode);
  119. $output = implode(\PHP_EOL, $outputLines);
  120. static::assertStringContainsString('Empty zipfile', $output);
  121. }
  122. $actualEmptyZipData = pack('VVVVVv', ZipConstants::END_CD, 0, 0, 0, 0, 0);
  123. static::assertStringEqualsFile($filename, $actualEmptyZipData);
  124. }
  125. public static function skipTestForRootUser(): void
  126. {
  127. /** @noinspection PhpComposerExtensionStubsInspection */
  128. if (\extension_loaded('posix') && posix_getuid() === 0) {
  129. static::markTestSkipped('Skip the test for a user with root privileges');
  130. }
  131. }
  132. public static function skipTestForWindows(): void
  133. {
  134. if (\DIRECTORY_SEPARATOR === '\\') {
  135. static::markTestSkipped('Skip on Windows');
  136. }
  137. }
  138. }