Commit e0ffc8a9 authored by Benjamin Eberlei's avatar Benjamin Eberlei

Merge pull request #377 from vlastv/patch-1

Fixed logic error in Sharding component
parents 8f119eea 227b50a0
......@@ -115,7 +115,7 @@ class PoolingShardManager implements ShardManager
$oldDistribution = $this->getCurrentDistributionValue();
foreach ($shards as $shard) {
$this->selectShard($shard['id']);
$this->conn->connect($shard['id']);
foreach ($this->conn->fetchAll($sql, $params, $types) as $row) {
$result[] = $row;
}
......
......@@ -36,6 +36,15 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
return $mock;
}
private function createStaticShardChoser()
{
$mock = $this->getMock('Doctrine\DBAL\Sharding\ShardChoser\ShardChoser');
$mock->expects($this->any())
->method('pickShard')
->will($this->returnCallback(function($value) { return 1; }));
return $mock;
}
public function testSelectGlobal()
{
$conn = $this->createConnectionMock();
......@@ -104,5 +113,35 @@ class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(array('id' => 1), array('id' => 2)), $result);
}
public function testQueryAllWithStaticShardChoser()
{
$sql = "SELECT * FROM table";
$params = array(1);
$types = array(1);
$conn = $this->createConnectionMock();
$conn->expects($this->at(0))->method('getParams')->will($this->returnValue(
array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createStaticShardChoser())
));
$conn->expects($this->at(1))->method('getParams')->will($this->returnValue(
array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createStaticShardChoser())
));
$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(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) ) ));
$shardManager = new PoolingShardManager($conn, $this->createStaticShardChoser());
$result = $shardManager->queryAll($sql, $params, $types);
$this->assertEquals(array(array('id' => 1), array('id' => 2)), $result);
}
}
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