8000 feat: Make serializer configurable via YAML configuration by lenchester · Pull Request #1390 · php-enqueue/enqueue-dev · GitHub
[go: up one dir, main page]

Skip to content

feat: Make serializer configurable via YAML configuration #1390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter 8000

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions pkg/rdkafka/RdKafkaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public function __construct(array $config)
$this->config = $config;
$this->kafkaConsumers = [];
$this->rdKafkaConsumers = [];

$this->setSerializer(new JsonSerializer());
$this->configureSerializer($config);
}

/**
Expand Down Expand Up @@ -180,6 +179,58 @@ public static function getLibrdKafkaVersion(): string
return "$major.$minor.$patch";
}

/**
* @return void
* JsonSerializer should be the default fallback if no serializer is specified
*/
Comment on lines +182 to +185
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to re-specify the return type, since it's already present in the PHP declaration.

As for the description, I'm not sure it's worthwile - but nothing against it either.

Suggested change
/**
* @return void
* JsonSerializer should be the default fallback if no serializer is specified
*/
/**
* JsonSerializer is the default fallback, if no serializer is specified.
*/

private function configureSerializer(array $config): void
{
if (!isset($config['serializer'])) {
$this->setSerializer(new JsonSerializer());

return;
}

$serializer = $config['serializer'];

if ($serializer instanceof Serializer) {
$this->setSerializer($serializer);

return;
}

$serializerClass = $this->resolveSerializerClass($serializer);

if (!class_exists($serializerClass) || !is_a($serializerClass, Serializer::class, true)) {
throw $this->createInvalidSerializerException($serializerClass);
}

$serializerOptions = $serializer['options'] ?? [];
$this->setSerializer(new $serializerClass($serializerOptions));
}

private function resolveSerializerClass(mixed $serializer): string
{
if (is_string($serializer)) {
return $serializer;
}

if (is_array($serializer) && isset($serializer['class'])) {
return $serializer['class'];
}

throw $this->createInvalidSerializerException($serializer);
}

private function createInvalidSerializerException(mixed $value): \InvalidArgumentException
{
return new \InvalidArgumentException(sprintf(
'Invalid serializer configuration. Expected "serializer" to be a string, an array with a "class" key, or a %s instance. Received %s instead.',
Serializer::class,
get_debug_type($value)
));
}

private function getConf(): Conf
{
if (null === $this->conf) {
Expand Down
28 changes: 28 additions & 0 deletions pkg/rdkafka/Tests/RdKafkaContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ public function testShouldSetJsonSerializerInConstructor()
$this->assertInstanceOf(JsonSerializer::class, $context->getSerializer());
}

public function testShouldUseStringSerializerClassFromConfig()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding unit test that checks assertSame instance when serializer object is passed. If you are able you might want to check constructor options with method call assertions.

{
$mockSerializerClass = get_class($this->createMock(Serializer::class));

$context = new RdKafkaContext([
'serializer' => $mockSerializerClass,
]);

$this->assertInstanceOf($mockSerializerClass, $context->getSerializer());
}

public function testShouldUseJsonSerializer()
{
$context = new RdKafkaContext([]);

$this->assertInstanceOf(JsonSerializer::class, $context->getSerializer());
}

public function testShouldThrowExceptionOnInvalidSerializerConfig()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid serializer configuration');

new RdKafkaContext([
'serializer' => 123,
]);
}

public function testShouldAllowGetPreviouslySetSerializer()
{
$context = new RdKafkaContext([]);
Expand Down
Loading
0