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
|
|
|
|
2018-09-15 17:24:59 +02:00
|
|
|
use Engelsystem\Application;
|
2017-10-31 13:40:13 +01:00
|
|
|
use Engelsystem\Config\Config;
|
2018-09-16 00:58:25 +02:00
|
|
|
use Engelsystem\Database\Database;
|
2017-10-31 13:40:13 +01:00
|
|
|
use Engelsystem\Database\DatabaseServiceProvider;
|
2018-09-16 00:58:25 +02:00
|
|
|
use Engelsystem\Database\Db;
|
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;
|
2018-09-15 17:24:59 +02:00
|
|
|
use PDO;
|
2018-01-15 23:31:19 +01:00
|
|
|
use PDOException;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\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()
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegister(): void
|
2017-10-31 13:40:13 +01:00
|
|
|
{
|
2018-09-16 00:58:25 +02:00
|
|
|
/** @var Application|MockObject $app */
|
|
|
|
/** @var CapsuleManager|MockObject $dbManager */
|
|
|
|
/** @var PDO|MockObject $pdo */
|
|
|
|
/** @var Database|MockObject $database */
|
|
|
|
/** @var Connection|MockObject $connection */
|
|
|
|
list($app, $dbManager, $pdo, $database, $connection) = $this->prepare(
|
|
|
|
[
|
|
|
|
'driver' => 'sqlite',
|
2019-04-24 11:01:37 +02:00
|
|
|
'database' => ':memory:',
|
2018-09-16 00:58:25 +02:00
|
|
|
]
|
|
|
|
);
|
2017-10-31 13:40:13 +01:00
|
|
|
|
2018-09-16 00:58:25 +02:00
|
|
|
$app->expects($this->exactly(7))
|
|
|
|
->method('instance')
|
|
|
|
->withConsecutive(
|
|
|
|
[CapsuleManager::class, $dbManager],
|
|
|
|
[Db::class, $dbManager],
|
|
|
|
[Connection::class, $connection],
|
|
|
|
[Database::class, $database],
|
|
|
|
['db', $database],
|
|
|
|
['db.pdo', $pdo],
|
|
|
|
['db.connection', $connection]
|
|
|
|
);
|
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::exitOnError()
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Database\DatabaseServiceProvider::register()
|
2018-01-15 23:31:19 +01:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRegisterError(): void
|
2018-01-15 23:31:19 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function prepare(array $dbConfigData, bool $getPdoThrowException = false): array
|
2018-01-15 23:31:19 +01:00
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Config|MockObject $config */
|
2018-01-15 23:31:19 +01:00
|
|
|
$config = $this->getMockBuilder(Config::class)
|
|
|
|
->getMock();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var CapsuleManager|MockObject $config */
|
2018-01-15 23:31:19 +01:00
|
|
|
$dbManager = $this->getMockBuilder(CapsuleManager::class)
|
|
|
|
->getMock();
|
2019-04-24 10:45:00 +02:00
|
|
|
/** @var Connection|MockObject $connection */
|
2018-01-15 23:31:19 +01:00
|
|
|
$connection = $this->getMockBuilder(Connection::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2018-09-16 00:58:25 +02:00
|
|
|
/** @var PDO|MockObject $pdo */
|
|
|
|
$pdo = $this->getMockBuilder(PDO::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
/** @var Database|MockObject $database */
|
|
|
|
$database = $this->getMockBuilder(Database::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2018-01-15 23:31:19 +01:00
|
|
|
|
|
|
|
$app = $this->getApp(['get', 'make', 'instance']);
|
|
|
|
|
|
|
|
$this->setExpects($app, 'get', ['config'], $config);
|
2019-10-13 21:06:24 +02:00
|
|
|
$config->expects($this->exactly(2))
|
|
|
|
->method('get')
|
|
|
|
->withConsecutive(['timezone'], ['database'])
|
|
|
|
->willReturnOnConsecutiveCalls('UTC', $dbConfigData);
|
2018-01-15 23:31:19 +01:00
|
|
|
|
2018-09-16 00:58:25 +02:00
|
|
|
$app->expects($this->atLeastOnce())
|
|
|
|
->method('make')
|
|
|
|
->withConsecutive(
|
|
|
|
[CapsuleManager::class],
|
|
|
|
[Database::class]
|
|
|
|
)
|
|
|
|
->willReturn(
|
|
|
|
$dbManager,
|
|
|
|
$database
|
|
|
|
);
|
|
|
|
|
2018-01-15 23:31:19 +01:00
|
|
|
$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')
|
2018-09-15 17:24:59 +02:00
|
|
|
->willReturnCallback(function () use ($getPdoThrowException, $pdo) {
|
2018-01-15 23:31:19 +01:00
|
|
|
if ($getPdoThrowException) {
|
|
|
|
throw new PDOException();
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:24:59 +02:00
|
|
|
return $pdo;
|
2018-01-15 23:31:19 +01:00
|
|
|
});
|
|
|
|
$this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce());
|
|
|
|
|
2018-09-16 00:58:25 +02:00
|
|
|
return [$app, $dbManager, $pdo, $database, $connection];
|
2018-01-15 23:31:19 +01:00
|
|
|
}
|
2017-10-31 13:40:13 +01:00
|
|
|
}
|