added database driven system configurations with admin screen (#647)

This commit is contained in:
Kevin Papst
2019-03-22 20:58:38 +01:00
committed by GitHub
parent e990ae4800
commit 13d9f8f3f7
142 changed files with 2262 additions and 64727 deletions

View File

@@ -0,0 +1,100 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Configuration;
use App\Configuration\FormConfiguration;
use App\Entity\Configuration;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Configuration\FormConfiguration
* @covers \App\Configuration\StringAccessibleConfigTrait
*/
class FormConfigurationTest extends TestCase
{
protected function getSut(array $settings, array $loaderSettings = [])
{
$loader = new TestConfigLoader($loaderSettings);
return new FormConfiguration($loader, $settings);
}
protected function getDefaultSettings()
{
return [
'customer' => [
'timezone' => 'Europe/London',
'currency' => 'GBP',
'country' => 'FR',
],
];
}
protected function getDefaultLoaderSettings()
{
return [
(new Configuration())->setName('defaults.customer.timezone')->setValue('Russia/Moscov'),
(new Configuration())->setName('defaults.customer.currency')->setValue('USD'),
(new Configuration())->setName('defaults.customer.country')->setValue('RU'),
];
}
public function testPrefix()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('defaults', $sut->getPrefix());
}
public function testDefaultWithoutLoader()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('Europe/London', $sut->getCustomerDefaultTimezone());
$this->assertEquals('GBP', $sut->getCustomerDefaultCurrency());
$this->assertEquals('FR', $sut->getCustomerDefaultCountry());
}
public function testDefaultWithLoader()
{
$sut = $this->getSut($this->getDefaultSettings(), $this->getDefaultLoaderSettings());
$this->assertEquals('Russia/Moscov', $sut->getCustomerDefaultTimezone());
$this->assertEquals('USD', $sut->getCustomerDefaultCurrency());
$this->assertEquals('RU', $sut->getCustomerDefaultCountry());
}
public function testDefaultWithMixedConfigs()
{
$sut = $this->getSut($this->getDefaultSettings(), [
(new Configuration())->setName('defaults.customer.country')->setValue('RU'),
(new Configuration())->setName('defaults.customer.foobar')->setValue('hello'),
]);
$this->assertEquals('Europe/London', $sut->getCustomerDefaultTimezone());
$this->assertEquals('GBP', $sut->getCustomerDefaultCurrency());
$this->assertEquals('RU', $sut->getCustomerDefaultCountry());
}
public function testFindByKey()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('FR', $sut->find('customer.country'));
$this->assertEquals('FR', $sut->find('defaults.customer.country'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unknown config: foobar
*/
public function testUnknownConfigAreNotImportedAndFindingThemThrowsException()
{
$sut = $this->getSut($this->getDefaultSettings(), [
(new Configuration())->setName('defaults.customer.foobar')->setValue('hello'),
]);
$this->assertEquals('hello', $sut->find('customer.foobar'));
}
}

View File

@@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Configuration;
use App\Configuration\SystemConfiguration;
use App\Entity\Configuration;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Configuration\SystemConfiguration
* @covers \App\Configuration\StringAccessibleConfigTrait
*/
class SystemConfigurationTest extends TestCase
{
/**
* @param array $settings
* @param array $loaderSettings
* @return SystemConfiguration
*/
protected function getSut(array $settings, array $loaderSettings = [])
{
$loader = new TestConfigLoader($loaderSettings);
return new SystemConfiguration($loader, $settings);
}
protected function getDefaultSettings()
{
return [
'timesheet' => [
'rules' => [
'allow_future_times' => false,
],
'duration_only' => true,
'markdown_content' => false,
'active_entries' => [
'hard_limit' => 99,
'soft_limit' => 15,
],
],
'defaults' => [
'customer' => [
'timezone' => 'Europe/London',
'currency' => 'GBP',
'country' => 'FR',
],
],
];
}
protected function getDefaultLoaderSettings()
{
return [
(new Configuration())->setName('defaults.customer.timezone')->setValue('Russia/Moscov'),
(new Configuration())->setName('defaults.customer.currency')->setValue('RUB'),
(new Configuration())->setName('timesheet.rules.allow_future_times')->setValue('1'),
(new Configuration())->setName('timesheet.duration_only')->setValue('0'),
(new Configuration())->setName('timesheet.markdown_content')->setValue('1'),
(new Configuration())->setName('timesheet.active_entries.hard_limit')->setValue('7'),
(new Configuration())->setName('timesheet.active_entries.soft_limit')->setValue('3'),
];
}
public function testPrefix()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('kimai', $sut->getPrefix());
}
public function testDefaultWithoutLoader()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('Europe/London', $sut->find('defaults.customer.timezone'));
$this->assertEquals('GBP', $sut->find('defaults.customer.currency'));
$this->assertEquals(false, $sut->find('timesheet.rules.allow_future_times'));
$this->assertEquals(99, $sut->find('timesheet.active_entries.hard_limit'));
}
public function testDefaultWithLoader()
{
$sut = $this->getSut($this->getDefaultSettings(), $this->getDefaultLoaderSettings());
$this->assertEquals('Russia/Moscov', $sut->find('defaults.customer.timezone'));
$this->assertEquals('RUB', $sut->find('defaults.customer.currency'));
$this->assertEquals(true, $sut->find('timesheet.rules.allow_future_times'));
$this->assertEquals(7, $sut->find('timesheet.active_entries.hard_limit'));
}
public function testDefaultWithMixedConfigs()
{
$sut = $this->getSut($this->getDefaultSettings(), [
(new Configuration())->setName('timesheet.duration_only')->setValue(''),
]);
$this->assertEquals(false, $sut->find('timesheet.duration_only'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unknown config: foo
*/
public function testUnknownConfigAreNotImportedAndFindingThemThrowsException()
{
$sut = $this->getSut($this->getDefaultSettings(), [
(new Configuration())->setName('timesheet.foo')->setValue('hello'),
]);
$this->assertEquals('hello', $sut->find('foo'));
}
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Configuration;
use App\Configuration\ConfigLoaderInterface;
use App\Entity\Configuration;
/**
* @covers \App\Configuration\FormConfiguration
*/
class TestConfigLoader implements ConfigLoaderInterface
{
private $configs = [];
public function __construct(array $configs)
{
$this->configs = $configs;
}
/**
* @param null|string $prefix
* @return Configuration[]
*/
public function getConfiguration(?string $prefix = null): array
{
return $this->configs;
}
}

View File

@@ -0,0 +1,112 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Configuration;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Configuration;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Configuration\TimesheetConfiguration
* @covers \App\Configuration\StringAccessibleConfigTrait
*/
class TimesheetConfigurationTest extends TestCase
{
/**
* @param array $settings
* @param array $loaderSettings
* @return TimesheetConfiguration
*/
protected function getSut(array $settings, array $loaderSettings = [])
{
$loader = new TestConfigLoader($loaderSettings);
return new TimesheetConfiguration($loader, $settings);
}
protected function getDefaultSettings()
{
return [
'rules' => [
'allow_future_times' => false,
],
'duration_only' => true,
'markdown_content' => false,
'active_entries' => [
'hard_limit' => 99,
'soft_limit' => 15,
],
];
}
protected function getDefaultLoaderSettings()
{
return [
(new Configuration())->setName('timesheet.rules.allow_future_times')->setValue('1'),
(new Configuration())->setName('timesheet.duration_only')->setValue('0'),
(new Configuration())->setName('timesheet.markdown_content')->setValue('1'),
(new Configuration())->setName('timesheet.active_entries.hard_limit')->setValue('7'),
(new Configuration())->setName('timesheet.active_entries.soft_limit')->setValue('3'),
];
}
public function testPrefix()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('timesheet', $sut->getPrefix());
}
public function testDefaultWithoutLoader()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals(99, $sut->getActiveEntriesHardLimit());
$this->assertEquals(15, $sut->getActiveEntriesSoftLimit());
$this->assertEquals(false, $sut->isAllowFutureTimes());
$this->assertEquals(true, $sut->isDurationOnly());
$this->assertEquals(false, $sut->isMarkdownEnabled());
}
public function testDefaultWithLoader()
{
$sut = $this->getSut($this->getDefaultSettings(), $this->getDefaultLoaderSettings());
$this->assertEquals(7, $sut->getActiveEntriesHardLimit());
$this->assertEquals(3, $sut->getActiveEntriesSoftLimit());
$this->assertEquals(true, $sut->isAllowFutureTimes());
$this->assertEquals(false, $sut->isDurationOnly());
$this->assertEquals(true, $sut->isMarkdownEnabled());
}
public function testDefaultWithMixedConfigs()
{
$sut = $this->getSut($this->getDefaultSettings(), [
(new Configuration())->setName('timesheet.duration_only')->setValue(''),
]);
$this->assertEquals(false, $sut->isDurationOnly());
}
public function testFindByKey()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals(false, $sut->find('rules.allow_future_times'));
$this->assertEquals(false, $sut->find('timesheet.rules.allow_future_times'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unknown config: foo
*/
public function testUnknownConfigAreNotImportedAndFindingThemThrowsException()
{
$sut = $this->getSut($this->getDefaultSettings(), [
(new Configuration())->setName('timesheet.foo')->setValue('hello'),
]);
$this->assertEquals('hello', $sut->find('foo'));
}
}