/** @test */ public function it_can_set_the_database_data() { $this->repository->setDatabaseData('mysql', ['database' => 'foobar', 'username' => 'foo', 'password' => 'bar']); $this->assertCount(4, $this->repository->getDatabaseData()); $this->assertCount(7, $this->repository->getDatabaseData('mysql')); $this->assertEquals('mysql', $this->repository->getDatabaseDriver()); }
/** * Sets up the database to work with Platform from the given repository. * * @return void */ protected function setupDatabase() { $driver = $this->repository->getDatabaseDriver(); $config = $this->repository->getDatabaseData($driver); try { $this->laravel['db.factory']->make(array_merge(compact('driver'), $config)); } catch (\PDOException $e) { throw $e; } $configFile = __DIR__ . "/stubs/database/{$driver}.php"; $envStub = __DIR__ . '/stubs/env.stub'; // Now, let's update our stub file with // our actual database credentials $contents = str_replace(array_map(function ($key) { return '{{' . $key . '}}'; }, array_keys($config)), array_values($config), $this->laravel['files']->get($envStub)); // Generate a key $contents = str_replace('{{key}}', Str::random(32), $contents); // Just a triple check we can actually // write the configuration. if ($this->laravel['files']->put($envFile = $this->laravel['path.base'] . '/.env', $contents) === false) { throw new \RuntimeException("Could not write env file to [{$envFile}]."); } if ($this->laravel['files']->put($newConfigFile = $this->laravel['path.base'] . '/config/database.php', $this->laravel['files']->get($configFile)) === false) { throw new \RuntimeException("Could not write database config file to [{$newConfigFile}]."); } // Reload env file $this->laravel['Illuminate\\Foundation\\Bootstrap\\DetectEnvironment']->bootstrap($this->laravel); // Reload config $this->laravel['Illuminate\\Foundation\\Bootstrap\\LoadConfiguration']->bootstrap($this->laravel); // Purge old connection $this->laravel['db']->purge(); // Set table prefix on the connection $this->laravel['db']->setTablePrefix(array_get($config, 'prefix')); // Reconnect using the new config $this->laravel['db']->reconnect($driver); }