PoolingShardConnectionTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. namespace Doctrine\Tests\DBAL\Sharding;
  20. use Doctrine\DBAL\DriverManager;
  21. class PoolingShardConnectionTest extends \PHPUnit_Framework_TestCase
  22. {
  23. public function testConnect()
  24. {
  25. $conn = DriverManager::getConnection(array(
  26. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  27. 'driver' => 'pdo_sqlite',
  28. 'global' => array('memory' => true),
  29. 'shards' => array(
  30. array('id' => 1, 'memory' => true),
  31. array('id' => 2, 'memory' => true),
  32. ),
  33. 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
  34. ));
  35. $this->assertFalse($conn->isConnected(0));
  36. $conn->connect(0);
  37. $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
  38. $this->assertTrue($conn->isConnected(0));
  39. $this->assertFalse($conn->isConnected(1));
  40. $conn->connect(1);
  41. $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
  42. $this->assertTrue($conn->isConnected(1));
  43. $this->assertFalse($conn->isConnected(2));
  44. $conn->connect(2);
  45. $this->assertEquals(1, $conn->fetchColumn('SELECT 1'));
  46. $this->assertTrue($conn->isConnected(2));
  47. $conn->close();
  48. $this->assertFalse($conn->isConnected(0));
  49. $this->assertFalse($conn->isConnected(1));
  50. $this->assertFalse($conn->isConnected(2));
  51. }
  52. public function testNoGlobalServerException()
  53. {
  54. $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");
  55. $conn = DriverManager::getConnection(array(
  56. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  57. 'driver' => 'pdo_sqlite',
  58. 'shards' => array(
  59. array('id' => 1, 'memory' => true),
  60. array('id' => 2, 'memory' => true),
  61. ),
  62. 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
  63. ));
  64. }
  65. public function testNoShardsServersExecption()
  66. {
  67. $this->setExpectedException('InvalidArgumentException', "Connection Parameters require 'global' and 'shards' configurations.");
  68. $conn = DriverManager::getConnection(array(
  69. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  70. 'driver' => 'pdo_sqlite',
  71. 'global' => array('memory' => true),
  72. 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
  73. ));
  74. }
  75. public function testNoShardsChoserExecption()
  76. {
  77. $this->setExpectedException('InvalidArgumentException', "Missing Shard Choser configuration 'shardChoser'");
  78. $conn = DriverManager::getConnection(array(
  79. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  80. 'driver' => 'pdo_sqlite',
  81. 'global' => array('memory' => true),
  82. 'shards' => array(
  83. array('id' => 1, 'memory' => true),
  84. array('id' => 2, 'memory' => true),
  85. ),
  86. ));
  87. }
  88. public function testShardChoserWrongInstance()
  89. {
  90. $this->setExpectedException('InvalidArgumentException', "The 'shardChoser' configuration is not a valid instance of Doctrine\DBAL\Sharding\ShardChoser\ShardChoser");
  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. 'shardChoser' => new \stdClass,
  100. ));
  101. }
  102. public function testShardNonNumericId()
  103. {
  104. $this->setExpectedException('InvalidArgumentException', "Shard Id has to be a non-negative number.");
  105. $conn = DriverManager::getConnection(array(
  106. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  107. 'driver' => 'pdo_sqlite',
  108. 'global' => array('memory' => true),
  109. 'shards' => array(
  110. array('id' => 'foo', 'memory' => true),
  111. ),
  112. 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
  113. ));
  114. }
  115. public function testShardMissingId()
  116. {
  117. $this->setExpectedException('InvalidArgumentException', "Missing 'id' for one configured shard. Please specificy a unique shard-id.");
  118. $conn = DriverManager::getConnection(array(
  119. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  120. 'driver' => 'pdo_sqlite',
  121. 'global' => array('memory' => true),
  122. 'shards' => array(
  123. array('memory' => true),
  124. ),
  125. 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
  126. ));
  127. }
  128. public function testDuplicateShardId()
  129. {
  130. $this->setExpectedException('InvalidArgumentException', "Shard 1 is duplicated in the configuration.");
  131. $conn = DriverManager::getConnection(array(
  132. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  133. 'driver' => 'pdo_sqlite',
  134. 'global' => array('memory' => true),
  135. 'shards' => array(
  136. array('id' => 1, 'memory' => true),
  137. array('id' => 1, 'memory' => true),
  138. ),
  139. 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
  140. ));
  141. }
  142. public function testSwitchShardWithOpenTransactionException()
  143. {
  144. $conn = DriverManager::getConnection(array(
  145. 'wrapperClass' => 'Doctrine\DBAL\Sharding\PoolingShardConnection',
  146. 'driver' => 'pdo_sqlite',
  147. 'global' => array('memory' => true),
  148. 'shards' => array(
  149. array('id' => 1, 'memory' => true),
  150. ),
  151. 'shardChoser' => 'Doctrine\DBAL\Sharding\ShardChoser\MultiTenantShardChoser',
  152. ));
  153. $conn->beginTransaction();
  154. $this->setExpectedException('Doctrine\DBAL\Sharding\ShardingException', 'Cannot switch shard when transaction is active.');
  155. $conn->connect(1);
  156. }
  157. }