cvs: pear /PEAR_PackageUpdate/tests customConfig.phpt

[email protected] ("Laurent Laville")
Newsgroups php.pear.cvs
Message-ID <cvsfarell1209415093@cvsserver>
farell		Mon Apr 28 20:38:13 2008 UTC

  Added files:                 
    /pear/PEAR_PackageUpdate/tests	customConfig.phpt 
  Log:
  initial commit of PEAR_PackageUpdate using custom configuration test case (with .phpt)
  

http://cvs.php.net/viewvc.cgi/pear/PEAR_PackageUpdate/tests/customConfig.phpt?view=markup&rev=1.1
Index: pear/PEAR_PackageUpdate/tests/customConfig.phpt
+++ pear/PEAR_PackageUpdate/tests/customConfig.phpt
--TEST--
PEAR_PackageUpdate using custom configuration
--FILE--
<?php
error_reporting(E_ALL & ~E_NOTICE);

require_once 'PEAR/Config.php';

$config =& PEAR_Config::singleton();
$cfgDir = $config->get('cfg_dir');  // available only since PEAR 1.7.0

$ds         = DIRECTORY_SEPARATOR;
$dir        = dirname(__FILE__);
$sysconfdir = $dir . $ds . 'sysconf_dir';
$peardir    = $dir . $ds . 'pear_dir';

if (!is_null($cfgDir) && is_dir($cfgDir)) {
    $pearcfgdir = $peardir . $ds . 'cfg';
} else {
    $pearcfgdir = $sysconfdir;
}

putenv("PHP_PEAR_SYSCONF_DIR=" . $sysconfdir);
chdir($dir);

// we get PEAR_PackageUpdate class only here due to setting of PEAR_CONFIG_SYSCONFDIR
require_once 'PEAR/PackageUpdate.php';

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $system_file = $sysconfdir . $ds . 'pearsys.ini';
    $pref_file   = $pearcfgdir . $ds . 'ppurc.ini';
} else {
    $system_file = $sysconfdir . $ds . 'pear.conf';
    $pref_file   = $pearcfgdir . $ds . '.ppurc';
}
$user_file = '';

if (!file_exists($system_file)) {
    // write once PEAR system-wide config file for simulation
    $config->set('php_dir', $peardir);
    if (!is_null($cfgDir)) {
        // only for PEAR 1.7.0 or greater
        $config->set('cfg_dir', $pearcfgdir);
    }
    $config->writeConfigFile($system_file);
}

/**
 * TestCase 1:
 * Test invalid driver name (misspell or does not exist)
 *
 * Will use the pear config files into the default system directory
 * (PEAR_CONFIG_SYSCONFDIR).
 */
$testCase = 'testWrongDriver';

$ppu =& PEAR_PackageUpdate::factory('Null', 'Text_Diff', 'pear');

$result = ($ppu === false) ? 'KO' : 'OK';

echo $testCase . ' initClass : ' . $result;
echo "\n";

/**
 * TestCase 2:
 * Test a preference PPU file that does not exist
 *
 * Must use the pear config files into the default system directory
 * (PEAR_CONFIG_SYSCONFDIR).
 */
$testCase = 'testWrongPrefFile';

$ppu =& PEAR_PackageUpdate::factory('Cli', 'Text_Diff', 'pear',
                                    '', '', $pearcfgdir . $ds . 'wrong_' . basename($pref_file));

$result = '??';

if ($ppu !== false && $ppu->hasErrors()) {
    $e = $ppu->popError();
    if ($e['code'] == PEAR_PACKAGEUPDATE_ERROR_INVALIDINIFILE) {
        $result = 'Preference configuration file does not exist';
    }
}
echo $testCase . ' initClass : ' . $result;
echo "\n";

$result = ($ppu->checkUpdate() === true)
    ? 'OK' : 'No update available for PEAR/Text_Diff';
echo $testCase . ' checkUpdate : ' . $result;
echo "\n";

/**
 * TestCase 3:
 * Test a corrupted preference PPU file (wrong content)
 *
 * Will use the pear config files into the default system directory
 * (PEAR_CONFIG_SYSCONFDIR).
 */
$testCase = 'testCorruptedPrefFile';

$bad_pref_file = $pearcfgdir . $ds . 'corrupted_' . basename($pref_file);
file_put_contents($bad_pref_file, '???');

$ppu =& PEAR_PackageUpdate::factory('Cli', 'Text_Diff', 'pear',
                                    '', '', $bad_pref_file);

$result = '??';

if ($ppu !== false && $ppu->hasErrors()) {
    $e = $ppu->popError();
    if ($e['code'] == PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED) {
        $result = 'Preference configuration file is corrupted';
    }
}
echo $testCase . ' initClass : ' . $result;
echo "\n";

$result = ($ppu->checkUpdate() === true)
    ? 'OK' : 'No update available for PEAR/Text_Diff';
echo $testCase . ' checkUpdate : ' . $result;
?>
--CLEAN--
<?php
require_once 'PEAR/Config.php';

$config =& PEAR_Config::singleton();
$cfgDir = $config->get('cfg_dir');  // available only since PEAR 1.7.0

$ds         = DIRECTORY_SEPARATOR;
$dir        = dirname(__FILE__);
$sysconfdir = $dir . $ds . 'sysconf_dir';
$peardir    = $dir . $ds . 'pear_dir';

if (!is_null($cfgDir) && is_dir($cfgDir)) {
    $pearcfgdir = $peardir . $ds . 'cfg';
} else {
    $pearcfgdir = $sysconfdir;
}

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    $system_file = $sysconfdir . $ds . 'pearsys.ini';
    $pref_file   = $pearcfgdir . $ds . 'ppurc.ini';
} else {
    $system_file = $sysconfdir . $ds . 'pear.conf';
    $pref_file   = $pearcfgdir . $ds . '.ppurc';
}
$bad_pref_file = $pearcfgdir . $ds . 'corrupted_' . basename($pref_file);

unlink ($system_file);
unlink ($pref_file);
unlink ($bad_pref_file);
?>
--EXPECT--
testWrongDriver initClass : KO
testWrongPrefFile initClass : Preference configuration file does not exist
testWrongPrefFile checkUpdate : OK
testCorruptedPrefFile initClass : Preference configuration file is corrupted
testCorruptedPrefFile checkUpdate : OK
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.