2017-10-31 13:40:13 +01:00
|
|
|
<?php
|
|
|
|
|
2017-10-31 14:23:23 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Database;
|
2017-10-31 13:40:13 +01:00
|
|
|
|
|
|
|
use Engelsystem\Config\Config;
|
|
|
|
use Engelsystem\Database\DatabaseServiceProvider;
|
2017-10-31 14:15:19 +01:00
|
|
|
use Engelsystem\Test\Unit\ServiceProviderTest;
|
2017-10-31 13:40:13 +01:00
|
|
|
use Exception;
|
2018-01-15 23:31:19 +01:00
|
|
|
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
|
|
|
use Illuminate\Database\Connection;
|
|
|
|
use PDOException;
|
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2017-10-31 14:15:19 +01:00
|
|
|
class DatabaseServiceProviderTest extends ServiceProviderTest
|
2017-10-31 13:40:13 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\DatabaseServiceProvider::register()
|
|
|
|
*/
|
|
|
|
public function testRegister()
|
|
|
|
{
|
2018-01-15 23:31:19 +01:00
|
|
|
list($app, $dbManager) = $this->prepare(['driver' => 'sqlite', 'database' => ':memory:']);
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2018-01-15 23:31:19 +01:00
|
|
|
$this->setExpects($app, 'instance', ['db', $dbManager]);
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2018-01-15 23:31:19 +01:00
|
|
|
$serviceProvider = new DatabaseServiceProvider($app);
|
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Database\DatabaseServiceProvider::register()
|
|
|
|
* @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError()
|
|
|
|
*/
|
|
|
|
public function testRegisterError()
|
|
|
|
{
|
|
|
|
list($app) = $this->prepare([
|
2018-01-14 01:48:50 +01:00
|
|
|
'host' => 'localhost',
|
|
|
|
'database' => 'database',
|
|
|
|
'username' => 'user',
|
|
|
|
'password' => 'password',
|
2018-01-15 23:31:19 +01:00
|
|
|
], true);
|
|
|
|
|
2017-10-31 13:40:13 +01:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
|
2017-10-31 14:15:19 +01:00
|
|
|
$serviceProvider = new DatabaseServiceProvider($app);
|
2017-10-31 13:40:13 +01:00
|
|
|
$serviceProvider->register();
|
|
|
|
}
|
2018-01-15 23:31:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
|
2018-01-16 21:26:59 +01:00
|
|
|
$this->setExpects($connection, 'useDefaultSchemaGrammar');
|
2018-01-15 23:31:19 +01:00
|
|
|
$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];
|
|
|
|
}
|
2017-10-31 13:40:13 +01:00
|
|
|
}
|