WriteTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Functional;
  3. use Doctrine\DBAL\Types\Type;
  4. use PDO;
  5. require_once __DIR__ . '/../../TestInit.php';
  6. class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
  7. {
  8. public function setUp()
  9. {
  10. parent::setUp();
  11. try {
  12. /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
  13. $table = new \Doctrine\DBAL\Schema\Table("write_table");
  14. $table->addColumn('test_int', 'integer');
  15. $table->addColumn('test_string', 'string', array('notnull' => false));
  16. $sm = $this->_conn->getSchemaManager();
  17. $sm->createTable($table);
  18. } catch(\Exception $e) {
  19. }
  20. $this->_conn->executeUpdate('DELETE FROM write_table');
  21. }
  22. /**
  23. * @group DBAL-80
  24. */
  25. public function testExecuteUpdateFirstTypeIsNull()
  26. {
  27. $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
  28. $this->_conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT));
  29. $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
  30. $this->assertTrue((bool)$this->_conn->fetchColumn($sql, array("text", 1111)));
  31. }
  32. public function testExecuteUpdate()
  33. {
  34. $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
  35. $affected = $this->_conn->executeUpdate($sql);
  36. $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
  37. }
  38. public function testExecuteUpdateWithTypes()
  39. {
  40. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  41. $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR));
  42. $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
  43. }
  44. public function testPrepareRowCountReturnsAffectedRows()
  45. {
  46. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  47. $stmt = $this->_conn->prepare($sql);
  48. $stmt->bindValue(1, 1);
  49. $stmt->bindValue(2, "foo");
  50. $stmt->execute();
  51. $this->assertEquals(1, $stmt->rowCount());
  52. }
  53. public function testPrepareWithPdoTypes()
  54. {
  55. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  56. $stmt = $this->_conn->prepare($sql);
  57. $stmt->bindValue(1, 1, \PDO::PARAM_INT);
  58. $stmt->bindValue(2, "foo", \PDO::PARAM_STR);
  59. $stmt->execute();
  60. $this->assertEquals(1, $stmt->rowCount());
  61. }
  62. public function testPrepareWithDbalTypes()
  63. {
  64. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  65. $stmt = $this->_conn->prepare($sql);
  66. $stmt->bindValue(1, 1, Type::getType('integer'));
  67. $stmt->bindValue(2, "foo", Type::getType('string'));
  68. $stmt->execute();
  69. $this->assertEquals(1, $stmt->rowCount());
  70. }
  71. public function testPrepareWithDbalTypeNames()
  72. {
  73. $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
  74. $stmt = $this->_conn->prepare($sql);
  75. $stmt->bindValue(1, 1, 'integer');
  76. $stmt->bindValue(2, "foo", 'string');
  77. $stmt->execute();
  78. $this->assertEquals(1, $stmt->rowCount());
  79. }
  80. public function insertRows()
  81. {
  82. $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1)));
  83. $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2)));
  84. }
  85. public function testInsert()
  86. {
  87. $this->insertRows();
  88. }
  89. public function testDelete()
  90. {
  91. $this->insertRows();
  92. $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
  93. $this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table')));
  94. $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
  95. $this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table')));
  96. }
  97. public function testUpdate()
  98. {
  99. $this->insertRows();
  100. $this->assertEquals(1, $this->_conn->update('write_table', array('test_int' => 2), array('test_int' => 1)));
  101. $this->assertEquals(2, $this->_conn->update('write_table', array('test_int' => 3), array('test_int' => 2)));
  102. }
  103. }