Skip to content

Commit

Permalink
Wrap function_exists in ServerConfigChecks to allow testing
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Čihař <michal@cihar.com>
  • Loading branch information
nijel committed Sep 5, 2017
1 parent a43dc28 commit 94219ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
24 changes: 18 additions & 6 deletions libraries/classes/Config/ServerConfigChecks.php
Expand Up @@ -292,7 +292,7 @@ protected function performConfigChecksServersZipdump() {
// $cfg['ZipDump']
// requires zip_open in import
//
if ($this->cfg->getValue('ZipDump') && !@function_exists('zip_open')) {
if ($this->cfg->getValue('ZipDump') && !$this->functionExists('zip_open')) {
PMA_messagesSet(
'error',
'ZipDump_import',
Expand All @@ -313,7 +313,7 @@ protected function performConfigChecksServersZipdump() {
// $cfg['ZipDump']
// requires gzcompress in export
//
if ($this->cfg->getValue('ZipDump') && !@function_exists('gzcompress')) {
if ($this->cfg->getValue('ZipDump') && !$this->functionExists('gzcompress')) {
PMA_messagesSet(
'error',
'ZipDump_export',
Expand Down Expand Up @@ -487,12 +487,12 @@ protected function performConfigChecksServerBZipdump()
// requires bzip2 functions
//
if ($this->cfg->getValue('BZipDump')
&& (!@function_exists('bzopen') || !@function_exists('bzcompress'))
&& (!$this->functionExists('bzopen') || !$this->functionExists('bzcompress'))
) {
$functions = @function_exists('bzopen')
$functions = $this->functionExists('bzopen')
? '' :
'bzopen';
$functions .= @function_exists('bzcompress')
$functions .= $this->functionExists('bzcompress')
? ''
: ($functions ? ', ' : '') . 'bzcompress';
PMA_messagesSet(
Expand Down Expand Up @@ -526,7 +526,7 @@ protected function performConfigChecksServerGZipdump()
// requires zlib functions
//
if ($this->cfg->getValue('GZipDump')
&& (@!function_exists('gzopen') || @!function_exists('gzencode'))
&& (!$this->functionExists('gzopen') || !$this->functionExists('gzencode'))
) {
PMA_messagesSet(
'error',
Expand All @@ -544,4 +544,16 @@ protected function performConfigChecksServerGZipdump()
);
}
}

/**
* Wrapper around function_exists to allow mock in test
*
* @param string $name Function name
*
* @return boolean
*/
protected function functionExists($name)
{
return @function_exists($name);
}
}
27 changes: 11 additions & 16 deletions test/classes/config/ServerConfigChecksTest.php
Expand Up @@ -37,7 +37,7 @@ public function setUp()
unset($_SESSION[$this->sessionID]);
}

public function testErrors()
public function testManyErrors()
{
$_SESSION[$this->sessionID]['Servers'] = array(
'1' => array(
Expand All @@ -61,23 +61,14 @@ public function testErrors()
$_SESSION[$this->sessionID]['BZipDump'] = true;
$_SESSION[$this->sessionID]['ZipDump'] = true;

if (@!function_exists('gzopen') || @!function_exists('gzencode')) {
$errorArrayKeys[] = 'GZipDump';
}
$configChecker = $this->getMockbuilder('PhpMyAdmin\Config\ServerConfigChecks')
->setMethods(['functionExists'])
->setConstructorArgs([$GLOBALS['ConfigFile']])
->getMock();

if (@!function_exists('bzopen') || @!function_exists('bzcompress')) {
$errorArrayKeys[] = 'BZipDump';
}
// Configure the stub.
$configChecker->method('functionExists')->willReturn(False);

if (!@function_exists('zip_open')) {
$errorArrayKeys[] = 'ZipDump_import';
}

if (!@function_exists('gzcompress')) {
$errorArrayKeys[] = 'ZipDump_export';
}

$configChecker = new ServerConfigChecks($GLOBALS['ConfigFile']);
$configChecker->performConfigChecks();

$this->assertEquals(
Expand All @@ -97,6 +88,10 @@ public function testErrors()
$this->assertEquals(
array(
'LoginCookieValidity',
'GZipDump',
'BZipDump',
'ZipDump_import',
'ZipDump_export',
),
array_keys($_SESSION['messages']['error'])
);
Expand Down

0 comments on commit 94219ff

Please sign in to comment.