diff --git a/composer.json b/composer.json index 83b623da..9c0602b6 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/config.default.php b/config/config.default.php index ed40aaf9..606fa143 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -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 diff --git a/src/Application.php b/src/Application.php index c9023c7b..68ce9e33 100644 --- a/src/Application.php +++ b/src/Application.php @@ -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); } diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index 49fb4af5..306ffaca 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -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); } /** diff --git a/src/Database/Db.php b/src/Database/Db.php index 114bd8fc..aa9b1a27 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -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(); } /** diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php index 61848c35..911af399 100644 --- a/tests/Unit/Database/DatabaseServiceProviderTest.php +++ b/tests/Unit/Database/DatabaseServiceProviderTest.php @@ -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); diff --git a/tests/Unit/Database/DbTest.php b/tests/Unit/Database/DbTest.php index 63607cad..17829e77 100644 --- a/tests/Unit/Database/DbTest.php +++ b/tests/Unit/Database/DbTest.php @@ -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( '