Skip to content

Commit

Permalink
Configuration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Aug 9, 2019
1 parent 91e1c87 commit 186023a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Aire.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
*/
class Aire
{
/**
* @var array
*/
protected static $default_theme_config;

/**
* Global store of element IDs
*
Expand Down Expand Up @@ -108,12 +113,28 @@ public function __construct(Factory $view_factory, Store $session_store, Closure
$this->form_resolver = $form_resolver;
$this->user_config = $config;

$default_theme_config = require dirname(__DIR__).'/config/default-theme.php';
$this->setTheme('aire', null, $default_theme_config);
$this->setTheme('aire', null, static::getDefaultThemeConfig());

$this->registerClasses();
}

/**
* Get the default Aire theme config.
*
* This is mostly for theme authors who wish to merge the defaults
* into their theme config instead of provided all new class names.
*
* @return array
*/
public static function getDefaultThemeConfig() : array
{
if (null === static::$default_theme_config) {
static::$default_theme_config = require dirname(__DIR__).'/config/default-theme.php';
}

return static::$default_theme_config;
}

/**
* Set where Aire looks for view files + any config overrides
*
Expand Down
63 changes: 63 additions & 0 deletions tests/Unit/ThemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Galahad\Aire\Aire;
use Galahad\Aire\Tests\TestCase;
use Illuminate\Config\Repository;
use Illuminate\Support\Arr;
use Illuminate\View\Factory;
use Illuminate\View\View;
use Mockery;
Expand Down Expand Up @@ -39,4 +41,65 @@ public function test_a_theme_can_be_set() : void

$this->assertEquals('hello world', $result);
}

public function test_themes_can_override_defaults_but_not_user_config() : void
{
$config_repo = $this->app->make(Repository::class);
$default_theme = Aire::getDefaultThemeConfig();

// Set some "user" configuration that should take precedence over themes
$config_repo->set('aire.default_classes.group', '__test__');
$config_repo->set('aire.validation_classes.none.input', '__test__');
$config_repo->set('aire.validation_classes.valid.input', '__test__');
$config_repo->set('aire.validation_classes.invalid.input', '__test__');

// Clear the singleton so Aire will instantiate with the updated config
$this->app->forgetInstance('galahad.aire');

// Make sure our "user" configuration is set
$this->assertEquals('__test__', $this->aire()->config('default_classes.group'));
$this->assertEquals('__test__', $this->aire()->config('validation_classes.none.input'));
$this->assertEquals('__test__', $this->aire()->config('validation_classes.valid.input'));
$this->assertEquals('__test__', $this->aire()->config('validation_classes.invalid.input'));

// Other values should match the default config
$this->assertEquals(Arr::get($default_theme, 'default_classes.label'), $this->aire()->config('default_classes.label'));
$this->assertEquals(Arr::get($default_theme, 'validation_classes.none.select'), $this->aire()->config('validation_classes.none.select'));
$this->assertEquals(Arr::get($default_theme, 'validation_classes.valid.select'), $this->aire()->config('validation_classes.valid.select'));
$this->assertEquals(Arr::get($default_theme, 'validation_classes.invalid.select'), $this->aire()->config('validation_classes.invalid.select'));

// Now set a new custom theme
$this->aire()->setTheme('aire', null, [
'default_classes' => [
'group' => '__test2__',
'checkbox' => '__test2__',
],
'validation_classes' => [
'none' => [
'input' => '__test2__',
'group_errors' => '__test2__',
],
'valid' => [
'input' => '__test2__',
'group_errors' => '__test2__',
],
'invalid' => [
'input' => '__test2__',
'group_errors' => '__test2__',
],
],
]);

// Values set by the user should remain the same
$this->assertEquals('__test__', $this->aire()->config('default_classes.group'));
$this->assertEquals('__test__', $this->aire()->config('validation_classes.none.input'));
$this->assertEquals('__test__', $this->aire()->config('validation_classes.valid.input'));
$this->assertEquals('__test__', $this->aire()->config('validation_classes.invalid.input'));

// Values set in the theme should be applied
$this->assertEquals('__test2__', $this->aire()->config('default_classes.checkbox'));
$this->assertEquals('__test2__', $this->aire()->config('validation_classes.none.group_errors'));
$this->assertEquals('__test2__', $this->aire()->config('validation_classes.valid.group_errors'));
$this->assertEquals('__test2__', $this->aire()->config('validation_classes.invalid.group_errors'));
}
}

0 comments on commit 186023a

Please sign in to comment.