Skip to content

Commit

Permalink
DI: try to cast enable to boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Aug 16, 2023
1 parent d376611 commit 2df2ab7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DI/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
40 changes: 40 additions & 0 deletions tests/Cases/DI/SentryExtension.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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://[email protected]/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()
Expand Down

0 comments on commit 2df2ab7

Please sign in to comment.