3 namespace Doctrine\Tests\DBAL\Functional;
4 use Doctrine\DBAL\Types\Type;
7 class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
9 public function setUp()
14 /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
15 $table = new \Doctrine\DBAL\Schema\Table("write_table");
16 $table->addColumn('id', 'integer', array('autoincrement' => true));
17 $table->addColumn('test_int', 'integer');
18 $table->addColumn('test_string', 'string', array('notnull' => false));
19 $table->setPrimaryKey(array('id'));
21 foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
22 $this->_conn->executeQuery($sql);
24 } catch(\Exception $e) {
27 $this->_conn->executeUpdate('DELETE FROM write_table');
33 public function testExecuteUpdateFirstTypeIsNull()
35 $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
36 $this->_conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT));
38 $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
39 $this->assertTrue((bool)$this->_conn->fetchColumn($sql, array("text", 1111)));
42 public function testExecuteUpdate()
44 $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
45 $affected = $this->_conn->executeUpdate($sql);
47 $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
50 public function testExecuteUpdateWithTypes()
52 $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
53 $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR));
55 $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
58 public function testPrepareRowCountReturnsAffectedRows()
60 $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
61 $stmt = $this->_conn->prepare($sql);
63 $stmt->bindValue(1, 1);
64 $stmt->bindValue(2, "foo");
67 $this->assertEquals(1, $stmt->rowCount());
70 public function testPrepareWithPdoTypes()
72 $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
73 $stmt = $this->_conn->prepare($sql);
75 $stmt->bindValue(1, 1, \PDO::PARAM_INT);
76 $stmt->bindValue(2, "foo", \PDO::PARAM_STR);
79 $this->assertEquals(1, $stmt->rowCount());
82 public function testPrepareWithDbalTypes()
84 $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
85 $stmt = $this->_conn->prepare($sql);
87 $stmt->bindValue(1, 1, Type::getType('integer'));
88 $stmt->bindValue(2, "foo", Type::getType('string'));
91 $this->assertEquals(1, $stmt->rowCount());
94 public function testPrepareWithDbalTypeNames()
96 $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
97 $stmt = $this->_conn->prepare($sql);
99 $stmt->bindValue(1, 1, 'integer');
100 $stmt->bindValue(2, "foo", 'string');
103 $this->assertEquals(1, $stmt->rowCount());
106 public function insertRows()
108 $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo')));
109 $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
112 public function testInsert()
117 public function testDelete()
121 $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
122 $this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table')));
124 $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
125 $this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table')));
128 public function testUpdate()
132 $this->assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
133 $this->assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
134 $this->assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
137 public function testLastInsertId()
139 if ( ! $this->_conn->getDatabasePlatform()->prefersIdentityColumns()) {
140 $this->markTestSkipped('Test only works on platforms with identity columns.');
143 $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
144 $num = $this->_conn->lastInsertId();
146 $this->assertNotNull($num, "LastInsertId() should not be null.");
147 $this->assertTrue($num > 0, "LastInsertId() should be non-negative number.");
150 public function testLastInsertIdSequence()
152 if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
153 $this->markTestSkipped('Test only works on platforms with sequences.');
156 $sequence = new \Doctrine\DBAL\Schema\Sequence('write_table_id_seq');
158 $this->_conn->getSchemaManager()->createSequence($sequence);
159 } catch(\Exception $e) {
162 $sequences = $this->_conn->getSchemaManager()->listSequences();
163 $this->assertEquals(1, count(array_filter($sequences, function($sequence) {
164 return $sequence->getName() === 'write_table_id_seq';
167 $stmt = $this->_conn->query($this->_conn->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
168 $nextSequenceVal = $stmt->fetchColumn();
170 $lastInsertId = $this->_conn->lastInsertId('write_table_id_seq');
172 $this->assertTrue($lastInsertId > 0);
173 $this->assertEquals($nextSequenceVal, $lastInsertId);
176 public function testLastInsertIdNoSequenceGiven()
178 if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
179 $this->markTestSkipped('Test only works on platforms with sequences.');
182 $this->assertFalse($this->_conn->lastInsertId( null ));