WriteTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional;
  3. use Doctrine\DBAL\Types\Type;
  4. use PDO;
  5. class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
  6. {
  7. public function setUp()
  8. {
  9. parent::setUp();
  10. try {
  11. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  12. $table = new \Doctrine\DBAL\Schema\Table("write_table");
  13. $table->addColumn('id', 'integer', array('autoincrement' => true));
  14. $table->addColumn('test_int', 'integer');
  15. $table->addColumn('test_string', 'string', array('notnull' => false));
  16. $table->setPrimaryKey(array('id'));
  17. foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
  18. $this->_conn->executeQuery($sql);
  19. }
  20. } catch(\Exception $e) {
  21. }
  22. $this->_conn->executeUpdate('DELETE FROM write_table');
  23. }
  24. /**
  25. * @group DBAL-80
  26. */
  27. public function testExecuteUpdateFirstTypeIsNull()
  28. {
  29. $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
  30. $this->_conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT));
  31. $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
  32. $this->assertTrue((bool)$this->_conn->fetchColumn($sql, array("text", 1111)));
  33. }
  34. public function testExecuteUpdate()
  35. {
  36. $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
  37. $affected = $this->_conn->executeUpdate($sql);
  38. $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
  39. }
  40. public function testExecuteUpdateWithTypes()
  41. {
  42. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  43. $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR));
  44. $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
  45. }
  46. public function testPrepareRowCountReturnsAffectedRows()
  47. {
  48. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  49. $stmt = $this->_conn->prepare($sql);
  50. $stmt->bindValue(1, 1);
  51. $stmt->bindValue(2, "foo");
  52. $stmt->execute();
  53. $this->assertEquals(1, $stmt->rowCount());
  54. }
  55. public function testPrepareWithPdoTypes()
  56. {
  57. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  58. $stmt = $this->_conn->prepare($sql);
  59. $stmt->bindValue(1, 1, \PDO::PARAM_INT);
  60. $stmt->bindValue(2, "foo", \PDO::PARAM_STR);
  61. $stmt->execute();
  62. $this->assertEquals(1, $stmt->rowCount());
  63. }
  64. public function testPrepareWithDbalTypes()
  65. {
  66. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  67. $stmt = $this->_conn->prepare($sql);
  68. $stmt->bindValue(1, 1, Type::getType('integer'));
  69. $stmt->bindValue(2, "foo", Type::getType('string'));
  70. $stmt->execute();
  71. $this->assertEquals(1, $stmt->rowCount());
  72. }
  73. public function testPrepareWithDbalTypeNames()
  74. {
  75. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  76. $stmt = $this->_conn->prepare($sql);
  77. $stmt->bindValue(1, 1, 'integer');
  78. $stmt->bindValue(2, "foo", 'string');
  79. $stmt->execute();
  80. $this->assertEquals(1, $stmt->rowCount());
  81. }
  82. public function insertRows()
  83. {
  84. $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo')));
  85. $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
  86. }
  87. public function testInsert()
  88. {
  89. $this->insertRows();
  90. }
  91. public function testDelete()
  92. {
  93. $this->insertRows();
  94. $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
  95. $this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table')));
  96. $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
  97. $this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table')));
  98. }
  99. public function testUpdate()
  100. {
  101. $this->insertRows();
  102. $this->assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
  103. $this->assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
  104. $this->assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
  105. }
  106. public function testLastInsertId()
  107. {
  108. if ( ! $this->_conn->getDatabasePlatform()->prefersIdentityColumns()) {
  109. $this->markTestSkipped('Test only works on platforms with identity columns.');
  110. }
  111. $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
  112. $num = $this->_conn->lastInsertId();
  113. $this->assertNotNull($num, "LastInsertId() should not be null.");
  114. $this->assertTrue($num > 0, "LastInsertId() should be non-negative number.");
  115. }
  116. public function testLastInsertIdSequence()
  117. {
  118. if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
  119. $this->markTestSkipped('Test only works on platforms with sequences.');
  120. }
  121. $sequence = new \Doctrine\DBAL\Schema\Sequence('write_table_id_seq');
  122. try {
  123. $this->_conn->getSchemaManager()->createSequence($sequence);
  124. } catch(\Exception $e) {
  125. }
  126. $sequences = $this->_conn->getSchemaManager()->listSequences();
  127. $this->assertEquals(1, count(array_filter($sequences, function($sequence) {
  128. return $sequence->getName() === 'write_table_id_seq';
  129. })));
  130. $stmt = $this->_conn->query($this->_conn->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
  131. $nextSequenceVal = $stmt->fetchColumn();
  132. $lastInsertId = $this->_conn->lastInsertId('write_table_id_seq');
  133. $this->assertTrue($lastInsertId > 0);
  134. $this->assertEquals($nextSequenceVal, $lastInsertId);
  135. }
  136. public function testLastInsertIdNoSequenceGiven()
  137. {
  138. if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
  139. $this->markTestSkipped('Test only works on platforms with sequences.');
  140. }
  141. $this->assertFalse($this->_conn->lastInsertId( null ));
  142. }
  143. }