ZipTestCase.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. $this->outputFilename = sys_get_temp_dir() . '/' . $id . '.zip';
  26. $this->outputDirname = sys_get_temp_dir() . '/' . $id;
  27. }
  28. /**
  29. * After test
  30. */
  31. protected function tearDown()
  32. {
  33. parent::tearDown();
  34. if ($this->outputFilename !== null && file_exists($this->outputFilename)) {
  35. unlink($this->outputFilename);
  36. }
  37. if ($this->outputDirname !== null && is_dir($this->outputDirname)) {
  38. FilesUtil::removeDir($this->outputDirname);
  39. }
  40. }
  41. /**
  42. * Assert correct zip archive.
  43. *
  44. * @param string $filename
  45. * @param string|null $password
  46. */
  47. public static function assertCorrectZipArchive($filename, $password = null)
  48. {
  49. if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) {
  50. $command = "unzip";
  51. if ($password !== null) {
  52. $command .= " -P " . escapeshellarg($password);
  53. }
  54. $command .= " -t " . escapeshellarg($filename);
  55. exec($command, $output, $returnCode);
  56. $output = implode(PHP_EOL, $output);
  57. if ($password !== null && $returnCode === 81) {
  58. if (`which 7z`) {
  59. // WinZip 99-character limit
  60. // @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
  61. $password = substr($password, 0, 99);
  62. $command = "7z t -p" . escapeshellarg($password) . " " . escapeshellarg($filename);
  63. exec($command, $output, $returnCode);
  64. $output = implode(PHP_EOL, $output);
  65. self::assertEquals($returnCode, 0);
  66. self::assertNotContains(' Errors', $output);
  67. self::assertContains(' Ok', $output);
  68. } else {
  69. fwrite(STDERR, 'Program unzip cannot support this function.' . PHP_EOL);
  70. fwrite(STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full' . PHP_EOL);
  71. }
  72. } else {
  73. self::assertEquals($returnCode, 0, $output);
  74. self::assertNotContains('incorrect password', $output);
  75. self::assertContains(' OK', $output);
  76. self::assertContains('No errors', $output);
  77. }
  78. }
  79. }
  80. /**
  81. * Assert correct empty zip archive.
  82. *
  83. * @param $filename
  84. */
  85. public static function assertCorrectEmptyZip($filename)
  86. {
  87. if (DIRECTORY_SEPARATOR !== '\\' && `which zipinfo`) {
  88. exec("zipinfo " . escapeshellarg($filename), $output, $returnCode);
  89. $output = implode(PHP_EOL, $output);
  90. self::assertContains('Empty zipfile', $output);
  91. }
  92. $actualEmptyZipData = pack('VVVVVv', EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0);
  93. self::assertEquals(file_get_contents($filename), $actualEmptyZipData);
  94. }
  95. /**
  96. * @param string $filename
  97. * @param bool $showErrors
  98. * @return bool|null If null - can not install zipalign
  99. */
  100. public static function doZipAlignVerify($filename, $showErrors = false)
  101. {
  102. if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
  103. exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
  104. if ($showErrors && $returnCode !== 0) fwrite(STDERR, implode(PHP_EOL, $output));
  105. return $returnCode === 0;
  106. } else {
  107. fwrite(STDERR, 'Can not find program "zipalign" for test');
  108. return null;
  109. }
  110. }
  111. }