/**
  * Test replace mode.
  */
 public function testReplace()
 {
     $last_insert_id = $this->connection->insert('writers', ['id' => 1, 'name' => 'Anton Chekhov', 'birthday' => new DateTime('1860-01-29')], ConnectionInterface::REPLACE);
     $this->assertEquals(1, $last_insert_id);
     $this->assertEquals(3, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`'));
     $this->assertEquals(['id' => 1, 'name' => 'Anton Chekhov', 'birthday' => '1860-01-29'], $this->connection->executeFirstRow('SELECT * FROM `writers` WHERE `id` = ?', $last_insert_id));
 }
 /**
  * Test delete where conditions are an array that needs to be prepared.
  */
 public function testDeleteWithConditionsThatNeedToBePrepared()
 {
     $affected_rows = $this->connection->delete('writers', ['`name` = ?', 'Leo Tolstoy']);
     $this->assertEquals(1, $affected_rows);
     $this->assertEquals(2, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`'));
     $this->assertEquals(0, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers` WHERE `name` = ?', 'Leo Tolstoy'));
 }
 /**
  * Execute first cell.
  */
 public function testExecuteFirstCell()
 {
     $this->assertEquals(1, $this->connection->executeFirstCell('SELECT `id` FROM `writers` WHERE `name` = ?', 'Leo Tolstoy'));
     $this->assertEquals(2, $this->connection->executeFirstCell('SELECT `id` FROM `writers` WHERE `name` = ?', 'Alexander Pushkin'));
     $this->assertEquals(3, $this->connection->executeFirstCell('SELECT `id` FROM `writers` WHERE `name` = ?', 'Fyodor Dostoyevsky'));
     $this->assertEquals('Leo Tolstoy', $this->connection->executeFirstCell('SELECT `name` FROM `writers` WHERE `id` = ?', 1));
     $this->assertEquals('Alexander Pushkin', $this->connection->executeFirstCell('SELECT `name` FROM `writers` WHERE `id` = ?', 2));
     $this->assertEquals('Fyodor Dostoyevsky', $this->connection->executeFirstCell('SELECT `name` FROM `writers` WHERE `id` = ?', 3));
 }
 /**
  * Test if rows can't be inserted escaped once batch is done.
  *
  * @expectedException \RuntimeException
  */
 public function testRowsCantBeInsertedEscapedOnceDone()
 {
     $batch_insert = $this->connection->batchInsert('writers', ['name', 'birthday'], 350);
     $batch_insert->insert('Leo Tolstoy', new DateTime('1828-09-09'));
     $batch_insert->insert('Alexander Pushkin', new DateTime('1799-06-06'));
     $this->assertEquals(0, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`'));
     $batch_insert->done();
     $this->assertEquals(2, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`'));
     $batch_insert->insertEscaped($this->connection->escapeValue('Fyodor Dostoyevsky'), $this->connection->escapeValue(new DateTime('1821-11-11')));
 }
 /**
  * Test error in transcation callback.
  */
 public function testErrorInTransactionCallback()
 {
     $this->assertEquals(3, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`'));
     /** @var Exception $exception_caught */
     $exception_caught = false;
     $this->connection->transact(function () {
         $this->connection->execute('INSERT INTO `writers` (`name`, `birthday`) VALUES (?, ?)', 'Anton Chekhov', new DateTime('1860-01-29'));
         throw new RuntimeException('Throwing an exception here');
     }, null, function (Exception $e) use(&$exception_caught) {
         $exception_caught = $e;
     });
     $this->assertInstanceOf('\\RuntimeException', $exception_caught);
     $this->assertEquals('Throwing an exception here', $exception_caught->getMessage());
     $this->assertEquals(3, $this->connection->executeFirstCell('SELECT COUNT(`id`) AS "row_count" FROM `writers`'));
 }