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.
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>.
20 namespace Doctrine\Tests\DBAL\Sharding;
22 use Doctrine\DBAL\DriverManager;
24 class PoolingShardConnectionTest extends \PHPUnit_Framework_TestCase
26 public function testConnect()
28 $conn = DriverManager::getConnection(array(
29 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
30 'driver' => 'pdo_sqlite',
31 'global' => array('memory' => true),
33 array('id' => 1, 'memory' => true),
34 array('id' => 2, 'memory' => true),
36 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
39 $this->assertFalse($conn->isConnected(0));
41 $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
42 $this->assertTrue($conn->isConnected(0));
44 $this->assertFalse($conn->isConnected(1));
46 $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
47 $this->assertTrue($conn->isConnected(1));
49 $this->assertFalse($conn->isConnected(2));
51 $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
52 $this->assertTrue($conn->isConnected(2));
55 $this->assertFalse($conn->isConnected(0));
56 $this->assertFalse($conn->isConnected(1));
57 $this->assertFalse($conn->isConnected(2));
60 public function testNoGlobalServerException()
62 $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");
64 $conn = DriverManager::getConnection(array(
65 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
66 'driver' => 'pdo_sqlite',
68 array('id' => 1, 'memory' => true),
69 array('id' => 2, 'memory' => true),
71 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
75 public function testNoShardsServersExecption()
77 $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");
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',
87 public function testNoShardsChoserExecption()
89 $this->setExpectedException('InvalidArgumentException', "Missing Shard Choser configuration 'shardChoser'");
91 $conn = DriverManager::getConnection(array(
92 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
93 'driver' => 'pdo_sqlite',
94 'global' => array('memory' => true),
96 array('id' => 1, 'memory' => true),
97 array('id' => 2, 'memory' => true),
102 public function testShardChoserWrongInstance()
104 $this->setExpectedException('InvalidArgumentException', "The 'shardChoser' configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser");
106 $conn = DriverManager::getConnection(array(
107 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
108 'driver' => 'pdo_sqlite',
109 'global' => array('memory' => true),
111 array('id' => 1, 'memory' => true),
112 array('id' => 2, 'memory' => true),
114 'shardChoser' => new \stdClass,
118 public function testShardNonNumericId()
120 $this->setExpectedException('InvalidArgumentException', "Shard Id has to be a non-negative number.");
122 $conn = DriverManager::getConnection(array(
123 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
124 'driver' => 'pdo_sqlite',
125 'global' => array('memory' => true),
127 array('id' => 'foo', 'memory' => true),
129 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
133 public function testShardMissingId()
135 $this->setExpectedException('InvalidArgumentException', "Missing 'id' for one configured shard. Please specificy a unique shard-id.");
137 $conn = DriverManager::getConnection(array(
138 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
139 'driver' => 'pdo_sqlite',
140 'global' => array('memory' => true),
142 array('memory' => true),
144 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
148 public function testDuplicateShardId()
150 $this->setExpectedException('InvalidArgumentException', "Shard 1 is duplicated in the configuration.");
152 $conn = DriverManager::getConnection(array(
153 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
154 'driver' => 'pdo_sqlite',
155 'global' => array('memory' => true),
157 array('id' => 1, 'memory' => true),
158 array('id' => 1, 'memory' => true),
160 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
164 public function testSwitchShardWithOpenTransactionException()
166 $conn = DriverManager::getConnection(array(
167 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
168 'driver' => 'pdo_sqlite',
169 'global' => array('memory' => true),
171 array('id' => 1, 'memory' => true),
173 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
176 $conn->beginTransaction();
178 $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard when transaction is active.');