Completed
Push — master ( c7757e...39cb21 )
by Luís
16s
created

Sharding/SQLAzure/SQLAzureShardManagerTest.php (1 issue)

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 View Code Duplication
    public function testNoFederationName()
10
    {
11
        $this->expectException('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 View Code Duplication
    public function testNoDistributionKey()
18
    {
19
        $this->expectException('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 View Code Duplication
    public function testNoDistributionType()
26
    {
27
        $this->expectException('Doctrine\DBAL\Sharding\ShardingException');
28
29
        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo')));
30
        $sm = new SQLAzureShardManager($conn);
31
    }
32
33 View Code Duplication
    public function testGetDefaultDistributionValue()
34
    {
35
        $conn = $this->createConnection(array('sharding' => array('federationName' => 'abc', 'distributionKey' => 'foo', 'distributionType' => 'integer')));
36
37
        $sm = new SQLAzureShardManager($conn);
38
        self::assertNull($sm->getCurrentDistributionValue());
39
    }
40
41 View Code Duplication
    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->expectException('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 View Code Duplication
    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->expectException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard during an active transaction.');
68
69
        $sm = new SQLAzureShardManager($conn);
70
        $sm->selectShard(1234);
71
72
        self::assertEquals(1234, $sm->getCurrentDistributionValue());
73
    }
74
75 View Code Duplication
    public function testSelectShardNoDistributionValue()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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->expectException('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->getMockBuilder('Doctrine\DBAL\Connection')
89
            ->setMethods(array('getParams', 'exec', 'isTransactionActive'))
90
            ->disableOriginalConstructor()
91
            ->getMock();
92
        $conn->expects($this->at(0))->method('getParams')->will($this->returnValue($params));
93
        return $conn;
94
    }
95
}
96
97