Skip to content

Commit

Permalink
Allow using short settings names in SettingsManager and added tests f…
Browse files Browse the repository at this point in the history
…or SettingsManager
  • Loading branch information
jbtronics committed Jan 7, 2024
1 parent 77f35d6 commit d342441
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'$settingsHydrator' => service('jbtronics.settings.settings_hydrator'),
'$settingsResetter' => service('jbtronics.settings.settings_resetter'),
'$settingsValidator' => service('jbtronics.settings.settings_validator'),
'$settingsRegistry' => service('jbtronics.settings.settings_registry'),
])
;
$services->alias(SettingsManagerInterface::class, 'jbtronics.settings.settings_manager');
Expand Down
6 changes: 6 additions & 0 deletions src/Manager/SettingsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(
private readonly SettingsHydratorInterface $settingsHydrator,
private readonly SettingsResetterInterface $settingsResetter,
private readonly SettingsValidatorInterface $settingsValidator,
private readonly SettingsRegistryInterface $settingsRegistry,
)
{

Expand All @@ -48,6 +49,11 @@ public function __construct(

public function get(string $settingsClass): object
{
//If not a class name is given, try to resolve the name via SettingsRegistry
if (!class_exists($settingsClass)) {
$settingsClass = $this->settingsRegistry->getSettingsClassByName($settingsClass);
}

//Check if the settings class is already in memory
if (isset($this->settings_by_class[$settingsClass])) {
return $this->settings_by_class[$settingsClass];
Expand Down
4 changes: 2 additions & 2 deletions src/Manager/SettingsManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ interface SettingsManagerInterface
{
/**
* Returns the value filled instance of the given settings class.
* @param string $settingsClass
* @param string $settingsClass The settings class to get the instance for. This can either be a class string, or the short name of the settings class.
* @return object
* @template T of object
* @phpstan-param class-string<T> $settingsClass
* @phpstan-param class-string<T>|string $settingsClass
* @phpstan-return T
*/
public function get(string $settingsClass): object;

Check failure on line 42 in src/Manager/SettingsManagerInterface.php

View workflow job for this annotation

GitHub Actions / Static analysis

Template type T of method Jbtronics\SettingsBundle\Manager\SettingsManagerInterface::get() is not referenced in a parameter.
Expand Down
99 changes: 99 additions & 0 deletions tests/Manager/SettingsManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/*
* This file is part of jbtronics/settings-bundle (https://github.com/jbtronics/settings-bundle).
*
* Copyright (c) 2024 Jan Böhmer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace Jbtronics\SettingsBundle\Tests\Manager;

use Jbtronics\SettingsBundle\Manager\SettingsManager;
use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface;
use Jbtronics\SettingsBundle\Tests\TestApplication\Settings\SimpleSettings;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* The functional/integration test for the SettingsManager
*/
class SettingsManagerTest extends KernelTestCase
{

private SettingsManagerInterface $service;

public function setUp(): void
{
self::bootKernel();
$this->service = self::getContainer()->get(SettingsManagerInterface::class);
}

public function testGet(): void
{
//Test if we can get the settings class by classname
/** @var SimpleSettings $settings */
$settings = $this->service->get(SimpleSettings::class);
$this->assertInstanceOf(SimpleSettings::class, $settings);

//Try to change a value
$settings->setValue1('changed');

//Test if we can get the settings class by short name
$settings2 = $this->service->get('simple');
$this->assertInstanceOf(SimpleSettings::class, $settings2);
//Must be the same instance of the class
$this->assertSame($settings, $settings2);

//Test if the value is changed
$this->assertEquals('changed', $settings2->getValue1());
}

public function testResetToDefaultValues(): void
{
/** @var SimpleSettings $settings */
$settings = $this->service->get(SimpleSettings::class);
$settings->setValue1('changed');

$this->service->resetToDefaultValues($settings);

$this->assertEquals('default', $settings->getValue1());
}



public function testSaveAndReload(): void
{
/** @var SimpleSettings $settings */
$settings = $this->service->get(SimpleSettings::class);
$settings->setValue1('changed');

//Save the settings
$this->service->save($settings);

//Change the value again
$settings->setValue1('changed again');

//Reload the settings
$this->service->reload($settings);

//And the value should be the one, which we saved before
$this->assertEquals('changed', $settings->getValue1());
}
}

0 comments on commit d342441

Please sign in to comment.