Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Sharding / SQLAzure / SQLAzureShardManagerTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Sharding\SQLAzure;
4
5 use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager;
6
7 class SQLAzureShardManagerTest extends \PHPUnit_Framework_TestCase
8 {
9     public function testNoFederationName()
10     {
11         $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'SQLAzure requires a federation name to be set during sharding configuration.');
12
13         $conn = $this->createConnection(array('sharding' => array('distributionKey' => 'abc', 'distributionType' => 'integer')));
14         $sm = new SQLAzureShardManager($conn);
15     }
16
17     public function testNoDistributionKey()
18     {
19         $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'SQLAzure requires a distribution key to be set during sharding configuration.');
20
21         $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionType' => 'integer')));
22         $sm = new SQLAzureShardManager($conn);
23     }
24
25     public function testNoDistributionType()
26     {
27         $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException');
28
29         $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo')));
30         $sm = new SQLAzureShardManager($conn);
31     }
32
33     public function testGetDefaultDistributionValue()
34     {
35         $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
36
37         $sm = new SQLAzureShardManager($conn);
38         $this->assertNull($sm->getCurrentDistributionValue());
39     }
40
41     public function testSelectGlobalTransactionActive()
42     {
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));
45
46         $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard during an active transaction.');
47
48         $sm = new SQLAzureShardManager($conn);
49         $sm->selectGlobal();
50     }
51
52     public function testSelectGlobal()
53     {
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'));
57
58         $sm = new SQLAzureShardManager($conn);
59         $sm->selectGlobal();
60     }
61
62     public function testSelectShard()
63     {
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));
66
67         $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard during an active transaction.');
68
69         $sm = new SQLAzureShardManager($conn);
70         $sm->selectShard(1234);
71
72         $this->assertEquals(1234, $sm->getCurrentDistributionValue());
73     }
74
75     public function testSelectShardNoDistriubtionValue()
76     {
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));
79
80         $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'You have to specify a string or integer as shard distribution value.');
81
82         $sm = new SQLAzureShardManager($conn);
83         $sm->selectShard(null);
84     }
85
86     private function createConnection(array $params)
87     {
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));
90         return $conn;
91     }
92 }
93