refactor configurations (#2626)

* composition instead of inheritance
* refactored some twig extensions to runtime extensions
This commit is contained in:
Kevin Papst
2021-06-26 13:03:54 +02:00
committed by GitHub
parent 3d1fba650b
commit 6d196879b4
22 changed files with 255 additions and 358 deletions

View File

@@ -98,7 +98,15 @@ class SystemConfigurationTest extends TestCase
],
'theme' => [
'color_choices' => 'Maroon|#800000,Brown|#a52a2a,Red|#ff0000,Orange|#ffa500,#ffffff,,|#000000',
'colors_limited' => true
'colors_limited' => true,
'tags_create' => true,
'branding' => [
'logo' => null,
'mini' => null,
'company' => 'Acme Corp.',
'title' => 'Fantastic Time-Tracking',
'translation' => null,
],
],
];
}
@@ -137,6 +145,9 @@ class SystemConfigurationTest extends TestCase
$this->assertTrue($sut->find('theme.colors_limited'));
$this->assertTrue($sut->isThemeColorsLimited());
$this->assertEquals('Maroon|#800000,Brown|#a52a2a,Red|#ff0000,Orange|#ffa500,#ffffff,,|#000000', $sut->getThemeColorChoices());
$this->assertEquals('Acme Corp.', $sut->getBrandingCompany());
$this->assertEquals('Fantastic Time-Tracking', $sut->getBrandingTitle());
$this->assertTrue($sut->isAllowTagCreation());
}
public function testDefaultWithLoader()

View File

@@ -9,20 +9,22 @@
namespace App\Tests\Configuration;
use App\Configuration\SystemConfiguration;
use App\Configuration\ThemeConfiguration;
use PHPUnit\Framework\TestCase;
/**
* @covers \App\Configuration\ThemeConfiguration
* @covers \App\Configuration\StringAccessibleConfigTrait
* @covers \App\Configuration\SystemConfiguration
*/
class ThemeConfigurationTest extends TestCase
{
protected function getSut(array $settings, array $loaderSettings = []): ThemeConfiguration
{
$loader = new TestConfigLoader($loaderSettings);
$config = new SystemConfiguration($loader, ['theme' => $settings]);
return new ThemeConfiguration($loader, $settings);
return new ThemeConfiguration($config);
}
/**
@@ -51,26 +53,13 @@ class ThemeConfigurationTest extends TestCase
];
}
public function testPrefix()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('theme', $sut->getPrefix());
}
public function testConfigs()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertTrue($sut->isAllowTagCreation());
$this->assertNull($sut->getTitle());
}
/**
* @group legacy
*/
public function testDeprecations()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
$this->assertEquals('', $sut->getSelectPicker());
$this->assertFalse($sut->isAutoReloadDatatable());
$this->assertTrue($sut->isAllowTagCreation());
$this->assertNull($sut->getTitle());
}
}

View File

@@ -48,7 +48,7 @@ class CalendarControllerTest extends ControllerBaseTest
$client = $this->getClientForAuthenticatedUser();
static::$kernel->getContainer()->set(SystemConfiguration::class, $config);
$this->request($client, '/calendar/');
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertSuccessResponse($client);
$crawler = $client->getCrawler();
$calendar = $crawler->filter('div#timesheet_calendar');
@@ -61,9 +61,35 @@ class CalendarControllerTest extends ControllerBaseTest
$this->assertStringContainsString("name: 'holidays_en'", $content);
}
protected function getDefaultSettings()
protected function getDefaultSettings(): array
{
return [
'theme' => [
'active_warning' => 3,
'box_color' => 'blue',
'select_type' => 'selectpicker',
'show_about' => true,
'chart' => [
'background_color' => '#3c8dbc',
'border_color' => '#3b8bba',
'grid_color' => 'rgba(0,0,0,.05)',
'height' => '200',
],
'branding' => [
'logo' => null,
'mini' => null,
'company' => null,
'title' => null,
'translation' => null,
],
'autocomplete_chars' => 3,
'tags_create' => true,
'calendar' => [
'background_color' => '#d2d6de'
],
'colors_limited' => true,
'color_choices' => 'Silver|#c0c0c0,Gray|#808080,Black|#000000,Maroon|#800000,Brown|#a52a2a,Red|#ff0000,Orange|#ffa500,Gold|#ffd700,Yellow|#ffff00,Peach|#ffdab9,Khaki|#f0e68c,Olive|#808000,Lime|#00ff00,Jelly|#9acd32,Green|#008000,Teal|#008080,Aqua|#00ffff,LightBlue|#add8e6,DeepSky|#00bfff,Dodger|#1e90ff,Blue|#0000ff,Navy|#000080,Purple|#800080,Fuchsia|#ff00ff,Violet|#ee82ee,Rose|#ffe4e1,Lavender|#E6E6FA'
],
'defaults' => [
'user' => [
'language' => 'en'

View File

@@ -18,6 +18,7 @@ use App\Tests\KernelTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Test\Constraint as ResponseConstraint;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
/**
@@ -161,6 +162,12 @@ abstract class ControllerBaseTest extends WebTestCase
);
}
protected function assertSuccessResponse(HttpKernelBrowser $client, string $message = '')
{
$response = $client->getResponse();
self::assertThat($response, new ResponseConstraint\ResponseIsSuccessful(), 'Response is not successful, got code: ' . $response->getStatusCode());
}
/**
* @param string $url
* @param string $method
@@ -381,26 +388,26 @@ abstract class ControllerBaseTest extends WebTestCase
* @param HttpKernelBrowser $client
* @param string $url
*/
protected function assertIsRedirect(HttpKernelBrowser $client, $url = null, $endsWith = true)
protected function assertIsRedirect(HttpKernelBrowser $client, $url = null)
{
self::assertTrue($client->getResponse()->isRedirect(), 'Response is not a redirect');
self::assertResponseRedirects();
if (null === $url) {
return;
}
$this->assertRedirectUrl($client, $url);
}
protected function assertRedirectUrl(HttpKernelBrowser $client, $url = null, $endsWith = true)
{
self::assertTrue($client->getResponse()->headers->has('Location'), 'Could not find "Location" header');
$location = $client->getResponse()->headers->get('Location');
if ($endsWith) {
self::assertStringEndsWith(
$url,
$client->getResponse()->headers->get('Location'),
'Redirect URL does not match'
);
self::assertStringEndsWith($url, $location, 'Redirect URL does not match');
} else {
self::assertStringContainsString(
$url,
$client->getResponse()->headers->get('Location'),
'Redirect URL does not match'
);
self::assertStringContainsString($url, $location, 'Redirect URL does not match');
}
}

View File

@@ -192,7 +192,8 @@ class InvoiceControllerTest extends ControllerBaseTest
$action = '/invoice/save-invoice/1/' . $template->getId() . '?' . http_build_query($urlParams);
$this->request($client, $action);
$this->assertIsRedirect($client, '/invoice/show?id=', false);
$this->assertIsRedirect($client);
$this->assertRedirectUrl($client, '/invoice/show?id=', false);
$client->followRedirect();
$this->assertDataTableRowCount($client, 'datatable_invoices', 1);

View File

@@ -177,8 +177,6 @@ class AppExtensionTest extends TestCase
'colors_limited' => true,
'color_choices' => 'Silver|#c0c0c0,Gray|#808080,Black|#000000,Maroon|#800000,Brown|#a52a2a,Red|#ff0000,Orange|#ffa500,Gold|#ffd700,Yellow|#ffff00,Peach|#ffdab9,Khaki|#f0e68c,Olive|#808000,Lime|#00ff00,Jelly|#9acd32,Green|#008000,Teal|#008080,Aqua|#00ffff,LightBlue|#add8e6,DeepSky|#00bfff,Dodger|#1e90ff,Blue|#0000ff,Navy|#000080,Purple|#800080,Fuchsia|#ff00ff,Violet|#ee82ee,Rose|#ffe4e1,Lavender|#E6E6FA'
],
'kimai.theme.select_type' => 'selectpicker',
'kimai.theme.show_about' => true,
'kimai.timesheet' => [
'mode' => 'default',
'markdown_content' => false,

View File

@@ -24,8 +24,7 @@ class KimaiMailerTest extends TestCase
{
public function getSut(): KimaiMailer
{
$config = $this->createMock(MailConfiguration::class);
$config->expects($this->any())->method('getFromAddress')->willReturn('zippel@example.com');
$config = new MailConfiguration('zippel@example.com');
$mailer = $this->createMock(MailerInterface::class);

View File

@@ -1,82 +0,0 @@
<?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\Twig;
use App\Configuration\SystemConfiguration;
use App\Tests\Configuration\TestConfigLoader;
use App\Twig\ConfigExtension;
use PHPUnit\Framework\TestCase;
use Twig\TwigFunction;
/**
* @covers \App\Twig\ConfigExtension
*/
class ConfigExtensionTest extends TestCase
{
protected function getSut(array $settings, array $loaderSettings = []): ConfigExtension
{
$loader = new TestConfigLoader($loaderSettings);
$config = new SystemConfiguration($loader, ['theme' => $settings]);
return new ConfigExtension($config);
}
public function testGetFunctions()
{
$functions = ['theme_config'];
$sut = $this->getSut([], []);
$twigFunctions = $sut->getFunctions();
self::assertCount(\count($functions), $twigFunctions);
$i = 0;
/** @var TwigFunction $filter */
foreach ($twigFunctions as $filter) {
self::assertInstanceOf(TwigFunction::class, $filter);
self::assertEquals($functions[$i++], $filter->getName());
}
}
private function getDefaultSettings(): array
{
return [
'active_warning' => 3,
'box_color' => 'green',
'select_type' => null,
'show_about' => true,
'chart' => [
'background_color' => 'rgba(0,115,183,0.7)',
'border_color' => '#3b8bba',
'grid_color' => 'rgba(0,0,0,.05)',
'height' => '200'
],
'branding' => [
'logo' => null,
'mini' => null,
'company' => null,
'title' => null,
],
];
}
public function testPrefix()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
self::assertEquals(3, $sut->getThemeConfig('active_warning'));
self::assertEquals('green', $sut->getThemeConfig('box_color'));
}
/**
* @group legacy
*/
public function testDeprecation()
{
$sut = $this->getSut($this->getDefaultSettings(), []);
self::assertFalse($sut->getThemeConfig('auto_reload_datatable'));
}
}

View File

@@ -9,15 +9,18 @@
namespace App\Tests\Twig\Runtime;
use App\Configuration\SystemConfiguration;
use App\Entity\Configuration;
use App\Entity\User;
use App\Event\ThemeEvent;
use App\Tests\Mocks\Security\CurrentUserFactory;
use App\Tests\Configuration\TestConfigLoader;
use App\Twig\Runtime\ThemeExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
@@ -26,15 +29,46 @@ use Twig\Loader\FilesystemLoader;
*/
class ThemeEventExtensionTest extends TestCase
{
protected function getSut(bool $hasListener = true): ThemeExtension
private function getDefaultSettings(): array
{
return [
'theme' => [
'active_warning' => 3,
'box_color' => 'green',
'select_type' => null,
'show_about' => true,
'chart' => [
'background_color' => 'rgba(0,115,183,0.7)',
'border_color' => '#3b8bba',
'grid_color' => 'rgba(0,0,0,.05)',
'height' => '200'
],
'branding' => [
'logo' => null,
'mini' => null,
'company' => null,
'title' => null,
],
],
];
}
protected function getSut(bool $hasListener = true, string $title = null): ThemeExtension
{
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->method('hasListeners')->willReturn($hasListener);
$dispatcher->expects($hasListener ? $this->once() : $this->never())->method('dispatch');
$user = (new CurrentUserFactory($this))->create(new User());
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
$translator->method('trans')->willReturn('foo');
return new ThemeExtension($dispatcher);
$configs = [
(new Configuration())->setName('theme.branding.title')->setValue($title)
];
$loader = new TestConfigLoader($configs);
$configuration = new SystemConfiguration($loader, $this->getDefaultSettings());
return new ThemeExtension($dispatcher, $translator, $configuration);
}
protected function getEnvironment(): Environment
@@ -119,4 +153,33 @@ class ThemeEventExtensionTest extends TestCase
$sut = $this->getSut(false);
self::assertEquals($expected, $sut->getProgressbarClass($percent, $reverseColors));
}
public function testGetTitle()
{
$sut = $this->getSut(false);
$this->assertEquals('Kimai foo', $sut->generateTitle());
$this->assertEquals('sdfsdf | Kimai foo', $sut->generateTitle('sdfsdf | '));
$this->assertEquals('<b>Kimai</b> ... foo', $sut->generateTitle('<b>', '</b> ... '));
$this->assertEquals('Kimai | foo', $sut->generateTitle(null, ' | '));
}
public function testGetBrandedTitle()
{
$sut = $this->getSut(false, 'MyCompany');
$this->assertEquals('MyCompany foo', $sut->generateTitle());
$this->assertEquals('sdfsdf | MyCompany foo', $sut->generateTitle('sdfsdf | '));
$this->assertEquals('<b>MyCompany</b> ... foo', $sut->generateTitle('<b>', '</b> ... '));
$this->assertEquals('MyCompany | foo', $sut->generateTitle(null, ' | '));
}
/**
* @group legacy
*/
public function testThemeConfig()
{
$sut = $this->getSut(false);
self::assertEquals(3, $sut->getThemeConfig('active_warning'));
self::assertEquals('green', $sut->getThemeConfig('box_color'));
self::assertFalse($sut->getThemeConfig('auto_reload_datatable'));
}
}

View File

@@ -40,8 +40,10 @@ class RuntimeExtensionsTest extends TestCase
$expected = [
'trigger',
'actions',
'get_title',
'progressbar_color',
'javascript_translations',
'theme_config',
'active_timesheets',
'encore_entry_css_source',
'render_widget',

View File

@@ -1,72 +0,0 @@
<?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\Twig;
use App\Configuration\ThemeConfiguration;
use App\Entity\Configuration;
use App\Tests\Configuration\TestConfigLoader;
use App\Twig\TitleExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\TwigFunction;
/**
* @covers \App\Twig\TitleExtension
*/
class TitleExtensionTest extends TestCase
{
protected function getSut(string $title = null): TitleExtension
{
$translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
$translator->method('trans')->willReturn('foo');
$configs = [
(new Configuration())->setName('theme.branding.title')->setValue($title)
];
$loader = new TestConfigLoader($configs);
$configuration = new ThemeConfiguration($loader, ['branding' => ['title' => null]]);
return new TitleExtension($translator, $configuration);
}
public function testGetFunctions()
{
$functions = ['get_title'];
$sut = $this->getSut();
$twigFunctions = $sut->getFunctions();
$this->assertCount(\count($functions), $twigFunctions);
$i = 0;
/** @var TwigFunction $function */
foreach ($twigFunctions as $function) {
$this->assertInstanceOf(TwigFunction::class, $function);
$this->assertEquals($functions[$i++], $function->getName());
}
}
public function testGetTitle()
{
$sut = $this->getSut();
$this->assertEquals('Kimai foo', $sut->generateTitle());
$this->assertEquals('sdfsdf | Kimai foo', $sut->generateTitle('sdfsdf | '));
$this->assertEquals('<b>Kimai</b> ... foo', $sut->generateTitle('<b>', '</b> ... '));
$this->assertEquals('Kimai | foo', $sut->generateTitle(null, ' | '));
}
public function testGetBrandedTitle()
{
$sut = $this->getSut('MyCompany');
$this->assertEquals('MyCompany foo', $sut->generateTitle());
$this->assertEquals('sdfsdf | MyCompany foo', $sut->generateTitle('sdfsdf | '));
$this->assertEquals('<b>MyCompany</b> ... foo', $sut->generateTitle('<b>', '</b> ... '));
$this->assertEquals('MyCompany | foo', $sut->generateTitle(null, ' | '));
}
}