ZipTestCase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Model\EndOfCentralDirectory;
  4. use PhpZip\Util\FilesUtil;
  5. /**
  6. * PHPUnit test case and helper methods.
  7. */
  8. class ZipTestCase extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var string
  12. */
  13. protected $outputFilename;
  14. /**
  15. * @var string
  16. */
  17. protected $outputDirname;
  18. /**
  19. * Before test
  20. */
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $id = uniqid('phpzip');
  25. $tempDir = sys_get_temp_dir() . '/phpunit-phpzip';
  26. if (!is_dir($tempDir)) {
  27. if (!mkdir($tempDir, 0755, true)) {
  28. throw new \RuntimeException("Dir " . $tempDir . " can't created");
  29. }
  30. }
  31. $this->outputFilename = $tempDir . '/' . $id . '.zip';
  32. $this->outputDirname = $tempDir . '/' . $id;
  33. }
  34. /**
  35. * After test
  36. */
  37. protected function tearDown()
  38. {
  39. parent::tearDown();
  40. if ($this->outputFilename !== null && file_exists($this->outputFilename)) {
  41. unlink($this->outputFilename);
  42. }
  43. if ($this->outputDirname !== null && is_dir($this->outputDirname)) {
  44. FilesUtil::removeDir($this->outputDirname);
  45. }
  46. }
  47. /**
  48. * Assert correct zip archive.
  49. *
  50. * @param string $filename
  51. * @param string|null $password
  52. */
  53. public static function assertCorrectZipArchive($filename, $password = null)
  54. {
  55. if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) {
  56. $command = "unzip";
  57. if ($password !== null) {
  58. $command .= " -P " . escapeshellarg($password);
  59. }
  60. $command .= " -t " . escapeshellarg($filename);
  61. exec($command, $output, $returnCode);
  62. $output = implode(PHP_EOL, $output);
  63. if ($password !== null && $returnCode === 81) {
  64. if (`which 7z`) {
  65. // WinZip 99-character limit
  66. // @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
  67. $password = substr($password, 0, 99);
  68. $command = "7z t -p" . escapeshellarg($password) . " " . escapeshellarg($filename);
  69. exec($command, $output, $returnCode);
  70. /**
  71. * @var array $output
  72. */
  73. $output = implode(PHP_EOL, $output);
  74. self::assertEquals($returnCode, 0);
  75. self::assertNotContains(' Errors', $output);
  76. self::assertContains(' Ok', $output);
  77. } else {
  78. fwrite(STDERR, 'Program unzip cannot support this function.' . PHP_EOL);
  79. fwrite(STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full' . PHP_EOL);
  80. }
  81. } else {
  82. self::assertEquals($returnCode, 0, $output);
  83. self::assertNotContains('incorrect password', $output);
  84. self::assertContains(' OK', $output);
  85. self::assertContains('No errors', $output);
  86. }
  87. }
  88. }
  89. /**
  90. * Assert correct empty zip archive.
  91. *
  92. * @param $filename
  93. */
  94. public static function assertCorrectEmptyZip($filename)
  95. {
  96. if (DIRECTORY_SEPARATOR !== '\\' && `which zipinfo`) {
  97. exec("zipinfo " . escapeshellarg($filename), $output, $returnCode);
  98. $output = implode(PHP_EOL, $output);
  99. self::assertContains('Empty zipfile', $output);
  100. }
  101. $actualEmptyZipData = pack('VVVVVv', EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0);
  102. self::assertEquals(file_get_contents($filename), $actualEmptyZipData);
  103. }
  104. /**
  105. * @param string $filename
  106. * @param bool $showErrors
  107. * @return bool|null If null - can not install zipalign
  108. */
  109. public static function assertVerifyZipAlign($filename, $showErrors = false)
  110. {
  111. if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
  112. exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
  113. if ($showErrors && $returnCode !== 0) {
  114. fwrite(STDERR, implode(PHP_EOL, $output));
  115. }
  116. return $returnCode === 0;
  117. } else {
  118. fwrite(STDERR, 'Can not find program "zipalign" for test' . PHP_EOL);
  119. return null;
  120. }
  121. }
  122. }