diff --git a/tests/Services/LogSystem/EventCommentNeededHelperTest.php b/tests/Services/LogSystem/EventCommentNeededHelperTest.php index 070cf071..2eb2aceb 100644 --- a/tests/Services/LogSystem/EventCommentNeededHelperTest.php +++ b/tests/Services/LogSystem/EventCommentNeededHelperTest.php @@ -25,13 +25,14 @@ namespace App\Tests\Services\LogSystem; use App\Services\LogSystem\EventCommentNeededHelper; use App\Services\LogSystem\EventCommentType; use App\Settings\SystemSettings\HistorySettings; +use App\Tests\SettingsTestHelper; use PHPUnit\Framework\TestCase; class EventCommentNeededHelperTest extends TestCase { public function testIsCommentNeeded(): void { - $settings = new HistorySettings(); + $settings = SettingsTestHelper::createSettingsDummy(HistorySettings::class); $settings->enforceComments = [EventCommentType::PART_CREATE, EventCommentType::PART_EDIT]; $service = new EventCommentNeededHelper($settings); diff --git a/tests/SettingsTestHelper.php b/tests/SettingsTestHelper.php new file mode 100644 index 00000000..30b8a110 --- /dev/null +++ b/tests/SettingsTestHelper.php @@ -0,0 +1,57 @@ +. + */ + +declare(strict_types=1); + +namespace App\Tests; + +use InvalidArgumentException; +use ReflectionClass; + +class SettingsTestHelper +{ + /** + * Creates a new dummy settings object for testing purposes. + * It does not contain any embedded objects! + * @template T of object + * @param string $class + * @phpstan-param class-string $class + * @return object + * @phpstan-return T + */ + public static function createSettingsDummy(string $class): object + { + $reflection = new ReflectionClass($class); + + //Check if it is a settings class (has a Settings attribute) + if ($reflection->getAttributes(\Jbtronics\SettingsBundle\Settings\Settings::class) === []) { + throw new InvalidArgumentException("The class $class is not a settings class!"); + } + + $object = $reflection->newInstanceWithoutConstructor(); + + //If the object has some initialization logic, then call it + if ($object instanceof \Jbtronics\SettingsBundle\Settings\ResettableSettingsInterface) { + $object->resetToDefaultValues(); + } + + return $object; + } +} \ No newline at end of file