From 78cddecef3ed0e29445a699ef7e6df56352779a4 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 15 Jan 2018 23:31:19 +0100 Subject: [PATCH] DatabaseServiceProvider: Added unit tests --- src/Database/DatabaseServiceProvider.php | 4 +- .../Database/DatabaseServiceProviderTest.php | 71 ++++++++++++++++--- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index 306ffaca..818b76c4 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -5,7 +5,7 @@ namespace Engelsystem\Database; use Engelsystem\Container\ServiceProvider; use Exception; use Illuminate\Database\Capsule\Manager as CapsuleManager; -use Illuminate\Events\Dispatcher as EventsDispatcher; +use PDOException; class DatabaseServiceProvider extends ServiceProvider { @@ -31,7 +31,7 @@ class DatabaseServiceProvider extends ServiceProvider try { $capsule->getConnection()->getPdo(); - } catch (\PDOException $e) { + } catch (PDOException $e) { $this->exitOnError(); } diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php index 911af399..2e05d27f 100644 --- a/tests/Unit/Database/DatabaseServiceProviderTest.php +++ b/tests/Unit/Database/DatabaseServiceProviderTest.php @@ -6,32 +6,85 @@ use Engelsystem\Config\Config; use Engelsystem\Database\DatabaseServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; use Exception; -use PHPUnit_Framework_MockObject_MockObject; +use Illuminate\Database\Capsule\Manager as CapsuleManager; +use Illuminate\Database\Connection; +use PDOException; +use PHPUnit_Framework_MockObject_MockObject as MockObject; class DatabaseServiceProviderTest extends ServiceProviderTest { /** * @covers \Engelsystem\Database\DatabaseServiceProvider::register() - * @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError() */ public function testRegister() { - /** @var PHPUnit_Framework_MockObject_MockObject|Config $config */ - $config = $this->getMockBuilder(Config::class) - ->getMock(); + list($app, $dbManager) = $this->prepare(['driver' => 'sqlite', 'database' => ':memory:']); - $app = $this->getApp(['get']); + $this->setExpects($app, 'instance', ['db', $dbManager]); - $this->setExpects($app, 'get', ['config'], $config); - $this->setExpects($config, 'get', ['database'], [ + $serviceProvider = new DatabaseServiceProvider($app); + $serviceProvider->register(); + } + + /** + * @covers \Engelsystem\Database\DatabaseServiceProvider::register() + * @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError() + */ + public function testRegisterError() + { + list($app) = $this->prepare([ 'host' => 'localhost', 'database' => 'database', 'username' => 'user', 'password' => 'password', - ], $this->atLeastOnce()); + ], true); + $this->expectException(Exception::class); $serviceProvider = new DatabaseServiceProvider($app); $serviceProvider->register(); } + + /** + * Prepare some mocks + * + * @param array $dbConfigData + * @param bool $getPdoThrowException + * @return array + */ + protected function prepare($dbConfigData, $getPdoThrowException = false) + { + /** @var MockObject|Config $config */ + $config = $this->getMockBuilder(Config::class) + ->getMock(); + /** @var MockObject|CapsuleManager $config */ + $dbManager = $this->getMockBuilder(CapsuleManager::class) + ->getMock(); + /** @var MockObject|Connection $connection */ + $connection = $this->getMockBuilder(Connection::class) + ->disableOriginalConstructor() + ->getMock(); + + $app = $this->getApp(['get', 'make', 'instance']); + + $this->setExpects($app, 'get', ['config'], $config); + $this->setExpects($app, 'make', [CapsuleManager::class], $dbManager); + $this->setExpects($config, 'get', ['database'], $dbConfigData, $this->atLeastOnce()); + + $this->setExpects($dbManager, 'setAsGlobal'); + $this->setExpects($dbManager, 'bootEloquent'); + + $connection->expects($this->once()) + ->method('getPdo') + ->willReturnCallback(function () use ($getPdoThrowException) { + if ($getPdoThrowException) { + throw new PDOException(); + } + + return ''; + }); + $this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce()); + + return [$app, $dbManager]; + } }