Failed Conditions
Pull Request — master (#3435)
by Sergei
64:41
created

SQLAzureShardManagerTest::testSelectShard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Sharding\SQLAzure;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Sharding\ShardingException;
7
use Doctrine\DBAL\Sharding\SQLAzure\SQLAzureShardManager;
8
use PHPUnit\Framework\TestCase;
9
10
class SQLAzureShardManagerTest extends TestCase
11
{
12
    public function testNoFederationName()
13
    {
14
        $this->expectException(ShardingException::class);
15
        $this->expectExceptionMessage('SQLAzure requires a federation name to be set during sharding configuration.');
16
17
        $conn = $this->createConnection(['sharding' => ['distributionKey' => 'abc', 'distributionType' => 'integer']]);
18
        new SQLAzureShardManager($conn);
19
    }
20
21
    public function testNoDistributionKey()
22
    {
23
        $this->expectException(ShardingException::class);
24
        $this->expectExceptionMessage('SQLAzure requires a distribution key to be set during sharding configuration.');
25
26
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionType' => 'integer']]);
27
        new SQLAzureShardManager($conn);
28
    }
29
30
    public function testNoDistributionType()
31
    {
32
        $this->expectException(ShardingException::class);
33
34
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo']]);
35
        new SQLAzureShardManager($conn);
36
    }
37
38
    public function testGetDefaultDistributionValue()
39
    {
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
    public function testSelectGlobalTransactionActive()
47
    {
48
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
49
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));
50
51
        $this->expectException(ShardingException::class);
52
        $this->expectExceptionMessage('Cannot switch shard during an active transaction.');
53
54
        $sm = new SQLAzureShardManager($conn);
55
        $sm->selectGlobal();
56
    }
57
58
    public function testSelectGlobal()
59
    {
60
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
61
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(false));
62
        $conn->expects($this->at(2))->method('exec')->with($this->equalTo('USE FEDERATION ROOT WITH RESET'));
63
64
        $sm = new SQLAzureShardManager($conn);
65
        $sm->selectGlobal();
66
    }
67
68
    public function testSelectShard()
69
    {
70
        $conn = $this->createConnection(['sharding' => ['federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer']]);
71
        $conn->expects($this->at(1))->method('isTransactionActive')->will($this->returnValue(true));
72
73
        $this->expectException(ShardingException::class);
74
        $this->expectExceptionMessage('Cannot switch shard during an active transaction.');
75
76
        $sm = new SQLAzureShardManager($conn);
77
        $sm->selectShard(1234);
78
79
        self::assertEquals(1234, $sm->getCurrentDistributionValue());
80
    }
81
82
    /**
83
     * @param mixed[] $params
84
     */
85
    private function createConnection(array $params)
86
    {
87
        $conn = $this->getMockBuilder(Connection::class)
88
            ->setMethods(['getParams', 'exec', 'isTransactionActive'])
89
            ->disableOriginalConstructor()
90
            ->getMock();
91
        $conn->expects($this->at(0))->method('getParams')->will($this->returnValue($params));
92
        return $conn;
93
    }
94
}
95