Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Sharding / PoolingShardConnectionTest.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the LGPL. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\Tests\DBAL\Sharding;
21
22 use Doctrine\DBAL\DriverManager;
23
24 class PoolingShardConnectionTest extends \PHPUnit_Framework_TestCase
25 {
26     public function testConnect()
27     {
28         $conn = DriverManager::getConnection(array(
29             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
30             'driver' => 'pdo_sqlite',
31             'global' => array('memory' => true),
32             'shards' => array(
33                 array('id' => 1, 'memory' => true),
34                 array('id' => 2, 'memory' => true),
35             ),
36             'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
37         ));
38
39         $this->assertFalse($conn->isConnected(0));
40         $conn->connect(0);
41         $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
42         $this->assertTrue($conn->isConnected(0));
43
44         $this->assertFalse($conn->isConnected(1));
45         $conn->connect(1);
46         $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
47         $this->assertTrue($conn->isConnected(1));
48
49         $this->assertFalse($conn->isConnected(2));
50         $conn->connect(2);
51         $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
52         $this->assertTrue($conn->isConnected(2));
53
54         $conn->close();
55         $this->assertFalse($conn->isConnected(0));
56         $this->assertFalse($conn->isConnected(1));
57         $this->assertFalse($conn->isConnected(2));
58     }
59
60     public function testNoGlobalServerException()
61     {
62         $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");
63
64         $conn = DriverManager::getConnection(array(
65             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
66             'driver' => 'pdo_sqlite',
67             'shards' => array(
68                 array('id' => 1, 'memory' => true),
69                 array('id' => 2, 'memory' => true),
70             ),
71             'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
72         ));
73     }
74
75     public function testNoShardsServersExecption()
76     {
77         $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");
78
79         $conn = DriverManager::getConnection(array(
80             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
81             'driver' => 'pdo_sqlite',
82             'global' => array('memory' => true),
83             'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
84         ));
85     }
86
87     public function testNoShardsChoserExecption()
88     {
89         $this->setExpectedException('InvalidArgumentException', "Missing Shard Choser configuration 'shardChoser'");
90
91         $conn = DriverManager::getConnection(array(
92             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
93             'driver' => 'pdo_sqlite',
94             'global' => array('memory' => true),
95             'shards' => array(
96                 array('id' => 1, 'memory' => true),
97                 array('id' => 2, 'memory' => true),
98             ),
99         ));
100     }
101
102     public function testShardChoserWrongInstance()
103     {
104         $this->setExpectedException('InvalidArgumentException', "The 'shardChoser' configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser");
105
106         $conn = DriverManager::getConnection(array(
107             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
108             'driver' => 'pdo_sqlite',
109             'global' => array('memory' => true),
110             'shards' => array(
111                 array('id' => 1, 'memory' => true),
112                 array('id' => 2, 'memory' => true),
113             ),
114             'shardChoser' => new \stdClass,
115         ));
116     }
117
118     public function testShardNonNumericId()
119     {
120         $this->setExpectedException('InvalidArgumentException', "Shard Id has to be a non-negative number.");
121
122         $conn = DriverManager::getConnection(array(
123             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
124             'driver' => 'pdo_sqlite',
125             'global' => array('memory' => true),
126             'shards' => array(
127                 array('id' => 'foo', 'memory' => true),
128             ),
129             'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
130         ));
131     }
132
133     public function testShardMissingId()
134     {
135         $this->setExpectedException('InvalidArgumentException', "Missing 'id' for one configured shard. Please specificy a unique shard-id.");
136
137         $conn = DriverManager::getConnection(array(
138             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
139             'driver' => 'pdo_sqlite',
140             'global' => array('memory' => true),
141             'shards' => array(
142                 array('memory' => true),
143             ),
144             'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
145         ));
146     }
147
148     public function testDuplicateShardId()
149     {
150         $this->setExpectedException('InvalidArgumentException', "Shard 1 is duplicated in the configuration.");
151
152         $conn = DriverManager::getConnection(array(
153             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
154             'driver' => 'pdo_sqlite',
155             'global' => array('memory' => true),
156             'shards' => array(
157                 array('id' => 1, 'memory' => true),
158                 array('id' => 1, 'memory' => true),
159             ),
160             'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
161         ));
162     }
163
164     public function testSwitchShardWithOpenTransactionException()
165     {
166         $conn = DriverManager::getConnection(array(
167             'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
168             'driver' => 'pdo_sqlite',
169             'global' => array('memory' => true),
170             'shards' => array(
171                 array('id' => 1, 'memory' => true),
172             ),
173             'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
174         ));
175
176         $conn->beginTransaction();
177
178         $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard when transaction is active.');
179         $conn->connect(1);
180     }
181 }
182