2
0

ZipTestCase.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace PhpZip;
  3. /**
  4. * PHPUnit test case and helper methods.
  5. */
  6. class ZipTestCase extends \PHPUnit_Framework_TestCase
  7. {
  8. /**
  9. * Assert correct zip archive.
  10. *
  11. * @param $filename
  12. */
  13. public static function assertCorrectZipArchive($filename)
  14. {
  15. if (DIRECTORY_SEPARATOR !== '\\' && `which zip`) {
  16. exec("zip -T " . escapeshellarg($filename), $output, $returnCode);
  17. $output = implode(PHP_EOL, $output);
  18. self::assertEquals($returnCode, 0);
  19. self::assertNotContains('zip error', $output);
  20. self::assertContains(' OK', $output);
  21. }
  22. }
  23. /**
  24. * Assert correct empty zip archive.
  25. *
  26. * @param $filename
  27. */
  28. public static function assertCorrectEmptyZip($filename)
  29. {
  30. if (DIRECTORY_SEPARATOR !== '\\' && `which zipinfo`) {
  31. exec("zipinfo " . escapeshellarg($filename), $output, $returnCode);
  32. $output = implode(PHP_EOL, $output);
  33. self::assertContains('Empty zipfile', $output);
  34. }
  35. $actualEmptyZipData = pack('VVVVVv', ZipConstants::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0);
  36. self::assertEquals(file_get_contents($filename), $actualEmptyZipData);
  37. }
  38. /**
  39. * @param string $filename
  40. * @return bool|null If null - can not install zipalign
  41. */
  42. public static function doZipAlignVerify($filename)
  43. {
  44. if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
  45. exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
  46. return $returnCode === 0;
  47. } else {
  48. echo 'Can not find program "zipalign" for test' . PHP_EOL;
  49. fwrite(STDERR, 'Can not find program "zipalign" for test');
  50. return null;
  51. }
  52. }
  53. }