Commit b6f48e1a authored by Benjamin Eberlei's avatar Benjamin Eberlei

Remove PoolingShardConnection#getShardManager() method and refactored code. Fixed tests

parent 12f381f1
...@@ -126,17 +126,11 @@ class PoolingShardConnection extends Connection ...@@ -126,17 +126,11 @@ class PoolingShardConnection extends Connection
} }
/** /**
* @return \Doctrine\DBAL\Sharding\PoolingShardManager * Connect to a given shard
*
* @param mixed $shardId
* @return bool
*/ */
public function getShardManager()
{
if ($this->shardManager === null) {
$params = $this->getParams();
$this->shardManager = new PoolingShardManager($this, $params['shardChoser']);
}
return $this->shardManager;
}
public function connect($shardId = null) public function connect($shardId = null)
{ {
if ($shardId === null && $this->_conn) { if ($shardId === null && $this->_conn) {
......
...@@ -32,10 +32,11 @@ class PoolingShardManager implements ShardManager ...@@ -32,10 +32,11 @@ class PoolingShardManager implements ShardManager
private $choser; private $choser;
private $currentDistributionValue; private $currentDistributionValue;
public function __construct(PoolingShardConnection $conn, ShardChoser $choser) public function __construct(PoolingShardConnection $conn)
{ {
$this->conn = $conn; $params = $conn->getParams();
$this->choser = $choser; $this->conn = $conn;
$this->choser = $params['shardChoser'];
} }
public function selectGlobal() public function selectGlobal()
......
...@@ -45,7 +45,7 @@ class SingleDatabaseSynchronizerTest extends \PHPUnit_Framework_TestCase ...@@ -45,7 +45,7 @@ class SingleDatabaseSynchronizerTest extends \PHPUnit_Framework_TestCase
$table->setPrimaryKey(array('id')); $table->setPrimaryKey(array('id'));
$sql = $this->synchronizer->getCreateSchema($schema); $sql = $this->synchronizer->getCreateSchema($schema);
$this->assertEquals(array('CREATE TABLE test (id INTEGER NOT NULL, PRIMARY KEY("id"))'), $sql); $this->assertEquals(array('CREATE TABLE test (id INTEGER NOT NULL, PRIMARY KEY(id))'), $sql);
} }
public function testGetUpdateSchema() public function testGetUpdateSchema()
...@@ -56,7 +56,7 @@ class SingleDatabaseSynchronizerTest extends \PHPUnit_Framework_TestCase ...@@ -56,7 +56,7 @@ class SingleDatabaseSynchronizerTest extends \PHPUnit_Framework_TestCase
$table->setPrimaryKey(array('id')); $table->setPrimaryKey(array('id'));
$sql = $this->synchronizer->getUpdateSchema($schema); $sql = $this->synchronizer->getUpdateSchema($schema);
$this->assertEquals(array('CREATE TABLE test (id INTEGER NOT NULL, PRIMARY KEY("id"))'), $sql); $this->assertEquals(array('CREATE TABLE test (id INTEGER NOT NULL, PRIMARY KEY(id))'), $sql);
} }
public function testGetDropSchema() public function testGetDropSchema()
......
...@@ -51,9 +51,10 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase ...@@ -51,9 +51,10 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
{ {
$shardId = 10; $shardId = 10;
$conn = $this->createConnectionMock(); $conn = $this->createConnectionMock();
$conn->expects($this->once())->method('connect')->with($this->equalTo($shardId)); $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(array('shardChoser' => $this->createPassthroughShardChoser())));
$conn->expects($this->at(1))->method('connect')->with($this->equalTo($shardId));
$shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser()); $shardManager = new PoolingShardManager($conn);
$shardManager->selectShard($shardId); $shardManager->selectShard($shardId);
$this->assertEquals($shardId, $shardManager->getCurrentDistributionValue()); $this->assertEquals($shardId, $shardManager->getCurrentDistributionValue());
...@@ -62,9 +63,11 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase ...@@ -62,9 +63,11 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
public function testGetShards() public function testGetShards()
{ {
$conn = $this->createConnectionMock(); $conn = $this->createConnectionMock();
$conn->expects($this->once())->method('getParams')->will($this->returnValue( $conn->expects($this->any())->method('getParams')->will(
array('shards' => array( array('id' => 1), array('id' => 2) )) $this->returnValue(
)); array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
)
);
$shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser()); $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
$shards = $shardManager->getShards(); $shards = $shardManager->getShards();
...@@ -80,15 +83,18 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase ...@@ -80,15 +83,18 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
$conn = $this->createConnectionMock(); $conn = $this->createConnectionMock();
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue( $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(
array('shards' => array( array('id' => 1), array('id' => 2) )) array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
));
$conn->expects($this->at(1))->method('getParams')->will($this->returnValue(
array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
)); ));
$conn->expects($this->at(1))->method('connect')->with($this->equalTo(1)); $conn->expects($this->at(2))->method('connect')->with($this->equalTo(1));
$conn->expects($this->at(2)) $conn->expects($this->at(3))
->method('fetchAll') ->method('fetchAll')
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
->will($this->returnValue(array( array('id' => 1) ) )); ->will($this->returnValue(array( array('id' => 1) ) ));
$conn->expects($this->at(3))->method('connect')->with($this->equalTo(2)); $conn->expects($this->at(4))->method('connect')->with($this->equalTo(2));
$conn->expects($this->at(4)) $conn->expects($this->at(5))
->method('fetchAll') ->method('fetchAll')
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types)) ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
->will($this->returnValue(array( array('id' => 2) ) )); ->will($this->returnValue(array( array('id' => 2) ) ));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment