Skip to content

Commit 8850436

Browse files
committed
Add all tests to class_serializers option
1 parent 50b7043 commit 8850436

File tree

4 files changed

+67
-39
lines changed

4 files changed

+67
-39
lines changed

src/DependencyInjection/SentryExtension.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\Config\FileLocator;
1515
use Symfony\Component\Console\ConsoleEvents;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17-
use Symfony\Component\DependencyInjection\Definition;
1817
use Symfony\Component\DependencyInjection\Loader;
1918
use Symfony\Component\DependencyInjection\Reference;
2019
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@@ -97,11 +96,22 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
9796
}
9897

9998
if (\array_key_exists('before_send', $processedOptions)) {
100-
$this->mapCallableValue($options, 'setBeforeSendCallback', $processedOptions['before_send']);
99+
$beforeSendCallable = $this->valueToCallable($processedOptions['before_send']);
100+
$options->addMethodCall('setBeforeSendCallback', [$beforeSendCallable]);
101101
}
102102

103103
if (\array_key_exists('before_breadcrumb', $processedOptions)) {
104-
$this->mapCallableValue($options, 'setBeforeBreadcrumbCallback', $processedOptions['before_breadcrumb']);
104+
$beforeBreadcrumbCallable = $this->valueToCallable($processedOptions['before_breadcrumb']);
105+
$options->addMethodCall('setBeforeBreadcrumbCallback', [$beforeBreadcrumbCallable]);
106+
}
107+
108+
if (\array_key_exists('class_serializers', $processedOptions)) {
109+
$classSerializers = [];
110+
foreach ($processedOptions['class_serializers'] as $class => $serializer) {
111+
$classSerializers[$class] = $this->valueToCallable($serializer);
112+
}
113+
114+
$options->addMethodCall('setClassSerializers', [$classSerializers]);
105115
}
106116

107117
if (\array_key_exists('integrations', $processedOptions)) {
@@ -114,20 +124,13 @@ private function passConfigurationToOptions(ContainerBuilder $container, array $
114124
}
115125
}
116126

117-
/**
118-
* @param Definition $options
119-
* @param string $method
120-
* @param callable|string $optionValue
121-
*/
122-
private function mapCallableValue(Definition $options, string $method, $optionValue): void
127+
private function valueToCallable($value)
123128
{
124-
if (is_string($optionValue) && 0 === strpos($optionValue, '@')) {
125-
$beforeSend = new Reference(substr($optionValue, 1));
126-
} else {
127-
$beforeSend = $optionValue;
129+
if (is_string($value) && 0 === strpos($value, '@')) {
130+
return new Reference(substr($value, 1));
128131
}
129132

130-
$options->addMethodCall($method, [$beforeSend]);
133+
return $value;
131134
}
132135

133136
/**

test/BaseTestCase.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Sentry\Options;
7+
use Sentry\SentryBundle\Test\DependencyInjection\ConfigurationTest;
8+
9+
abstract class BaseTestCase extends TestCase
10+
{
11+
protected function classSerializersAreSupported(): bool
12+
{
13+
try {
14+
new Options(['class_serializers' => []]);
15+
16+
return true;
17+
} catch (\Throwable $throwable) {
18+
return false;
19+
}
20+
}
21+
22+
protected function getSupportedOptionsCount(): int
23+
{
24+
if ($this->classSerializersAreSupported()) {
25+
return ConfigurationTest::SUPPORTED_SENTRY_OPTIONS_COUNT + 1;
26+
}
27+
28+
return ConfigurationTest::SUPPORTED_SENTRY_OPTIONS_COUNT;
29+
}
30+
}

test/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Sentry\SentryBundle\Test\DependencyInjection;
44

55
use Jean85\PrettyVersions;
6-
use PHPUnit\Framework\TestCase;
76
use Sentry\Options;
87
use Sentry\SentryBundle\DependencyInjection\Configuration;
8+
use Sentry\SentryBundle\Test\BaseTestCase;
99
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1010
use Symfony\Component\Config\Definition\Processor;
1111
use Symfony\Component\HttpKernel\Kernel;
1212

13-
class ConfigurationTest extends TestCase
13+
class ConfigurationTest extends BaseTestCase
1414
{
1515
public const SUPPORTED_SENTRY_OPTIONS_COUNT = 23;
1616

@@ -204,24 +204,4 @@ private function processConfiguration(array $values): array
204204

205205
return $processor->processConfiguration(new Configuration(), ['sentry' => $values]);
206206
}
207-
208-
private function classSerializersAreSupported(): bool
209-
{
210-
try {
211-
new Options(['class_serializers' => []]);
212-
213-
return true;
214-
} catch (\Throwable $throwable) {
215-
return false;
216-
}
217-
}
218-
219-
private function getSupportedOptionsCount(): int
220-
{
221-
if ($this->classSerializersAreSupported()) {
222-
return self::SUPPORTED_SENTRY_OPTIONS_COUNT + 1;
223-
}
224-
225-
return self::SUPPORTED_SENTRY_OPTIONS_COUNT;
226-
}
227207
}

test/DependencyInjection/SentryExtensionTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace Sentry\SentryBundle\Test\DependencyInjection;
44

55
use Jean85\PrettyVersions;
6-
use PHPUnit\Framework\TestCase;
76
use Sentry\Breadcrumb;
87
use Sentry\Event;
98
use Sentry\Integration\IntegrationInterface;
109
use Sentry\Options;
1110
use Sentry\SentryBundle\DependencyInjection\SentryExtension;
11+
use Sentry\SentryBundle\Test\BaseTestCase;
1212
use Symfony\Component\DependencyInjection\Alias;
1313
use Symfony\Component\DependencyInjection\Container;
1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -17,7 +17,7 @@
1717
use Symfony\Component\HttpFoundation\RequestStack;
1818
use Symfony\Component\HttpKernel\Kernel;
1919

20-
class SentryExtensionTest extends TestCase
20+
class SentryExtensionTest extends BaseTestCase
2121
{
2222
private const OPTIONS_TEST_PUBLIC_ALIAS = 'sentry.options.public_alias';
2323

@@ -34,7 +34,7 @@ public function testDataProviderIsMappingTheRightNumberOfOptions(): void
3434
}
3535

3636
$this->assertCount(
37-
$expectedCount,
37+
$this->getSupportedOptionsCount(),
3838
$supportedOptions,
3939
'Provider for configuration options mismatch: ' . PHP_EOL . print_r($supportedOptions, true)
4040
);
@@ -138,6 +138,16 @@ public function optionsValueProvider(): array
138138
$options[] = ['capture_silenced_errors', true, 'shouldCaptureSilencedErrors'];
139139
}
140140

141+
if ($this->classSerializersAreSupported()) {
142+
$options['class_serializer'] = [
143+
'class_serializers',
144+
[
145+
self::class => __NAMESPACE__ . '\mockClassSerializer',
146+
],
147+
'getClassSerializers',
148+
];
149+
}
150+
141151
return $options;
142152
}
143153

@@ -375,6 +385,11 @@ function mockBeforeBreadcrumb(Breadcrumb $breadcrumb): ?Breadcrumb
375385
return null;
376386
}
377387

388+
function mockClassSerializer($object)
389+
{
390+
return ['value' => 'serialized_class'];
391+
}
392+
378393
class CallbackMock
379394
{
380395
public static function callback()

0 commit comments

Comments
 (0)