Skip to content

Commit

Permalink
test app
Browse files Browse the repository at this point in the history
  • Loading branch information
JanHuang committed Apr 20, 2017
1 parent d447664 commit 8d68fd2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
44 changes: 21 additions & 23 deletions src/Model/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace FastD\Model;

use Medoo\Medoo;
use PDO;
use PDOException;

/**
* Class Database.
Expand All @@ -31,6 +33,9 @@ public function __construct(array $options = null)
$this->config = $options;

parent::__construct($this->config);

$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}

/**
Expand All @@ -41,31 +46,22 @@ public function reconnect()
$this->__construct($this->config);
}

/**
* check database gone away.
*/
public function checkGoneAway()
{
list(, $code) = $this->pdo->errorInfo();
if (2006 === $code || 2013 === $code) {
$this->reconnect();
}
}

/**
* @param $query
*
* @return bool|\PDOStatement
*/
public function query($query)
{
$result = parent::query($query);
if (false === $result) {
$this->checkGoneAway();
$result = parent::query($query);
try {
return parent::query($query);
} catch (PDOException $e) {
if('HY000' !== $e->getCode()) {
throw $e;
}
$this->reconnect();
return parent::query($query);
}

return $result;
}

/**
Expand All @@ -75,12 +71,14 @@ public function query($query)
*/
public function exec($query)
{
$result = parent::exec($query);
if (false === $result) {
$this->checkGoneAway();
$result = parent::exec($query);
try {
return parent::exec($query);
} catch (PDOException $e) {
if('HY000' !== $e->getCode()) {
throw $e;
}
$this->reconnect();
return parent::exec($query);
}

return $result;
}
}
31 changes: 29 additions & 2 deletions tests/Model/DatabaseTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
<?php
use FastD\Application;

/**
* @author jan huang <[email protected]>
* @copyright 2016
*
* @see https://www.github.com/janhuang
* @see http://www.fast-d.cn/
*/
class DatabaseTest extends PHPUnit_Framework_TestCase
class DatabaseTest extends \FastD\TestCase
{
public function testConnection()
public function createApplication()
{
$app = new Application(__DIR__.'/../app/default');

return $app;
}

public function createDatabase()
{
$config = config()->get('database.default');
return new \FastD\Model\Database([
'database_type' => isset($config['adapter']) ? $config['adapter'] : 'mysql',
'database_name' => $config['name'],
'server' => $config['host'],
'username' => $config['user'],
'password' => $config['pass'],
'charset' => isset($config['charset']) ? $config['charset'] : 'utf8',
'port' => isset($config['port']) ? $config['port'] : 3306,
'prefix' => isset($config['prefix']) ? $config['prefix'] : '',
]);
}

public function testGoneAwayConnection()
{
$database = $this->createDatabase();
$tables = $database->query('show tables;')->fetchAll();
$this->assertTrue(true);
}
}
6 changes: 0 additions & 6 deletions tests/app/minimal/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
*/
'name' => 'fast-d',

/*
* Application logger path
*/
'log' => [
],

/*
* Exception handle
*/
Expand Down

0 comments on commit 8d68fd2

Please sign in to comment.