Skip to content

Commit

Permalink
[Db] loadDump reports sql statement which caused error (#4120)
Browse files Browse the repository at this point in the history
* [Db] loadDump reports sql statement which caused error

This functionality was accidently broken by removal of sqlToRun property in 2.2.10
Replaces #4115

* Made failing test pass on HHVM
  • Loading branch information
Naktibalda authored and DavertMik committed Apr 11, 2017
1 parent 5a254bd commit c82a621
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/Codeception/Lib/Driver/Db.php
@@ -1,6 +1,8 @@
<?php
namespace Codeception\Lib\Driver;

use Codeception\Exception\ModuleException;

class Db
{
/**
Expand Down Expand Up @@ -212,7 +214,14 @@ protected function sqlLine($sql)

protected function sqlQuery($query)
{
$this->dbh->exec($query);
try {
$this->dbh->exec($query);
} catch (\PDOException $e) {
throw new ModuleException(
'Codeception\Module\Db',
$e->getMessage() . "\nSQL query being executed: " . $query
);
}
}

public function executeQuery($query, array $params)
Expand Down
9 changes: 1 addition & 8 deletions src/Codeception/Module/Db.php
Expand Up @@ -291,14 +291,7 @@ protected function loadDump()
if (!$this->sql) {
return;
}
try {
$this->driver->load($this->sql);
} catch (\PDOException $e) {
throw new ModuleException(
__CLASS__,
$e->getMessage() . "\nSQL query being executed: " . $this->driver->sqlToRun
);
}
$this->driver->load($this->sql);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Codeception/Lib/Driver/MysqlTest.php
Expand Up @@ -14,6 +14,9 @@ class MysqlTest extends \PHPUnit_Framework_TestCase
];

protected static $sql;
/**
* @var \Codeception\Lib\Driver\MySql
*/
protected $mysql;

public static function setUpBeforeClass()
Expand Down Expand Up @@ -126,4 +129,14 @@ public function testInsertIntoBitField()
);
$this->assertEquals(1, $res->rowCount());
}

public function testLoadThrowsExceptionWhenDumpFileContainsSyntaxError()
{
$sql = "INSERT INTO `users` (`name`) VALS('')";
$expectedMessage = 'You have an error in your SQL syntax; ' .
'check the manual that corresponds to your MySQL server version for the right syntax to use near ' .
"'VALS('')' at line 1\nSQL query being executed: \n" . $sql;
$this->setExpectedException('Codeception\Exception\ModuleException', $expectedMessage);
$this->mysql->load([$sql]);
}
}

0 comments on commit c82a621

Please sign in to comment.