Skip to content

Commit

Permalink
Fixed Issue/4059 (Codeception#4071)
Browse files Browse the repository at this point in the history
* [Db] Run the last statement in file even if it doesn't end with delimiter

Fixes Codeception#4059

* Eliminated property sqltoRun
  • Loading branch information
Naktibalda authored and bulzgkri committed Jun 16, 2017
1 parent 271083e commit c088c4b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
14 changes: 6 additions & 8 deletions src/Codeception/Lib/Driver/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class Db
protected $user;
protected $password;

/**
* @var string
*/
public $sqlToRun;

/**
* associative array with table name => primary-key
*
Expand Down Expand Up @@ -123,11 +118,14 @@ public function load($sql)
$query .= "\n" . rtrim($sqlLine);

if (substr($query, -1 * $delimiterLength, $delimiterLength) == $delimiter) {
$this->sqlToRun = substr($query, 0, -1 * $delimiterLength);
$this->sqlQuery($this->sqlToRun);
$query = "";
$this->sqlQuery(substr($query, 0, -1 * $delimiterLength));
$query = '';
}
}

if ($query !== '') {
$this->sqlQuery($query);
}
}

public function insert($tableName, array &$data)
Expand Down
7 changes: 5 additions & 2 deletions src/Codeception/Lib/Driver/Oci.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ public function load($sql)
$query .= "\n" . rtrim($sqlLine);

if (substr($query, -1 * $delimiterLength, $delimiterLength) == $delimiter) {
$this->sqlToRun = substr($query, 0, -1 * $delimiterLength);
$this->sqlQuery($this->sqlToRun);
$this->sqlQuery(substr($query, 0, -1 * $delimiterLength));
$query = "";
}
}

if ($query !== '') {
$this->sqlQuery($query);
}
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Codeception/Lib/Driver/PostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ public function load($sql)
$query .= "\n" . rtrim($sqlLine);

if (!$dollarsOpen && substr($query, -1 * $delimiterLength, $delimiterLength) == $delimiter) {
$this->sqlToRun = substr($query, 0, -1 * $delimiterLength);
$this->sqlQuery($this->sqlToRun);
$this->sqlQuery(substr($query, 0, -1 * $delimiterLength));
$query = '';
}
}

if ($query !== '') {
$this->sqlQuery($query);
}
}

public function cleanup()
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/Codeception/Lib/Driver/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,16 @@ public function testGetPrimaryColumnThrowsExceptionIfTableHasCompositePrimaryKey
);
$this->postgres->getPrimaryColumn('composite_pk');
}

/**
* @issue https://github.com/Codeception/Codeception/issues/4059
*/
public function testLoadDumpEndingWithoutDelimiter()
{
$newDriver = new \Codeception\Lib\Driver\PostgreSql(self::$config['dsn'], self::$config['user'], self::$config['password']);
$newDriver->load(['INSERT INTO empty_table VALUES(1, \'test\')']);
$res = $newDriver->getDbh()->query("select * from empty_table where field = 'test'");
$this->assertNotEquals(false, $res);
$this->assertNotEmpty($res->fetchAll());
}
}
15 changes: 15 additions & 0 deletions tests/unit/Codeception/Lib/Driver/SqliteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class SqliteTest extends \PHPUnit_Framework_TestCase
'password' => ''
);

/**
* @var \Codeception\Lib\Driver\Sqlite
*/
protected static $sqlite;
protected static $sql;

Expand Down Expand Up @@ -124,4 +127,16 @@ public function testThrowsExceptionIfInMemoryDatabaseIsUsed()

Db::create('sqlite::memory:', '', '');
}

/**
* @issue https://github.com/Codeception/Codeception/issues/4059
*/
public function testLoadDumpEndingWithoutDelimiter()
{
$newDriver = new \Codeception\Lib\Driver\Sqlite(self::$config['dsn'], '', '');
$newDriver->load(['INSERT INTO empty_table VALUES(1, "test")']);
$res = $newDriver->getDbh()->query("select * from empty_table where field = 'test'");
$this->assertNotEquals(false, $res);
$this->assertNotEmpty($res->fetchAll());
}
}

0 comments on commit c088c4b

Please sign in to comment.