Re: PEAR_Info TestSuite with PHPUnit 3.1.x

From: Date: Sun, 29 Jul 2007 09:34:42 +0000
Subject: Re: PEAR_Info TestSuite with PHPUnit 3.1.x
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-47718@lists.php.net to get a copy of this message
Laurent Laville a écrit :
Laurent Laville a écrit :
To resume: If you want to use PEAR/Config.php file, take care to call it once before 'PHPUnit/Framework.php'. Because PHPUnit/Framework.php call others files that in some of them include the PEAR/Config.
No way here ! When we run phpunit on command line, PHPUnit/TextUI/Command.php, load itself some class file that will use PEAR/Config. No solution is possible
Finally after a long and peace night, I've found a solution ! I think it's the only one. If you want to use PEAR/Config and simulate default system config dir, by setting the "PHP_PEAR_SYSCONF_DIR" constant, there is no way to do it due to usage of PEAR/Config itself in PHPUnit (see my previous post). So there is no choice than using the real system config dir itself. Test suite should backup data in setUp() testcase method, that will be restore later by tearDown() tescase method (see example below). The user or system config file will be modified during test suite. It's the only solution I've found to solve include (once) of PEAR/Config resource. Example I'll implement later in PEAR_Info testcase will be something like : File "PEAR_Info_TestCase_DefaultConfig.php" <?php // ... require_once '..' . DIRECTORY_SEPARATOR . 'Info.php'; class PEAR_Info_TestCase_DefaultConfig extends PHPUnit_Framework_TestCase {
    private $config = array();
    // Backup PEAR 'php_dir' setting in system config file
    protected function setUp()
    {
        chdir(dirname(__FILE__));
        $config =& PEAR_Config::singleton();
        $layer = 'user';
        $conf_file = $config->getConfFile($layer);
        if (!file_exists($conf_file)) {
            $layer = 'system';
            $conf_file = $config->getConfFile($layer);
        }
        if (file_exists($conf_file)) {
            $this->config = array('pear_dir' => $config->get('php_dir'), 'layer' => $layer);
            $config->set('php_dir', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'pear_dir');
            $config->writeConfigFile($conf_file, $layer);
        }
    }
    // Restore PEAR 'php_dir' setting in system config file
    protected function tearDown()
    {
        if (isset($this->config['pear_dir'])) {
            $config =& PEAR_Config::singleton();
            $config->set('php_dir', $this->config['pear_dir']);
            $conf_file = $config->getConfFile($this->config['layer']);
            $config->writeConfigFile($conf_file, $this->config['layer']);
            $this->config = array();
        }
    }
    // ...
} // ... ?> Comment are welcome !

« previous php.pear.dev (#47718) next »