3 namespace Doctrine\Tests\DBAL\Sharding\SQLAzure;
5 use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager;
7 class SQLAzureShardManagerTest extends \PHPUnit_Framework_TestCase
9 public function testNoFederationName()
11 $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'SQLAzure requires a federation name to be set during sharding configuration.');
13 $conn = $this->createConnection(array('sharding' => array('distributionKey' => 'abc', 'distributionType' => 'integer')));
14 $sm = new SQLAzureShardManager($conn);
17 public function testNoDistributionKey()
19 $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'SQLAzure requires a distribution key to be set during sharding configuration.');
21 $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionType' => 'integer')));
22 $sm = new SQLAzureShardManager($conn);
25 public function testNoDistributionType()
27 $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException');
29 $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo')));
30 $sm = new SQLAzureShardManager($conn);
33 public function testGetDefaultDistributionValue()
35 $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
37 $sm = new SQLAzureShardManager($conn);
38 $this->assertNull($sm->getCurrentDistributionValue());
41 public function testSelectGlobalTransactionActive()
43 $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
44 $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));
46 $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard during an active transaction.');
48 $sm = new SQLAzureShardManager($conn);
52 public function testSelectGlobal()
54 $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
55 $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(false));
56 $conn->expects($this->at(2))->method('exec')->with($this->equalTo('USE FEDERATION ROOT WITH RESET'));
58 $sm = new SQLAzureShardManager($conn);
62 public function testSelectShard()
64 $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
65 $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));
67 $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard during an active transaction.');
69 $sm = new SQLAzureShardManager($conn);
70 $sm->selectShard(1234);
72 $this->assertEquals(1234, $sm->getCurrentDistributionValue());
75 public function testSelectShardNoDistriubtionValue()
77 $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
78 $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(false));
80 $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'You have to specify a string or integer as shard distribution value.');
82 $sm = new SQLAzureShardManager($conn);
83 $sm->selectShard(null);
86 private function createConnection(array $params)
88 $conn = $this->getMock('Doctrine\DBAL\Connection', array('getParams', 'exec', 'isTransactionActive'), array(), '', false);
89 $conn->expects($this->at(0))->method('getParams')->will($this->returnValue($params));