Added Illuminate/Database

This commit is contained in:
Igor Scheller 2018-01-14 01:48:50 +01:00
parent e0b58d2a7d
commit d4b36e9bad
7 changed files with 81 additions and 45 deletions

View File

@ -17,6 +17,7 @@
"php": ">=7.0.0",
"erusev/parsedown": "^1.6",
"illuminate/container": "5.5.*",
"illuminate/database": "5.5.*",
"illuminate/support": "^5.5",
"psr/container": "^1.0",
"psr/log": "^1.0",

View File

@ -5,10 +5,10 @@
return [
// MySQL-Connection Settings
'database' => [
'host' => env('MYSQL_HOST', (env('CI', false) ? 'mariadb' : 'localhost')),
'user' => env('MYSQL_USER', 'root'),
'pw' => env('MYSQL_PASSWORD', ''),
'db' => env('MYSQL_DATABASE', 'engelsystem'),
'host' => env('MYSQL_HOST', (env('CI', false) ? 'mariadb' : 'localhost')),
'database' => env('MYSQL_DATABASE', 'engelsystem'),
'username' => env('MYSQL_USER', 'root'),
'password' => env('MYSQL_PASSWORD', ''),
],
// For accessing stats

View File

@ -5,6 +5,7 @@ namespace Engelsystem;
use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
use Engelsystem\Container\ServiceProvider;
use Illuminate\Container\Container as IlluminateContainer;
use Psr\Container\ContainerInterface;
class Application extends Container
@ -44,6 +45,7 @@ class Application extends Container
$this->instance('container', $this);
$this->instance(Container::class, $this);
$this->instance(Application::class, $this);
$this->instance(IlluminateContainer::class, $this);
$this->bind(ContainerInterface::class, Application::class);
}

View File

@ -4,23 +4,39 @@ namespace Engelsystem\Database;
use Engelsystem\Container\ServiceProvider;
use Exception;
use PDO;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Events\Dispatcher as EventsDispatcher;
class DatabaseServiceProvider extends ServiceProvider
{
public function register()
{
$config = $this->app->get('config');
Db::connect(
'mysql:host=' . $config->get('database')['host']
. ';dbname=' . $config->get('database')['db']
. ';charset=utf8',
$config->get('database')['user'],
$config->get('database')['pw']
) || $this->exitOnError();
$capsule = $this->app->make(CapsuleManager::class);
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConfig = $config->get('database');
$capsule->addConnection(array_merge([
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], $dbConfig));
$capsule->setAsGlobal();
$capsule->bootEloquent();
try {
$capsule->getConnection()->getPdo();
} catch (\PDOException $e) {
$this->exitOnError();
}
$this->app->instance('db', $capsule);
Db::setDbManager($capsule);
}
/**

View File

@ -2,14 +2,14 @@
namespace Engelsystem\Database;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use PDO;
use PDOException;
use PDOStatement;
class Db
{
/** @var PDO */
protected static $db;
/** @var CapsuleManager */
protected static $dbManager;
/** @var PDOStatement */
protected static $stm = null;
@ -18,23 +18,13 @@ class Db
protected static $lastStatus = true;
/**
* Connect to database
* Set the database connection manager
*
* @param string $dsn
* @param string $username
* @param string $password
* @param array $options
* @return bool
* @param CapsuleManager $dbManager
*/
public static function connect($dsn, $username = null, $password = null, $options = [])
public static function setDbManager($dbManager)
{
try {
self::$db = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
return false;
}
return true;
self::$dbManager = $dbManager;
}
/**
@ -46,7 +36,7 @@ class Db
*/
public static function query($query, array $bindings = [])
{
self::$stm = self::$db->prepare($query);
self::$stm = self::getPdo()->prepare($query);
self::$lastStatus = self::$stm->execute($bindings);
return self::$stm;
@ -60,7 +50,7 @@ class Db
*/
public static function unprepared($query)
{
self::$stm = self::$db->query($query);
self::$stm = self::getPdo()->query($query);
self::$lastStatus = (self::$stm instanceof PDOStatement);
return self::$lastStatus;
@ -175,7 +165,7 @@ class Db
*/
public static function getPdo()
{
return self::$db;
return self::$dbManager->getConnection()->getPdo();
}
/**

View File

@ -24,10 +24,10 @@ class DatabaseServiceProviderTest extends ServiceProviderTest
$this->setExpects($app, 'get', ['config'], $config);
$this->setExpects($config, 'get', ['database'], [
'host' => 'localhost',
'db' => 'database',
'user' => 'user',
'pw' => 'password',
'host' => 'localhost',
'database' => 'database',
'username' => 'user',
'password' => 'password',
], $this->atLeastOnce());
$this->expectException(Exception::class);

View File

@ -3,24 +3,46 @@
namespace Engelsystem\Test\Unit\Database;
use Engelsystem\Database\Db;
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Database\Connection as DatabaseConnection;
use PDO;
use PDOStatement;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use ReflectionObject;
use Throwable;
class DbTest extends TestCase
{
/**
* @covers \Engelsystem\Database\Db::connect()
* @covers \Engelsystem\Database\Db::setDbManager()
*/
public function testConnect()
public function testSetDbManager()
{
$result = Db::connect('mysql:host=localhost;dbname=someTestDatabaseThatDoesNotExist;charset=utf8');
$this->assertFalse($result);
/** @var MockObject|Pdo $pdo */
$pdo = $this->getMockBuilder(Pdo::class)
->disableOriginalConstructor()
->getMock();
/** @var MockObject|CapsuleManager $dbManager */
$dbManager = $this->getMockBuilder(CapsuleManager::class)
->disableOriginalConstructor()
->getMock();
/** @var MockObject|DatabaseConnection $dbManager */
$databaseConnection = $this->getMockBuilder(DatabaseConnection::class)
->disableOriginalConstructor()
->getMock();
$result = Db::connect('sqlite::memory:');
$this->assertTrue($result);
$dbManager
->expects($this->atLeastOnce())
->method('getConnection')
->willReturn($databaseConnection);
$databaseConnection
->expects($this->atLeastOnce())
->method('getPdo')
->willReturn($pdo);
Db::setDbManager($dbManager);
$this->assertEquals($pdo, Db::getPdo());
}
/**
@ -167,7 +189,12 @@ class DbTest extends TestCase
*/
protected function setUp()
{
Db::connect('sqlite::memory:');
$dbManager = new CapsuleManager();
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
$dbManager->setAsGlobal();
$dbManager->bootEloquent();
Db::setDbManager($dbManager);
Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Db::query(
'