lithium\tests\integration\data\DatabaseTest::testSwitchingDatabaseOnModel PHP Method

testSwitchingDatabaseOnModel() public method

Prove that one model's connection can be switched while keeping on working upon the correct databases.
    public function testSwitchingDatabaseOnModel()
    {
        $connection1 = $this->_connection;
        $connection2 = $this->_connection . '_alternative';
        $connectionConfig1 = Connections::get($connection1, array('config' => true));
        $connectionConfig2 = Connections::get($connection2, array('config' => true));
        parent::connect($connection2);
        $this->skipIf(!$connectionConfig2, "The `'{$connection2}' connection is not available`.");
        $this->skipIf(!$this->with(array('MySql', 'PostgreSql', 'Sqlite3')));
        $bothInMemory = $connectionConfig1['database'] == ':memory:';
        $bothInMemory = $bothInMemory && $connectionConfig2['database'] == ':memory:';
        $this->skipIf($bothInMemory, 'Cannot use two connections with in memory databases');
        Galleries::config(array('meta' => array('connection' => $connection1)));
        $galleriesCountOriginal = Galleries::find('count');
        $gallery = Galleries::create(array('name' => 'record_in_db'));
        $gallery->save();
        Fixtures::save('db_alternative');
        Galleries::config(array('meta' => array('connection' => $connection2)));
        $expected = $galleriesCountOriginal;
        $result = Galleries::find('count');
        $this->assertEqual($expected, $result);
        Galleries::config(array('meta' => array('connection' => $connection1)));
        $expected = $galleriesCountOriginal + 1;
        $result = Galleries::find('count');
        $this->assertEqual($expected, $result);
        Fixtures::clear('db_alternative');
    }