SQLAzureShardManagerTest.php 4.19 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Sharding\SQLAzure;

Sergei Morozov's avatar
Sergei Morozov committed
5 6
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Sharding\ShardingException;
7
use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager;
Sergei Morozov's avatar
Sergei Morozov committed
8
use PHPUnit\Framework\TestCase;
9

Sergei Morozov's avatar
Sergei Morozov committed
10
class SQLAzureShardManagerTest extends TestCase
11 12 13
{
    public function testNoFederationName()
    {
Sergei Morozov's avatar
Sergei Morozov committed
14
        $this->expectException(ShardingException::class);
15
        $this->expectExceptionMessage('SQLAzure requires a federation name to be set during sharding configuration.');
16

Sergei Morozov's avatar
Sergei Morozov committed
17
        $conn = $this->createConnection(['sharding' => ['distributionKey' => 'abc', 'distributionType' => 'integer']]);
Sergei Morozov's avatar
Sergei Morozov committed
18
        new SQLAzureShardManager($conn);
19 20 21 22
    }

    public function testNoDistributionKey()
    {
Sergei Morozov's avatar
Sergei Morozov committed
23
        $this->expectException(ShardingException::class);
24
        $this->expectExceptionMessage('SQLAzure requires a distribution key to be set during sharding configuration.');
25

Sergei Morozov's avatar
Sergei Morozov committed
26
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionType' => 'integer']]);
Sergei Morozov's avatar
Sergei Morozov committed
27
        new SQLAzureShardManager($conn);
28 29 30 31
    }

    public function testNoDistributionType()
    {
Sergei Morozov's avatar
Sergei Morozov committed
32
        $this->expectException(ShardingException::class);
33

Sergei Morozov's avatar
Sergei Morozov committed
34
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo']]);
Sergei Morozov's avatar
Sergei Morozov committed
35
        new SQLAzureShardManager($conn);
36 37 38 39
    }

    public function testGetDefaultDistributionValue()
    {
Sergei Morozov's avatar
Sergei Morozov committed
40
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
41 42

        $sm = new SQLAzureShardManager($conn);
43
        self::assertNull($sm->getCurrentDistributionValue());
44 45 46 47
    }

    public function testSelectGlobalTransactionActive()
    {
Sergei Morozov's avatar
Sergei Morozov committed
48
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
49 50
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));

Sergei Morozov's avatar
Sergei Morozov committed
51
        $this->expectException(ShardingException::class);
52
        $this->expectExceptionMessage('Cannot switch shard during an active transaction.');
53 54 55 56 57 58 59

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

    public function testSelectGlobal()
    {
Sergei Morozov's avatar
Sergei Morozov committed
60
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
61 62 63 64 65 66 67 68 69
        $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()
    {
Sergei Morozov's avatar
Sergei Morozov committed
70
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
71 72
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));

Sergei Morozov's avatar
Sergei Morozov committed
73
        $this->expectException(ShardingException::class);
74
        $this->expectExceptionMessage('Cannot switch shard during an active transaction.');
75 76 77 78

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

79
        self::assertEquals(1234, $sm->getCurrentDistributionValue());
80 81
    }

Possum's avatar
Possum committed
82
    public function testSelectShardNoDistributionValue()
83
    {
Sergei Morozov's avatar
Sergei Morozov committed
84
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
85 86
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(false));

Sergei Morozov's avatar
Sergei Morozov committed
87
        $this->expectException(ShardingException::class);
88
        $this->expectExceptionMessage('You have to specify a string or integer as shard distribution value.');
89 90 91 92 93

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

Sergei Morozov's avatar
Sergei Morozov committed
94 95 96
    /**
     * @param mixed[] $params
     */
97 98
    private function createConnection(array $params)
    {
Sergei Morozov's avatar
Sergei Morozov committed
99
        $conn = $this->getMockBuilder(Connection::class)
Sergei Morozov's avatar
Sergei Morozov committed
100
            ->setMethods(['getParams', 'exec', 'isTransactionActive'])
101 102
            ->disableOriginalConstructor()
            ->getMock();
103 104 105 106
        $conn->expects($this->at(0))->method('getParams')->will($this->returnValue($params));
        return $conn;
    }
}