3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
19 namespace Doctrine\Tests\DBAL\Sharding;
21 use Doctrine\DBAL\Sharding\PoolingShardManager;
23 class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
25 private function createConnectionMock()
27 return $this->getMock('Doctrine\DBAL\Sharding\PoolingShardConnection', array('connect', 'getParams', 'fetchAll'), array(), '', false);
30 private function createPassthroughShardChoser()
32 $mock = $this->getMock('Doctrine\DBAL\Sharding\ShardChoser\ShardChoser');
33 $mock->expects($this->any())
35 ->will($this->returnCallback(function($value) { return $value; }));
39 public function testSelectGlobal()
41 $conn = $this->createConnectionMock();
42 $conn->expects($this->once())->method('connect')->with($this->equalTo(0));
44 $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
45 $shardManager->selectGlobal();
47 $this->assertNull($shardManager->getCurrentDistributionValue());
50 public function testSelectShard()
53 $conn = $this->createConnectionMock();
54 $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(array('shardChoser' => $this->createPassthroughShardChoser())));
55 $conn->expects($this->at(1))->method('connect')->with($this->equalTo($shardId));
57 $shardManager = new PoolingShardManager($conn);
58 $shardManager->selectShard($shardId);
60 $this->assertEquals($shardId, $shardManager->getCurrentDistributionValue());
63 public function testGetShards()
65 $conn = $this->createConnectionMock();
66 $conn->expects($this->any())->method('getParams')->will(
68 array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
72 $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
73 $shards = $shardManager->getShards();
75 $this->assertEquals(array(array('id' => 1), array('id' => 2)), $shards);
78 public function testQueryAll()
80 $sql = "SELECT * FROM table";
84 $conn = $this->createConnectionMock();
85 $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(
86 array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
88 $conn->expects($this->at(1))->method('getParams')->will($this->returnValue(
89 array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
91 $conn->expects($this->at(2))->method('connect')->with($this->equalTo(1));
92 $conn->expects($this->at(3))
94 ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
95 ->will($this->returnValue(array( array('id' => 1) ) ));
96 $conn->expects($this->at(4))->method('connect')->with($this->equalTo(2));
97 $conn->expects($this->at(5))
99 ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
100 ->will($this->returnValue(array( array('id' => 2) ) ));
102 $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
103 $result = $shardManager->queryAll($sql, $params, $types);
105 $this->assertEquals(array(array('id' => 1), array('id' => 2)), $result);