events: extend system configurations and set invoice formatter (#1995)
This commit is contained in:
@@ -120,12 +120,14 @@ final class ReloadCommand extends Command
|
|||||||
'bin/console cache:warmup --env=' . $environment
|
'bin/console cache:warmup --env=' . $environment
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
$io->success(
|
return $cacheResult;
|
||||||
sprintf('Kimai config was reloaded')
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$io->success(
|
||||||
|
sprintf('Kimai config was reloaded')
|
||||||
|
);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ trait StringAccessibleConfigTrait
|
|||||||
}
|
}
|
||||||
foreach ($temp as $key2) {
|
foreach ($temp as $key2) {
|
||||||
if (!\array_key_exists($key2, $array)) {
|
if (!\array_key_exists($key2, $array)) {
|
||||||
// unknown values will silently be skipped
|
$array[$key2] = $configuration->getValue();
|
||||||
continue 2;
|
continue;
|
||||||
}
|
}
|
||||||
if (\is_array($array[$key2])) {
|
if (\is_array($array[$key2])) {
|
||||||
$array = &$array[$key2];
|
$array = &$array[$key2];
|
||||||
@@ -87,6 +87,13 @@ trait StringAccessibleConfigTrait
|
|||||||
public function find(string $key)
|
public function find(string $key)
|
||||||
{
|
{
|
||||||
$this->prepare();
|
$this->prepare();
|
||||||
|
$key = $this->prepareSearchKey($key);
|
||||||
|
|
||||||
|
return $this->get($key, $this->settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function prepareSearchKey(string $key): string
|
||||||
|
{
|
||||||
$prefix = $this->getPrefix() . '.';
|
$prefix = $this->getPrefix() . '.';
|
||||||
$length = \strlen($prefix);
|
$length = \strlen($prefix);
|
||||||
|
|
||||||
@@ -94,7 +101,7 @@ trait StringAccessibleConfigTrait
|
|||||||
$key = substr($key, $length);
|
$key = substr($key, $length);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->get($key, $this->settings);
|
return $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,7 +115,7 @@ trait StringAccessibleConfigTrait
|
|||||||
$search = array_shift($keys);
|
$search = array_shift($keys);
|
||||||
|
|
||||||
if (!\array_key_exists($search, $config)) {
|
if (!\array_key_exists($search, $config)) {
|
||||||
throw new \InvalidArgumentException('Unknown config: ' . $key);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\is_array($config[$search]) && !empty($keys)) {
|
if (\is_array($config[$search]) && !empty($keys)) {
|
||||||
@@ -118,18 +125,27 @@ trait StringAccessibleConfigTrait
|
|||||||
return $config[$search];
|
return $config[$search];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function has(string $key): bool
|
||||||
|
{
|
||||||
|
$this->prepare();
|
||||||
|
$key = $this->prepareSearchKey($key);
|
||||||
|
|
||||||
|
$keys = explode('.', $key);
|
||||||
|
$search = array_shift($keys);
|
||||||
|
|
||||||
|
if (!\array_key_exists($search, $this->settings)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function offsetExists($offset)
|
public function offsetExists($offset)
|
||||||
{
|
{
|
||||||
try {
|
return $this->has($offset);
|
||||||
$this->find($offset);
|
|
||||||
} catch (\Exception $ex) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -202,6 +202,10 @@ final class SystemConfigurationController extends AbstractController
|
|||||||
|
|
||||||
foreach ($event->getConfigurations() as $configs) {
|
foreach ($event->getConfigurations() as $configs) {
|
||||||
foreach ($configs->getConfiguration() as $config) {
|
foreach ($configs->getConfiguration() as $config) {
|
||||||
|
if (!$this->configurations->has($config->getName())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$configValue = $this->configurations->find($config->getName());
|
$configValue = $this->configurations->find($config->getName());
|
||||||
if (null !== $configValue) {
|
if (null !== $configValue) {
|
||||||
$config->setValue($configValue);
|
$config->setValue($configValue);
|
||||||
|
|||||||
@@ -21,27 +21,20 @@ class SystemConfiguration
|
|||||||
public const SECTION_BRANDING = 'branding';
|
public const SECTION_BRANDING = 'branding';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string|null
|
||||||
*/
|
*/
|
||||||
private $section;
|
private $section;
|
||||||
/**
|
/**
|
||||||
* @var Configuration[]
|
* @var Configuration[]
|
||||||
*/
|
*/
|
||||||
private $configuration;
|
private $configuration = [];
|
||||||
|
|
||||||
/**
|
public function getSection(): ?string
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getSection(): string
|
|
||||||
{
|
{
|
||||||
return $this->section;
|
return $this->section;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function setSection(?string $section): SystemConfiguration
|
||||||
* @param string $section
|
|
||||||
* @return SystemConfiguration
|
|
||||||
*/
|
|
||||||
public function setSection(string $section)
|
|
||||||
{
|
{
|
||||||
$this->section = $section;
|
$this->section = $section;
|
||||||
|
|
||||||
@@ -60,10 +53,17 @@ class SystemConfiguration
|
|||||||
* @param Configuration[] $configuration
|
* @param Configuration[] $configuration
|
||||||
* @return SystemConfiguration
|
* @return SystemConfiguration
|
||||||
*/
|
*/
|
||||||
public function setConfiguration(array $configuration)
|
public function setConfiguration(array $configuration): SystemConfiguration
|
||||||
{
|
{
|
||||||
$this->configuration = $configuration;
|
$this->configuration = $configuration;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addConfiguration(Configuration $configuration): SystemConfiguration
|
||||||
|
{
|
||||||
|
$this->configuration[] = $configuration;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -267,11 +267,18 @@ final class InvoiceModel
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFormatter(): ?InvoiceFormatter
|
public function getFormatter(): InvoiceFormatter
|
||||||
{
|
{
|
||||||
return $this->formatter;
|
return $this->formatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setFormatter(InvoiceFormatter $formatter): InvoiceModel
|
||||||
|
{
|
||||||
|
$this->formatter = $formatter;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function getCurrency(): string
|
public function getCurrency(): string
|
||||||
{
|
{
|
||||||
if (null === $this->getCustomer()) {
|
if (null === $this->getCustomer()) {
|
||||||
|
|||||||
@@ -86,14 +86,13 @@ class FormConfigurationTest extends TestCase
|
|||||||
$this->assertEquals('FR', $sut->find('defaults.customer.country'));
|
$this->assertEquals('FR', $sut->find('defaults.customer.country'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUnknownConfigAreNotImportedAndFindingThemThrowsException()
|
public function testUnknownConfigAreImported()
|
||||||
{
|
{
|
||||||
$this->expectException(\InvalidArgumentException::class);
|
|
||||||
$this->expectExceptionMessage('Unknown config: foobar');
|
|
||||||
|
|
||||||
$sut = $this->getSut($this->getDefaultSettings(), [
|
$sut = $this->getSut($this->getDefaultSettings(), [
|
||||||
(new Configuration())->setName('defaults.customer.foobar')->setValue('hello'),
|
(new Configuration())->setName('defaults.customer.foobar')->setValue('hello'),
|
||||||
]);
|
]);
|
||||||
|
$this->assertTrue($sut->has('customer.foobar'));
|
||||||
|
$this->assertFalse($sut->has('xxxx.foobar'));
|
||||||
$this->assertEquals('hello', $sut->find('customer.foobar'));
|
$this->assertEquals('hello', $sut->find('customer.foobar'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,15 +129,14 @@ class SystemConfigurationTest extends TestCase
|
|||||||
$this->assertEquals(false, $sut->find('timesheet.rules.allow_future_times'));
|
$this->assertEquals(false, $sut->find('timesheet.rules.allow_future_times'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUnknownConfigAreNotImportedAndFindingThemThrowsException()
|
public function testUnknownConfigs()
|
||||||
{
|
{
|
||||||
$this->expectException(\InvalidArgumentException::class);
|
|
||||||
$this->expectExceptionMessage('Unknown config: foo');
|
|
||||||
|
|
||||||
$sut = $this->getSut($this->getDefaultSettings(), [
|
$sut = $this->getSut($this->getDefaultSettings(), [
|
||||||
(new Configuration())->setName('timesheet.foo')->setValue('hello'),
|
(new Configuration())->setName('timesheet.foo')->setValue('hello'),
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('hello', $sut->find('foo'));
|
$this->assertEquals('hello', $sut->find('timesheet.foo'));
|
||||||
|
$this->assertFalse($sut->has('xxxxxxxx.yyyyyyyyy'));
|
||||||
|
$this->assertNull($sut->find('xxxxxxxx.yyyyyyyyy'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCalendarWithoutLoader()
|
public function testCalendarWithoutLoader()
|
||||||
|
|||||||
@@ -116,14 +116,12 @@ class TimesheetConfigurationTest extends TestCase
|
|||||||
$this->assertEquals(false, $sut->find('timesheet.rules.allow_future_times'));
|
$this->assertEquals(false, $sut->find('timesheet.rules.allow_future_times'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUnknownConfigAreNotImportedAndFindingThemThrowsException()
|
public function testUnknownConfigAreImported()
|
||||||
{
|
{
|
||||||
$this->expectException(\InvalidArgumentException::class);
|
|
||||||
$this->expectExceptionMessage('Unknown config: foo');
|
|
||||||
|
|
||||||
$sut = $this->getSut($this->getDefaultSettings(), [
|
$sut = $this->getSut($this->getDefaultSettings(), [
|
||||||
(new Configuration())->setName('timesheet.foo')->setValue('hello'),
|
(new Configuration())->setName('timesheet.foo')->setValue('hello'),
|
||||||
]);
|
]);
|
||||||
|
$this->assertTrue($sut->has('foo'));
|
||||||
$this->assertEquals('hello', $sut->find('foo'));
|
$this->assertEquals('hello', $sut->find('foo'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
48
tests/Form/Model/SystemConfigurationTest.php
Normal file
48
tests/Form/Model/SystemConfigurationTest.php
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<?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\Form\Model;
|
||||||
|
|
||||||
|
use App\Form\Model\Configuration;
|
||||||
|
use App\Form\Model\SystemConfiguration;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers \App\Form\Model\SystemConfiguration
|
||||||
|
*/
|
||||||
|
class SystemConfigurationTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testDefaultValues()
|
||||||
|
{
|
||||||
|
$sut = new SystemConfiguration();
|
||||||
|
self::assertNull($sut->getSection());
|
||||||
|
self::assertEquals([], $sut->getConfiguration());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetterAndGetter()
|
||||||
|
{
|
||||||
|
$sut = new SystemConfiguration();
|
||||||
|
|
||||||
|
self::assertInstanceOf(SystemConfiguration::class, $sut->setSection('foo'));
|
||||||
|
self::assertEquals('foo', $sut->getSection());
|
||||||
|
|
||||||
|
self::assertInstanceOf(SystemConfiguration::class, $sut->setConfiguration([]));
|
||||||
|
self::assertEquals([], $sut->getConfiguration());
|
||||||
|
|
||||||
|
$config = new Configuration();
|
||||||
|
self::assertInstanceOf(SystemConfiguration::class, $sut->setConfiguration([$config]));
|
||||||
|
self::assertEquals([$config], $sut->getConfiguration());
|
||||||
|
|
||||||
|
self::assertInstanceOf(SystemConfiguration::class, $sut->setConfiguration([$config, $config]));
|
||||||
|
self::assertEquals([$config, $config], $sut->getConfiguration());
|
||||||
|
|
||||||
|
self::assertInstanceOf(SystemConfiguration::class, $sut->addConfiguration($config));
|
||||||
|
self::assertEquals([$config, $config, $config], $sut->getConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -41,6 +41,11 @@ class InvoiceModelTest extends TestCase
|
|||||||
self::assertInstanceOf(\DateTime::class, $sut->getInvoiceDate());
|
self::assertInstanceOf(\DateTime::class, $sut->getInvoiceDate());
|
||||||
|
|
||||||
self::assertSame($formatter, $sut->getFormatter());
|
self::assertSame($formatter, $sut->getFormatter());
|
||||||
|
|
||||||
|
$newFormatter = new DebugFormatter();
|
||||||
|
$sut->setFormatter($newFormatter);
|
||||||
|
self::assertNotSame($formatter, $sut->getFormatter());
|
||||||
|
self::assertSame($newFormatter, $sut->getFormatter());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmptyObjectThrowsExceptionOnNumberGenerator()
|
public function testEmptyObjectThrowsExceptionOnNumberGenerator()
|
||||||
|
|||||||
Reference in New Issue
Block a user