ZipTestCase.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PHPUnit\Framework\TestCase;
  4. use PhpZip\Constants\ZipConstants;
  5. use PhpZip\Util\FilesUtil;
  6. /**
  7. * PHPUnit test case and helper methods.
  8. */
  9. abstract class ZipTestCase extends TestCase
  10. {
  11. /** @var string */
  12. protected $outputFilename;
  13. /** @var string */
  14. protected $outputDirname;
  15. /**
  16. * Before test.
  17. *
  18. * @noinspection PhpMissingParentCallCommonInspection
  19. */
  20. protected function setUp()
  21. {
  22. $id = uniqid('phpzip', false);
  23. $tempDir = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'phpunit-phpzip';
  24. if (!is_dir($tempDir) && !mkdir($tempDir, 0755, true) && !is_dir($tempDir)) {
  25. throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempDir));
  26. }
  27. $this->outputFilename = $tempDir . \DIRECTORY_SEPARATOR . $id . '.zip';
  28. $this->outputDirname = $tempDir . \DIRECTORY_SEPARATOR . $id;
  29. }
  30. /**
  31. * After test.
  32. */
  33. protected function tearDown()
  34. {
  35. parent::tearDown();
  36. if ($this->outputFilename !== null && file_exists($this->outputFilename)) {
  37. unlink($this->outputFilename);
  38. }
  39. if ($this->outputDirname !== null && is_dir($this->outputDirname)) {
  40. FilesUtil::removeDir($this->outputDirname);
  41. }
  42. }
  43. /**
  44. * Assert correct zip archive.
  45. *
  46. * @param string $filename
  47. * @param string|null $password
  48. */
  49. public static function assertCorrectZipArchive($filename, $password = null)
  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. /**
  68. * @param string $filename
  69. * @param string|null $password
  70. */
  71. private static function assertCorrectZipArchiveFrom7z($filename, $password = null)
  72. {
  73. $command = '7z t';
  74. if ($password !== null) {
  75. $command .= ' -p' . escapeshellarg($password);
  76. }
  77. $command .= ' ' . escapeshellarg($filename) . ' 2>&1';
  78. exec($command, $output, $returnCode);
  79. $output = implode(\PHP_EOL, $output);
  80. static::assertSame($returnCode, 0);
  81. static::assertNotContains(' Errors', $output);
  82. static::assertContains(' Ok', $output);
  83. }
  84. /**
  85. * @param string $filename
  86. * @param string|null $password
  87. */
  88. private static function assertCorrectZipArchiveFromUnzip($filename, $password = null)
  89. {
  90. $command = 'unzip';
  91. if ($password !== null) {
  92. $command .= ' -P ' . escapeshellarg($password);
  93. }
  94. $command .= ' -t ' . escapeshellarg($filename) . ' 2>&1';
  95. exec($command, $output, $returnCode);
  96. $output = implode(\PHP_EOL, $output);
  97. if ($password !== null && $returnCode === 81) {
  98. fwrite(\STDERR, 'Program unzip cannot support this function.' . \PHP_EOL);
  99. fwrite(\STDERR, 'You have to install 7-zip to complete this test.' . \PHP_EOL);
  100. fwrite(\STDERR, 'Install 7-Zip on Ubuntu: sudo apt-get install p7zip-full' . \PHP_EOL);
  101. fwrite(\STDERR, 'Install 7-Zip on Windows: https://www.7-zip.org/download.html' . \PHP_EOL);
  102. return;
  103. }
  104. static::assertSame($returnCode, 0, $output);
  105. static::assertNotContains('incorrect password', $output);
  106. static::assertContains(' OK', $output);
  107. static::assertContains('No errors', $output);
  108. }
  109. /**
  110. * @param string $program
  111. * @param array $successCodes
  112. *
  113. * @return bool
  114. */
  115. protected static function existsProgram($program, array $successCodes = [0])
  116. {
  117. $command = \DIRECTORY_SEPARATOR === '\\' ?
  118. escapeshellarg($program) :
  119. 'which ' . escapeshellarg($program);
  120. $command .= ' 2>&1';
  121. exec($command, $output, $returnCode);
  122. return \in_array($returnCode, $successCodes, true);
  123. }
  124. /**
  125. * Assert correct empty zip archive.
  126. *
  127. * @param $filename
  128. */
  129. public static function assertCorrectEmptyZip($filename)
  130. {
  131. if (self::existsProgram('zipinfo')) {
  132. exec('zipinfo ' . escapeshellarg($filename), $output, $returnCode);
  133. $output = implode(\PHP_EOL, $output);
  134. static::assertContains('Empty zipfile', $output);
  135. }
  136. $actualEmptyZipData = pack('VVVVVv', ZipConstants::END_CD, 0, 0, 0, 0, 0);
  137. static::assertStringEqualsFile($filename, $actualEmptyZipData);
  138. }
  139. /**
  140. * @param string $filename
  141. * @param bool $showErrors
  142. *
  143. * @return bool|null If null returned, then the zipalign program is not installed
  144. */
  145. public static function assertVerifyZipAlign($filename, $showErrors = false)
  146. {
  147. if (self::existsProgram('zipalign', [0, 2])) {
  148. exec('zipalign -c -v 4 ' . escapeshellarg($filename), $output, $returnCode);
  149. if ($showErrors && $returnCode !== 0) {
  150. fwrite(\STDERR, implode(\PHP_EOL, $output));
  151. }
  152. return $returnCode === 0;
  153. }
  154. fwrite(\STDERR, "Cannot find the program 'zipalign' for the test" . \PHP_EOL);
  155. fwrite(\STDERR, 'To fix this, install zipalign.' . \PHP_EOL);
  156. fwrite(\STDERR, \PHP_EOL);
  157. fwrite(\STDERR, 'Install on Ubuntu: sudo apt-get install zipalign' . \PHP_EOL);
  158. fwrite(\STDERR, \PHP_EOL);
  159. fwrite(\STDERR, 'Install on Windows:' . \PHP_EOL);
  160. fwrite(\STDERR, ' 1. Install Android Studio' . \PHP_EOL);
  161. fwrite(\STDERR, ' 2. Install Android Sdk' . \PHP_EOL);
  162. fwrite(\STDERR, ' 3. Add zipalign path to \$Path' . \PHP_EOL);
  163. return null;
  164. }
  165. /**
  166. * @return bool
  167. */
  168. public static function skipTestForRootUser()
  169. {
  170. /** @noinspection PhpComposerExtensionStubsInspection */
  171. if (\extension_loaded('posix') && posix_getuid() === 0) {
  172. static::markTestSkipped('Skip the test for a user with root privileges');
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * @return bool
  179. */
  180. public static function skipTestForWindows()
  181. {
  182. if (\DIRECTORY_SEPARATOR === '\\') {
  183. static::markTestSkipped('Skip on Windows');
  184. return true;
  185. }
  186. return false;
  187. }
  188. }