SQLAzureShardManagerTest.php 4.08 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\DBAL\Sharding\SQLAzure;

use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager;

Luís Cobucci's avatar
Luís Cobucci committed
7
class SQLAzureShardManagerTest extends \PHPUnit\Framework\TestCase
8 9 10
{
    public function testNoFederationName()
    {
Luís Cobucci's avatar
Luís Cobucci committed
11
        $this->expectException('Doctrine\DBAL\Sharding\ShardingException', 'SQLAzure requires a federation name to be set during sharding configuration.');
12 13 14 15 16 17 18

        $conn = $this->createConnection(array('sharding' => array('distributionKey' => 'abc', 'distributionType' => 'integer')));
        $sm = new SQLAzureShardManager($conn);
    }

    public function testNoDistributionKey()
    {
Luís Cobucci's avatar
Luís Cobucci committed
19
        $this->expectException('Doctrine\DBAL\Sharding\ShardingException', 'SQLAzure requires a distribution key to be set during sharding configuration.');
20 21 22 23 24 25 26

        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionType' => 'integer')));
        $sm = new SQLAzureShardManager($conn);
    }

    public function testNoDistributionType()
    {
Luís Cobucci's avatar
Luís Cobucci committed
27
        $this->expectException('Doctrine\DBAL\Sharding\ShardingException');
28 29 30 31 32 33 34 35 36 37

        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo')));
        $sm = new SQLAzureShardManager($conn);
    }

    public function testGetDefaultDistributionValue()
    {
        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));

        $sm = new SQLAzureShardManager($conn);
38
        self::assertNull($sm->getCurrentDistributionValue());
39 40 41 42 43 44 45
    }

    public function testSelectGlobalTransactionActive()
    {
        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));

Luís Cobucci's avatar
Luís Cobucci committed
46
        $this->expectException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard during an active transaction.');
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

        $sm = new SQLAzureShardManager($conn);
        $sm->selectGlobal();
    }

    public function testSelectGlobal()
    {
        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(false));
        $conn->expects($this->at(2))->method('exec')->with($this->equalTo('USE FEDERATION ROOT WITH RESET'));

        $sm = new SQLAzureShardManager($conn);
        $sm->selectGlobal();
    }

    public function testSelectShard()
    {
        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));

Luís Cobucci's avatar
Luís Cobucci committed
67
        $this->expectException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard during an active transaction.');
68 69 70 71

        $sm = new SQLAzureShardManager($conn);
        $sm->selectShard(1234);

72
        self::assertEquals(1234, $sm->getCurrentDistributionValue());
73 74
    }

Possum's avatar
Possum committed
75
    public function testSelectShardNoDistributionValue()
76 77 78 79
    {
        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(false));

Luís Cobucci's avatar
Luís Cobucci committed
80
        $this->expectException('Doctrine\DBAL\Sharding\ShardingException', 'You have to specify a string or integer as shard distribution value.');
81 82 83 84 85 86 87

        $sm = new SQLAzureShardManager($conn);
        $sm->selectShard(null);
    }

    private function createConnection(array $params)
    {
88 89 90 91
        $conn = $this->getMockBuilder('Doctrine\DBAL\Connection')
            ->setMethods(array('getParams', 'exec', 'isTransactionActive'))
            ->disableOriginalConstructor()
            ->getMock();
92 93 94 95 96
        $conn->expects($this->at(0))->method('getParams')->will($this->returnValue($params));
        return $conn;
    }
}