Add tests for PDO exceptions rethrowing [MAILPOET-966]

This commit is contained in:
stoletniy
2017-07-03 12:32:49 +03:00
committed by pavel-mailpoet
parent 693117eb40
commit d5227a9f2c
2 changed files with 53 additions and 1 deletions

View File

@ -1,5 +1,6 @@
<?php
use Codeception\Util\Stub;
use MailPoet\Config\Database;
use MailPoet\Config\Env;
@ -58,4 +59,26 @@ class DatabaseTestTest extends MailPoetTest {
// time zone should be set based on WP's time zone
expect($result->time_zone)->equals(Env::$db_timezone_offset);
}
function testItRethrowsPDOExceptions() {
$message = 'Error message';
$pdo = Stub::make(
'PDO',
array(
'prepare' => function() use ($message) {
throw new \PDOException($message);
}
)
);
\ORM::setDb($pdo);
try {
$this->database->setupDriverOptions();
$this->fail('Exception was not thrown');
} catch(\Exception $e) {
expect($e instanceof \PDOException)->false();
expect($e->getMessage())->equals($message);
}
// Remove the DB stub
$this->_before();
}
}

View File

@ -0,0 +1,29 @@
<?php
use Codeception\Util\Stub;
use MailPoet\Models\Model;
class ModelTest extends MailPoetTest {
function testItRethrowsPDOExceptions() {
$message = 'Error message';
$model = Stub::make('MailPoet\Models\Model');
$pdo = Stub::make(
'PDO',
array(
'prepare' => function() use ($message) {
throw new \PDOException($message);
}
)
);
\ORM::setDb($pdo);
try {
$model::findMany();
$this->fail('Exception was not thrown');
} catch(\Exception $e) {
expect($e instanceof \PDOException)->false();
expect($e->getMessage())->equals($message);
}
// Remove the DB stub
\ORM::setDb(null);
}
}