PoolingShardManagerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Sharding\PoolingShardManager;
  21. class PoolingShardManagerTest extends \PHPUnit_Framework_TestCase
  22. {
  23. private function createConnectionMock()
  24. {
  25. return $this->getMock('Doctrine\DBAL\Sharding\PoolingShardConnection', array('connect', 'getParams', 'fetchAll'), array(), '', false);
  26. }
  27. private function createPassthroughShardChoser()
  28. {
  29. $mock = $this->getMock('Doctrine\DBAL\Sharding\ShardChoser\ShardChoser');
  30. $mock->expects($this->any())
  31. ->method('pickShard')
  32. ->will($this->returnCallback(function($value) { return $value; }));
  33. return $mock;
  34. }
  35. public function testSelectGlobal()
  36. {
  37. $conn = $this->createConnectionMock();
  38. $conn->expects($this->once())->method('connect')->with($this->equalTo(0));
  39. $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
  40. $shardManager->selectGlobal();
  41. $this->assertNull($shardManager->getCurrentDistributionValue());
  42. }
  43. public function testSelectShard()
  44. {
  45. $shardId = 10;
  46. $conn = $this->createConnectionMock();
  47. $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(array('shardChoser' => $this->createPassthroughShardChoser())));
  48. $conn->expects($this->at(1))->method('connect')->with($this->equalTo($shardId));
  49. $shardManager = new PoolingShardManager($conn);
  50. $shardManager->selectShard($shardId);
  51. $this->assertEquals($shardId, $shardManager->getCurrentDistributionValue());
  52. }
  53. public function testGetShards()
  54. {
  55. $conn = $this->createConnectionMock();
  56. $conn->expects($this->any())->method('getParams')->will(
  57. $this->returnValue(
  58. array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
  59. )
  60. );
  61. $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
  62. $shards = $shardManager->getShards();
  63. $this->assertEquals(array(array('id' => 1), array('id' => 2)), $shards);
  64. }
  65. public function testQueryAll()
  66. {
  67. $sql = "SELECT * FROM table";
  68. $params = array(1);
  69. $types = array(1);
  70. $conn = $this->createConnectionMock();
  71. $conn->expects($this->at(0))->method('getParams')->will($this->returnValue(
  72. array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
  73. ));
  74. $conn->expects($this->at(1))->method('getParams')->will($this->returnValue(
  75. array('shards' => array( array('id' => 1), array('id' => 2) ), 'shardChoser' => $this->createPassthroughShardChoser())
  76. ));
  77. $conn->expects($this->at(2))->method('connect')->with($this->equalTo(1));
  78. $conn->expects($this->at(3))
  79. ->method('fetchAll')
  80. ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
  81. ->will($this->returnValue(array( array('id' => 1) ) ));
  82. $conn->expects($this->at(4))->method('connect')->with($this->equalTo(2));
  83. $conn->expects($this->at(5))
  84. ->method('fetchAll')
  85. ->with($this->equalTo($sql), $this->equalTo($params), $this->equalTo($types))
  86. ->will($this->returnValue(array( array('id' => 2) ) ));
  87. $shardManager = new PoolingShardManager($conn, $this->createPassthroughShardChoser());
  88. $result = $shardManager->queryAll($sql, $params, $types);
  89. $this->assertEquals(array(array('id' => 1), array('id' => 2)), $result);
  90. }
  91. }