1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-08-01 13:10:09 +02:00

Replaced the execution operator backticks with the function call exec in tests

This commit is contained in:
Ne-Lexa
2019-03-13 15:14:01 +03:00
parent c9871c9f80
commit b3277fcc5c

View File

@@ -10,7 +10,6 @@ use PhpZip\Util\FilesUtil;
*/ */
class ZipTestCase extends \PHPUnit_Framework_TestCase class ZipTestCase extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var string * @var string
*/ */
@@ -27,12 +26,10 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
{ {
parent::setUp(); parent::setUp();
$id = uniqid('phpzip'); $id = uniqid('phpzip', true);
$tempDir = sys_get_temp_dir() . '/phpunit-phpzip'; $tempDir = sys_get_temp_dir() . '/phpunit-phpzip';
if (!is_dir($tempDir)) { if (!is_dir($tempDir) && !mkdir($tempDir, 0755, true) && !is_dir($tempDir)) {
if (!mkdir($tempDir, 0755, true)) { throw new \RuntimeException('Dir ' . $tempDir . " can't created");
throw new \RuntimeException("Dir " . $tempDir . " can't created");
}
} }
$this->outputFilename = $tempDir . '/' . $id . '.zip'; $this->outputFilename = $tempDir . '/' . $id . '.zip';
$this->outputDirname = $tempDir . '/' . $id; $this->outputDirname = $tempDir . '/' . $id;
@@ -61,23 +58,23 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
*/ */
public static function assertCorrectZipArchive($filename, $password = null) public static function assertCorrectZipArchive($filename, $password = null)
{ {
if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) { if (self::existsProgram('unzip')) {
$command = "unzip"; $command = 'unzip';
if ($password !== null) { if ($password !== null) {
$command .= " -P " . escapeshellarg($password); $command .= ' -P ' . escapeshellarg($password);
} }
$command .= " -t " . escapeshellarg($filename); $command .= ' -t ' . escapeshellarg($filename);
exec($command, $output, $returnCode); exec($command, $output, $returnCode);
$output = implode(PHP_EOL, $output); $output = implode(PHP_EOL, $output);
if ($password !== null && $returnCode === 81) { if ($password !== null && $returnCode === 81) {
if (`which 7z`) { if (self::existsProgram('7z')) {
// WinZip 99-character limit // WinZip 99-character limit
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/ // @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
$password = substr($password, 0, 99); $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); exec($command, $output, $returnCode);
/** /**
* @var array $output * @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. * Assert correct empty zip archive.
* *
@@ -107,15 +117,15 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
*/ */
public static function assertCorrectEmptyZip($filename) public static function assertCorrectEmptyZip($filename)
{ {
if (DIRECTORY_SEPARATOR !== '\\' && `which zipinfo`) { if (self::existsProgram('zipinfo')) {
exec("zipinfo " . escapeshellarg($filename), $output, $returnCode); exec('zipinfo ' . escapeshellarg($filename), $output, $returnCode);
$output = implode(PHP_EOL, $output); $output = implode(PHP_EOL, $output);
self::assertContains('Empty zipfile', $output); self::assertContains('Empty zipfile', $output);
} }
$actualEmptyZipData = pack('VVVVVv', EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0); $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) public static function assertVerifyZipAlign($filename, $showErrors = false)
{ {
if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) { if (self::existsProgram('zipalign')) {
exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode); exec('zipalign -c -v 4 ' . escapeshellarg($filename), $output, $returnCode);
if ($showErrors && $returnCode !== 0) { if ($showErrors && $returnCode !== 0) {
fwrite(STDERR, implode(PHP_EOL, $output)); fwrite(STDERR, implode(PHP_EOL, $output));
} }
return $returnCode === 0; 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;
} }
} }