Skip to content

Commit

Permalink
config: allow renaming of config.default.php to config.php
Browse files Browse the repository at this point in the history
Closes #444 (Problems after installation)
  • Loading branch information
MyIgel authored and msquare committed Aug 12, 2018
1 parent f46e921 commit d243090
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/Config/ConfigServiceProvider.php
Expand Up @@ -3,24 +3,33 @@
namespace Engelsystem\Config;

use Engelsystem\Container\ServiceProvider;
use Exception;

class ConfigServiceProvider extends ServiceProvider
{
/** @var array */
protected $configFiles = ['config.default.php', 'config.php'];

public function register()
{
$defaultConfigFile = config_path('config.default.php');
$configFile = config_path('config.php');

$config = $this->app->make(Config::class);
$this->app->instance('config', $config);

$config->set(require $defaultConfigFile);
foreach ($this->configFiles as $file) {
$file = config_path($file);

if (!file_exists($file)) {
continue;
}

if (file_exists($configFile)) {
$config->set(array_replace_recursive(
$config->get(null),
require $configFile
require $file
));
}

if (empty($config->get(null))) {
throw new Exception('Configuration not found');
}
}
}
33 changes: 31 additions & 2 deletions tests/Unit/Config/ConfigServiceProviderTest.php
Expand Up @@ -6,6 +6,7 @@
use Engelsystem\Config\Config;
use Engelsystem\Config\ConfigServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
use Exception;
use PHPUnit_Framework_MockObject_MockObject;

class ConfigServiceProviderTest extends ServiceProviderTest
Expand All @@ -27,12 +28,15 @@ public function testRegister()
$this->setExpects($app, 'get', ['path.config'], __DIR__ . '/../../../config', $this->atLeastOnce());

$this->setExpects($config, 'set', null, null, $this->exactly(2));
$this->setExpects($config, 'get', [null], []);
$config->expects($this->exactly(3))
->method('get')
->with(null)
->willReturnOnConsecutiveCalls([], [], ['lor' => 'em']);

$configFile = __DIR__ . '/../../../config/config.php';
$configExists = file_exists($configFile);
if (!$configExists) {
file_put_contents($configFile, '<?php return [];');
file_put_contents($configFile, '<?php return ["lor"=>"em"];');
}

$serviceProvider = new ConfigServiceProvider($app);
Expand All @@ -42,4 +46,29 @@ public function testRegister()
unlink($configFile);
}
}

/**
* @covers \Engelsystem\Config\ConfigServiceProvider::register()
*/
public function testRegisterException()
{
/** @var PHPUnit_Framework_MockObject_MockObject|Config $config */
$config = $this->getMockBuilder(Config::class)
->getMock();

$app = $this->getApp(['make', 'instance', 'get']);
Application::setInstance($app);

$this->setExpects($app, 'make', [Config::class], $config);
$this->setExpects($app, 'instance', ['config', $config]);
$this->setExpects($app, 'get', ['path.config'], __DIR__ . '/not_existing', $this->atLeastOnce());

$this->setExpects($config, 'set', null, null, $this->never());
$this->setExpects($config, 'get', [null], []);

$this->expectException(Exception::class);

$serviceProvider = new ConfigServiceProvider($app);
$serviceProvider->register();
}
}

0 comments on commit d243090

Please sign in to comment.