Skip to content

Commit

Permalink
Remove PoolingShardConnection#getShardManager() method and refactored…
Browse files Browse the repository at this point in the history
… code. Fixed tests
  • Loading branch information
beberlei committed Jun 26, 2012
1 parent 12f381f commit b6f48e1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
14 changes: 4 additions & 10 deletions lib/Doctrine/DBAL/Sharding/PoolingShardConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,11 @@ public function __construct(array $params, Driver $driver, Configuration $config
}

/**
* @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) {
Expand Down
7 changes: 4 additions & 3 deletions lib/Doctrine/DBAL/Sharding/PoolingShardManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testGetCreateSchema()
$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()
Expand All @@ -56,7 +56,7 @@ public function testGetUpdateSchema()
$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()
Expand Down
26 changes: 16 additions & 10 deletions tests/Doctrine/Tests/DBAL/Sharding/PoolingShardManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public function testSelectShard()
{
$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());
Expand All @@ -62,9 +63,11 @@ public function testSelectShard()
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();
Expand All @@ -80,15 +83,18 @@ public function testQueryAll()

$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) ) ));
Expand Down

0 comments on commit b6f48e1

Please sign in to comment.