prepare( [ 'driver' => 'sqlite', 'database' => ':memory:', ] ); $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] ); $serviceProvider = new DatabaseServiceProvider($app); $serviceProvider->register(); } /** * @covers \Engelsystem\Database\DatabaseServiceProvider::exitOnError() * @covers \Engelsystem\Database\DatabaseServiceProvider::register() */ public function testRegisterError(): void { list($app) = $this->prepare([ 'host' => 'localhost', 'database' => 'database', 'username' => 'user', 'password' => 'password', ], true); $this->expectException(Exception::class); $serviceProvider = new DatabaseServiceProvider($app); $serviceProvider->register(); } /** * Prepare some mocks * * @param array $dbConfigData * @return array */ protected function prepare(array $dbConfigData, bool $getPdoThrowException = false): array { /** @var Config|MockObject $config */ $config = $this->getMockBuilder(Config::class) ->getMock(); /** @var CapsuleManager|MockObject $config */ $dbManager = $this->getMockBuilder(CapsuleManager::class) ->getMock(); /** @var Connection|MockObject $connection */ $connection = $this->getMockBuilder(Connection::class) ->disableOriginalConstructor() ->getMock(); /** @var PDO|MockObject $pdo */ $pdo = $this->getMockBuilder(PDO::class) ->disableOriginalConstructor() ->getMock(); /** @var Database|MockObject $database */ $database = $this->getMockBuilder(Database::class) ->disableOriginalConstructor() ->getMock(); $app = $this->getApp(['get', 'make', 'instance']); $this->setExpects($app, 'get', ['config'], $config); $config->expects($this->exactly(2)) ->method('get') ->withConsecutive(['timezone'], ['database']) ->willReturnOnConsecutiveCalls('UTC', $dbConfigData); $app->expects($this->atLeastOnce()) ->method('make') ->withConsecutive( [CapsuleManager::class], [Database::class] ) ->willReturn( $dbManager, $database ); $this->setExpects($dbManager, 'setAsGlobal'); $this->setExpects($dbManager, 'bootEloquent'); $this->setExpects($connection, 'useDefaultSchemaGrammar'); $connection->expects($this->once()) ->method('getPdo') ->willReturnCallback(function () use ($getPdoThrowException, $pdo) { if ($getPdoThrowException) { throw new PDOException(); } return $pdo; }); $this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce()); return [$app, $dbManager, $pdo, $database, $connection]; } }