Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix connection binding to component for all autoloaded models #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ matrix:
- php: nightly
- php: hhvm

before_script:
- composer install

script:
- php -dshort_open_tag=Off -dmagic_quotes_gpc=Off tests/index.php
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
},
"autoload": {
"psr-0": { "Doctrine_": "lib/" }
},
"autoload-dev": {
"psr-4": { "": "tests/autoloaded/" }
}
}
8 changes: 5 additions & 3 deletions lib/Doctrine/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,7 @@ public function bindComponent($componentName, $connectionName)
*/
public function getConnectionForComponent($componentName)
{
Doctrine_Core::modelsAutoload($componentName);

if (isset($this->_bound[$componentName])) {
if ($this->hasConnectionForComponent($componentName)) {
return $this->getConnection($this->_bound[$componentName]);
}

Expand All @@ -569,6 +567,10 @@ public function getConnectionForComponent($componentName)
*/
public function hasConnectionForComponent($componentName = null)
{
if (!Doctrine_Core::modelsAutoload($componentName)) {
class_exists($componentName); // Trigger autoloader to bind connection.
}

return isset($this->_bound[$componentName]);
}

Expand Down
39 changes: 39 additions & 0 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,45 @@ public function testAllModelsAvailable()
$this->assertTrue( class_exists('BaseConservativeModelLoadingUser', true));
}

public function testCreateTableBindConnection()
{
Doctrine_Manager::getInstance()->openConnection('sqlite::memory:', 'test_bind_connection', false);

$table = Doctrine_Core::getTable('ModelBindConnection');
$this->assertEqual('test_bind_connection', $table->getConnection()->getName());
}

public function testCreateQueryBindConnection()
{
$manager = Doctrine_Manager::getInstance();
$currentConnection = $manager->getCurrentConnection();
$connection = $manager->openConnection('sqlite::memory:', 'test_bind_connection', false);

$query = Doctrine_Core::getTable('ModelBindConnection')->createQuery();

$refl = new ReflectionProperty($query, '_passedConn');
$refl->setAccessible(true);
$isPassedConn = $refl->getValue($query);
$refl->setAccessible(false);

// Assert that the connedtion of the query is not passed.
$this->assertFalse($isPassedConn);

$query->getSqlQuery();

// Assert that the current connection was not changed.
$this->assertTrue($currentConnection === $manager->getCurrentConnection());

// Assert that the model does not exists on the current connection.
$this->assertFalse($currentConnection->hasTable('ModelBindConnection'));

// Assert that the model does not exists on the bound connection.
$this->assertTrue($connection->hasTable('ModelBindConnection'));

// Assert that the connection of the query is the bound connection.
$this->assertEqual('test_bind_connection', $query->getConnection()->getName());
}

public function testModelLoadingCacheInformation()
{
$models = Doctrine_Core::getLoadedModels();
Expand Down
11 changes: 11 additions & 0 deletions tests/autoloaded/ModelBindConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

Doctrine_Manager::getInstance()->bindComponent('ModelBindConnection', 'test_bind_connection');

class ModelBindConnection extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('name', 'string', 1);
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

define('DOCTRINE_DIR', $_SERVER['DOCTRINE_DIR']);

require_once(DOCTRINE_DIR . '/lib/Doctrine/Core.php');
require_once DOCTRINE_DIR.'/vendor/autoload.php';

spl_autoload_register(array('Doctrine_Core', 'autoload'));
spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));
Expand Down