From 2df2ab70925a3f991a7179837ec0ad3a23599b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milan=20Felix=20=C5=A0ulc?= Date: Wed, 16 Aug 2023 16:10:55 +0200 Subject: [PATCH] DI: try to cast enable to boolean --- src/DI/SentryExtension.php | 2 +- tests/Cases/DI/SentryExtension.phpt | 40 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/DI/SentryExtension.php b/src/DI/SentryExtension.php index cc66957..105a9a9 100644 --- a/src/DI/SentryExtension.php +++ b/src/DI/SentryExtension.php @@ -37,7 +37,7 @@ class SentryExtension extends CompilerExtension public function getConfigSchema(): Schema { return Expect::structure([ - 'enable' => Expect::bool()->default(false), + 'enable' => Expect::bool()->before(fn ($s): mixed => is_scalar($s) ? filter_var($s, FILTER_VALIDATE_BOOLEAN) : $s)->default(false), 'client' => Expect::array()->default([]), 'integrations' => Expect::bool()->default(true), 'logger' => Expect::structure([ diff --git a/tests/Cases/DI/SentryExtension.phpt b/tests/Cases/DI/SentryExtension.phpt index 77f108d..b8dec4c 100644 --- a/tests/Cases/DI/SentryExtension.phpt +++ b/tests/Cases/DI/SentryExtension.phpt @@ -7,6 +7,7 @@ use Contributte\Tester\Toolkit; use Contributte\Tester\Utils\ContainerBuilder; use Contributte\Tester\Utils\Neonkit; use Nette\DI\Compiler; +use Nette\DI\InvalidConfigurationException; use Sentry\SentrySdk; use Sentry\State\HubInterface; use Tester\Assert; @@ -85,6 +86,45 @@ Toolkit::test(function (): void { Assert::count(13, SentrySdk::getCurrentHub()->getClient()->getOptions()->getIntegrations()); }); +// Enable is string +Toolkit::test(function (): void { + $container = ContainerBuilder::of() + ->withCompiler(function (Compiler $compiler): void { + $compiler->addExtension('tracy', new TracyExtension()); + $compiler->addExtension('sentry', new SentryExtension()); + $compiler->addConfig(Neonkit::load(' + sentry: + enable: "true" + client: + dsn: "https://fakefakefake@fakefake.ingest.sentry.io/12345678" + ')); + }) + ->build(); + + call_user_func([$container, 'initialize']); + + Assert::equal(12345678, SentrySdk::getCurrentHub()->getClient()->getOptions()->getDsn()->getProjectId()); +}); +// Enable is invalid +Toolkit::test(function (): void { + Assert::exception( + static function (): void { + ContainerBuilder::of() + ->withCompiler(function (Compiler $compiler): void { + $compiler->addExtension('tracy', new TracyExtension()); + $compiler->addExtension('sentry', new SentryExtension()); + $compiler->addConfig(Neonkit::load(' + sentry: + enable: [] + ')); + }) + ->build(); + }, + InvalidConfigurationException::class, + "The item 'sentry › enable' expects to be bool, array given." + ); +}); + // No default integrations Toolkit::test(function (): void { $container = ContainerBuilder::of()