123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace Doctrine\Tests\DBAL\Functional;
- use Doctrine\DBAL\Types\Type;
- use PDO;
- class WriteTest extends \Doctrine\Tests\DbalFunctionalTestCase
- {
- public function setUp()
- {
- parent::setUp();
- try {
- /* @var $sm \Doctrine\DBAL\Schema\AbstractSchemaManager */
- $table = new \Doctrine\DBAL\Schema\Table("write_table");
- $table->addColumn('id', 'integer', array('autoincrement' => true));
- $table->addColumn('test_int', 'integer');
- $table->addColumn('test_string', 'string', array('notnull' => false));
- $table->setPrimaryKey(array('id'));
- foreach ($this->_conn->getDatabasePlatform()->getCreateTableSQL($table) AS $sql) {
- $this->_conn->executeQuery($sql);
- }
- } catch(\Exception $e) {
- }
- $this->_conn->executeUpdate('DELETE FROM write_table');
- }
- /**
- * @group DBAL-80
- */
- public function testExecuteUpdateFirstTypeIsNull()
- {
- $sql = "INSERT INTO write_table (test_string, test_int) VALUES (?, ?)";
- $this->_conn->executeUpdate($sql, array("text", 1111), array(null, PDO::PARAM_INT));
- $sql = "SELECT * FROM write_table WHERE test_string = ? AND test_int = ?";
- $this->assertTrue((bool)$this->_conn->fetchColumn($sql, array("text", 1111)));
- }
- public function testExecuteUpdate()
- {
- $sql = "INSERT INTO write_table (test_int) VALUES ( " . $this->_conn->quote(1) . ")";
- $affected = $this->_conn->executeUpdate($sql);
- $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
- }
- public function testExecuteUpdateWithTypes()
- {
- $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
- $affected = $this->_conn->executeUpdate($sql, array(1, 'foo'), array(\PDO::PARAM_INT, \PDO::PARAM_STR));
- $this->assertEquals(1, $affected, "executeUpdate() should return the number of affected rows!");
- }
- public function testPrepareRowCountReturnsAffectedRows()
- {
- $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
- $stmt = $this->_conn->prepare($sql);
- $stmt->bindValue(1, 1);
- $stmt->bindValue(2, "foo");
- $stmt->execute();
- $this->assertEquals(1, $stmt->rowCount());
- }
- public function testPrepareWithPdoTypes()
- {
- $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
- $stmt = $this->_conn->prepare($sql);
- $stmt->bindValue(1, 1, \PDO::PARAM_INT);
- $stmt->bindValue(2, "foo", \PDO::PARAM_STR);
- $stmt->execute();
- $this->assertEquals(1, $stmt->rowCount());
- }
- public function testPrepareWithDbalTypes()
- {
- $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
- $stmt = $this->_conn->prepare($sql);
- $stmt->bindValue(1, 1, Type::getType('integer'));
- $stmt->bindValue(2, "foo", Type::getType('string'));
- $stmt->execute();
- $this->assertEquals(1, $stmt->rowCount());
- }
- public function testPrepareWithDbalTypeNames()
- {
- $sql = "INSERT INTO write_table (test_int, test_string) VALUES (?, ?)";
- $stmt = $this->_conn->prepare($sql);
- $stmt->bindValue(1, 1, 'integer');
- $stmt->bindValue(2, "foo", 'string');
- $stmt->execute();
- $this->assertEquals(1, $stmt->rowCount());
- }
- public function insertRows()
- {
- $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 1, 'test_string' => 'foo')));
- $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
- }
- public function testInsert()
- {
- $this->insertRows();
- }
- public function testDelete()
- {
- $this->insertRows();
- $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 2)));
- $this->assertEquals(1, count($this->_conn->fetchAll('SELECT * FROM write_table')));
- $this->assertEquals(1, $this->_conn->delete('write_table', array('test_int' => 1)));
- $this->assertEquals(0, count($this->_conn->fetchAll('SELECT * FROM write_table')));
- }
- public function testUpdate()
- {
- $this->insertRows();
- $this->assertEquals(1, $this->_conn->update('write_table', array('test_string' => 'bar'), array('test_string' => 'foo')));
- $this->assertEquals(2, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
- $this->assertEquals(0, $this->_conn->update('write_table', array('test_string' => 'baz'), array('test_string' => 'bar')));
- }
- public function testLastInsertId()
- {
- if ( ! $this->_conn->getDatabasePlatform()->prefersIdentityColumns()) {
- $this->markTestSkipped('Test only works on platforms with identity columns.');
- }
- $this->assertEquals(1, $this->_conn->insert('write_table', array('test_int' => 2, 'test_string' => 'bar')));
- $num = $this->_conn->lastInsertId();
- $this->assertNotNull($num, "LastInsertId() should not be null.");
- $this->assertTrue($num > 0, "LastInsertId() should be non-negative number.");
- }
- public function testLastInsertIdSequence()
- {
- if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
- $this->markTestSkipped('Test only works on platforms with sequences.');
- }
- $sequence = new \Doctrine\DBAL\Schema\Sequence('write_table_id_seq');
- try {
- $this->_conn->getSchemaManager()->createSequence($sequence);
- } catch(\Exception $e) {
- }
- $sequences = $this->_conn->getSchemaManager()->listSequences();
- $this->assertEquals(1, count(array_filter($sequences, function($sequence) {
- return $sequence->getName() === 'write_table_id_seq';
- })));
- $stmt = $this->_conn->query($this->_conn->getDatabasePlatform()->getSequenceNextValSQL('write_table_id_seq'));
- $nextSequenceVal = $stmt->fetchColumn();
- $lastInsertId = $this->_conn->lastInsertId('write_table_id_seq');
- $this->assertTrue($lastInsertId > 0);
- $this->assertEquals($nextSequenceVal, $lastInsertId);
- }
- public function testLastInsertIdNoSequenceGiven()
- {
- if ( ! $this->_conn->getDatabasePlatform()->supportsSequences()) {
- $this->markTestSkipped('Test only works on platforms with sequences.');
- }
- $this->assertFalse($this->_conn->lastInsertId( null ));
- }
- }
|