diff --git a/tests/PhpZip/ZipTestCase.php b/tests/PhpZip/ZipTestCase.php index d0eb595..91fd205 100644 --- a/tests/PhpZip/ZipTestCase.php +++ b/tests/PhpZip/ZipTestCase.php @@ -10,7 +10,6 @@ use PhpZip\Util\FilesUtil; */ class ZipTestCase extends \PHPUnit_Framework_TestCase { - /** * @var string */ @@ -27,12 +26,10 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase { parent::setUp(); - $id = uniqid('phpzip'); + $id = uniqid('phpzip', true); $tempDir = sys_get_temp_dir() . '/phpunit-phpzip'; - if (!is_dir($tempDir)) { - if (!mkdir($tempDir, 0755, true)) { - throw new \RuntimeException("Dir " . $tempDir . " can't created"); - } + if (!is_dir($tempDir) && !mkdir($tempDir, 0755, true) && !is_dir($tempDir)) { + throw new \RuntimeException('Dir ' . $tempDir . " can't created"); } $this->outputFilename = $tempDir . '/' . $id . '.zip'; $this->outputDirname = $tempDir . '/' . $id; @@ -61,23 +58,23 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase */ public static function assertCorrectZipArchive($filename, $password = null) { - if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) { - $command = "unzip"; + if (self::existsProgram('unzip')) { + $command = 'unzip'; if ($password !== null) { - $command .= " -P " . escapeshellarg($password); + $command .= ' -P ' . escapeshellarg($password); } - $command .= " -t " . escapeshellarg($filename); + $command .= ' -t ' . escapeshellarg($filename); exec($command, $output, $returnCode); $output = implode(PHP_EOL, $output); if ($password !== null && $returnCode === 81) { - if (`which 7z`) { + if (self::existsProgram('7z')) { // WinZip 99-character limit // @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/ $password = substr($password, 0, 99); - $command = "7z t -p" . escapeshellarg($password) . " " . escapeshellarg($filename); + $command = '7z t -p' . escapeshellarg($password) . ' ' . escapeshellarg($filename); exec($command, $output, $returnCode); /** * @var array $output @@ -100,6 +97,19 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase } } + /** + * @param string $program + * @return bool + */ + private static function existsProgram($program){ + if (DIRECTORY_SEPARATOR !== '\\') { + exec('which ' . escapeshellarg($program), $output, $returnCode); + return $returnCode === 0; + } + // false for Windows + return false; + } + /** * Assert correct empty zip archive. * @@ -107,15 +117,15 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase */ public static function assertCorrectEmptyZip($filename) { - if (DIRECTORY_SEPARATOR !== '\\' && `which zipinfo`) { - exec("zipinfo " . escapeshellarg($filename), $output, $returnCode); + if (self::existsProgram('zipinfo')) { + exec('zipinfo ' . escapeshellarg($filename), $output, $returnCode); $output = implode(PHP_EOL, $output); self::assertContains('Empty zipfile', $output); } $actualEmptyZipData = pack('VVVVVv', EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0); - self::assertEquals(file_get_contents($filename), $actualEmptyZipData); + self::assertStringEqualsFile($filename, $actualEmptyZipData); } /** @@ -125,15 +135,15 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase */ public static function assertVerifyZipAlign($filename, $showErrors = false) { - if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) { - exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode); + if (self::existsProgram('zipalign')) { + exec('zipalign -c -v 4 ' . escapeshellarg($filename), $output, $returnCode); if ($showErrors && $returnCode !== 0) { fwrite(STDERR, implode(PHP_EOL, $output)); } return $returnCode === 0; - } else { - fwrite(STDERR, 'Can not find program "zipalign" for test' . PHP_EOL); - return null; } + + fwrite(STDERR, 'Can not find program "zipalign" for test' . PHP_EOL); + return null; } }