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
}
/**
* @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)
{
if ($shardId === null && $this->_conn) {
......
......@@ -32,10 +32,11 @@ class PoolingShardManager implements ShardManager
private $choser;
private $currentDistributionValue;
public function __construct(PoolingShardConnection $conn, ShardChoser $choser)
public function __construct(PoolingShardConnection $conn)
{
$this->conn = $conn;
$this->choser = $choser;
$params = $conn->getParams();
$this->conn = $conn;
$this->choser = $params['shardChoser'];
}
public function selectGlobal()
......
......@@ -45,7 +45,7 @@ class SingleDatabaseSynchronizerTest extends \PHPUnit_Framework_TestCase
$table->setPrimaryKey(array('id'));
$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()
......@@ -56,7 +56,7 @@ class SingleDatabaseSynchronizerTest extends \PHPUnit_Framework_TestCase
$table->setPrimaryKey(array('id'));
$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()
......
......@@ -51,9 +51,10 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
{
$shardId = 10;
$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);
$this->assertEquals($shardId, $shardManager->getCurrentDistributionValue());
......@@ -62,9 +63,11 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
public function testGetShards()
{
$conn = $this->createConnectionMock();
$conn->expects($this->once())->method('getParams')->will($this->returnValue(
array('shards' => array( array('id' => 1), array('id' => 2) ))
));
$conn->expects($this->any())->method('getParams')->will(
$this->returnValue(
array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
)
);
$shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
$shards = $shardManager->getShards();
......@@ -80,15 +83,18 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
$conn = $this->createConnectionMock();
$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))
$conn->expects($this->at(2))->method('connect')->with($this->equalTo(1));
$conn->expects($this->at(3))
->method('fetchAll')
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
->will($this->returnValue(array( array('id' => 1) ) ));
$conn->expects($this->at(3))->method('connect')->with($this->equalTo(2));
$conn->expects($this->at(4))
$conn->expects($this->at(4))->method('connect')->with($this->equalTo(2));
$conn->expects($this->at(5))
->method('fetchAll')
->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
->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