Changeset 1252

Show
Ignore:
Timestamp:
02/23/09 17:28:29 (3 years ago)
Author:
bermi
Message:

Fixing #253. Added unit test for verifying that connection to multiple databases on a model basis works.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/Ak.php

    r1250 r1252  
    19701970    { 
    19711971        static $_config; 
    1972         if (!in_array($environment,Ak::toArray(AK_AVAILABLE_ENVIRONMENTS))) { 
    1973             trigger_error('The environment '.$environment.' is not allowed. Allowed environments: '.AK_AVAILABLE_ENVIRONMENTS); 
     1972        if ($raise_error_if_config_file_not_found && !in_array($environment,Ak::toArray(AK_AVAILABLE_ENVIRONMENTS))) { 
     1973            trigger_error('The environment '.$environment.' is not allowed. Allowed environments: '.AK_AVAILABLE_ENVIRONMENTS, E_USER_ERROR); 
    19741974            return false; 
    19751975        } 
     
    19781978            $_config = new AkConfig(); 
    19791979        } 
    1980         return $_config->get($namespace,$environment,$raise_error_if_config_file_not_found); 
     1980        return $_config->get($namespace, $environment, $raise_error_if_config_file_not_found); 
    19811981    } 
    19821982     
  • trunk/lib/AkActiveRecord/AkDbAdapter.php

    r1245 r1252  
    5555        require_once(AK_CONTRIB_DIR.DS.'adodb'.DS.'adodb.inc.php'); 
    5656        $this->connection = AK_DEBUG ? NewADOConnection($dsn) : @NewADOConnection($dsn); 
     57 
    5758        if (!$this->connection){ 
    5859            error_reporting(E_ALL); 
     
    9495            if (empty($database_specifications)) { 
    9596                $settings_hash = AK_ENVIRONMENT; 
    96                 $database_specifications = Ak::getSettings('database',false,$settings_hash); 
     97                $database_specifications = Ak::getSettings('database', false, $settings_hash); 
    9798            } else if (is_string($database_specifications)){ 
    98                 $environment_settings = Ak::getSettings('database',false,$database_specifications); 
     99                 
     100                $environment_settings = Ak::getSettings('database', false, $database_specifications); 
     101                 
    99102                if (!empty($environment_settings)){ 
    100103                    $database_specifications = $environment_settings; 
  • trunk/lib/AkConfig.php

    r1245 r1252  
    241241            return false; 
    242242        } 
     243 
    243244        require_once(AK_VENDOR_DIR.DS.'TextParsers'.DS.'spyc.php'); 
    244245        $content = file_get_contents($yaml_file_name); 
     
    246247        $config = Spyc::YAMLLoad($content); 
    247248         
    248         if (!is_array($config)) return false; 
     249        if (!is_array($config)){ 
     250            return false; 
     251        } 
    249252         
    250253        $default = isset($config['default'])?$config['default']:array(); 
    251          
    252254         
    253255        $configs = array(); 
     
    257259        $default_environments = array('testing','development','production'); 
    258260        $environments = array_merge($default_environments, $environments); 
     261         
    259262        foreach($environments as $env) { 
    260              
    261263            $envConfig = $this->_merge($default, isset($config[$env])?$config[$env]:array()); 
    262264            $this->_writeCache($envConfig,$namespace,$env,$this->_useWriteCache($environment)); 
     
    264266        } 
    265267         
    266         return isset($configs[$environment])?$configs[$environment]:$default
     268        return isset($configs[$environment]) ? $configs[$environment] : (in_array($environments, $environments) ? $default : false)
    267269         
    268270    } 
     
    285287        } 
    286288        if ($uncached || !($config = $this->_readCache($namespace, $environment))) { 
    287             $config = $this->_readConfig($namespace, $environment,$raise_error_if_config_file_not_found); 
     289            $config = $this->_readConfig($namespace, $environment, $raise_error_if_config_file_not_found); 
    288290        } 
    289291        if (!isset($_configs[$namespace])) { 
  • trunk/lib/constants.php

    r1250 r1252  
    314314defined('AK_BEEP_ON_ERRORS_WHEN_TESTING') ? null : define('AK_BEEP_ON_ERRORS_WHEN_TESTING', false); 
    315315 
    316 AK_DEV_MODE ? require_once(AK_LIB_DIR.DS.'AkDevelopmentErrorHandler.php') : null; 
    317  
     316AK_PRODUCTION_MODE ? null : require_once(AK_LIB_DIR.DS.'AkDevelopmentErrorHandler.php'); 
    318317 
    319318?> 
  • trunk/test/unit/lib/AkActiveRecord/_AkActiveRecord_connection_handling.php

    r1185 r1252  
    2525         
    2626        $this->assertFalse($Model->establishConnection('not_specified_profile')); 
    27         $this->assertError("The environment not_specified_profile is not allowed. Allowed environments: setup,testing,development,production"); 
    2827        $this->assertError("Could not find the database profile 'not_specified_profile' in config/database.yml."); 
    29          
     28 
    3029        $check_default_connection =& AkDbAdapter::getInstance(); 
    3130        $this->assertReference($default_connection,$check_default_connection); 
     
    4342         
    4443    } 
     44     
     45    function test_should_establish_multiple_connections() 
     46    { 
     47        if(AK_PHP5){ 
     48            $db_settings = Ak::convert('yaml', 'array', AK_CONFIG_DIR.DS.'database.yml'); 
     49            $db_settings['sqlite_databases'] = array( 
     50                    'database_file' => AK_TMP_DIR.DS.'testing_sqlite_database.sqlite', 
     51                    'type' => 'sqlite' 
     52                    ); 
     53            file_put_contents(AK_CONFIG_DIR.DS.'database.yml', Ak::convert('array', 'yaml', $db_settings)); 
     54         
     55        
     56            $this->installAndIncludeModels(array('TestOtherConnection')); 
     57 
     58            Ak::import('test_other_connection'); 
     59            $OtherConnection = new TestOtherConnection(array('name'=>'Delia')); 
     60            $this->assertTrue($OtherConnection->save()); 
     61         
     62         
     63            $this->installAndIncludeModels(array('DummyModel'=>'id,name')); 
     64            $Dummy = new DummyModel(); 
     65         
     66            $this->assertNotEqual($Dummy->getConnection(), $OtherConnection->getConnection()); 
     67         
     68            unset($db_settings['sqlite_databases']); 
     69            file_put_contents(AK_CONFIG_DIR.DS.'database.yml', Ak::convert('array', 'yaml', $db_settings)); 
     70        } 
     71    } 
    4572 
    4673}