MasterSlaveConnectionTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional;
  3. use Doctrine\Tests\DbalFunctionalTestCase;
  4. use Doctrine\DBAL\DriverManager;
  5. /**
  6. * @group DBAL-20
  7. */
  8. class MasterSlaveConnectionTest extends DbalFunctionalTestCase
  9. {
  10. public function setUp()
  11. {
  12. parent::setUp();
  13. if ($this->_conn->getDatabasePlatform()->getName() == "sqlite") {
  14. $this->markTestSkipped('Test does not work on sqlite.');
  15. }
  16. try {
  17. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  18. $table = new \Doctrine\DBAL\Schema\Table("master_slave_table");
  19. $table->addColumn('test_int', 'integer');
  20. $table->setPrimaryKey(array('test_int'));
  21. $sm = $this->_conn->getSchemaManager();
  22. $sm->createTable($table);
  23. } catch(\Exception $e) {
  24. }
  25. $this->_conn->executeUpdate('DELETE FROM master_slave_table');
  26. $this->_conn->insert('master_slave_table', array('test_int' => 1));
  27. }
  28. public function createMasterSlaveConnection($keepSlave = false)
  29. {
  30. $params = $this->_conn->getParams();
  31. $params['master'] = $params;
  32. $params['slaves'] = array($params, $params);
  33. $params['keepSlave'] = $keepSlave;
  34. $params['wrapperClass'] = 'Doctrine\DBAL\Connections\MasterSlaveConnection';
  35. return DriverManager::getConnection($params);
  36. }
  37. public function testMasterOnConnect()
  38. {
  39. $conn = $this->createMasterSlaveConnection();
  40. $this->assertFalse($conn->isConnectedToMaster());
  41. $conn->connect('slave');
  42. $this->assertFalse($conn->isConnectedToMaster());
  43. $conn->connect('master');
  44. $this->assertTrue($conn->isConnectedToMaster());
  45. }
  46. public function testNoMasterOnExecuteQuery()
  47. {
  48. $conn = $this->createMasterSlaveConnection();
  49. $sql = "SELECT count(*) as num FROM master_slave_table";
  50. $data = $conn->fetchAll($sql);
  51. $data[0] = array_change_key_case($data[0], CASE_LOWER);
  52. $this->assertEquals(1, $data[0]['num']);
  53. $this->assertFalse($conn->isConnectedToMaster());
  54. }
  55. public function testMasterOnWriteOperation()
  56. {
  57. $conn = $this->createMasterSlaveConnection();
  58. $conn->insert('master_slave_table', array('test_int' => 30));
  59. $this->assertTrue($conn->isConnectedToMaster());
  60. $sql = "SELECT count(*) as num FROM master_slave_table";
  61. $data = $conn->fetchAll($sql);
  62. $data[0] = array_change_key_case($data[0], CASE_LOWER);
  63. $this->assertEquals(2, $data[0]['num']);
  64. $this->assertTrue($conn->isConnectedToMaster());
  65. }
  66. /**
  67. * @group DBAL-335
  68. */
  69. public function testKeepSlaveBeginTransactionStaysOnMaster()
  70. {
  71. $conn = $this->createMasterSlaveConnection($keepSlave = true);
  72. $conn->connect('slave');
  73. $conn->insert('master_slave_table', array('test_int' => 40));
  74. $this->assertTrue($conn->isConnectedToMaster());
  75. $conn->connect();
  76. $this->assertTrue($conn->isConnectedToMaster());
  77. $conn->connect('slave');
  78. $this->assertFalse($conn->isConnectedToMaster());
  79. }
  80. }