Skip to content

Commit

Permalink
Merge pull request #7 from softonic/feature/CAT-3814-Add-xMsgPriority…
Browse files Browse the repository at this point in the history
…-and-xRequestId

Allow to pass headers
  • Loading branch information
xaviapa authored Mar 14, 2022
2 parents 95b0b4d + f013832 commit 169e9ef
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 25 deletions.
48 changes: 27 additions & 21 deletions src/ExternalEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ExternalEvents
{
private const CAMEL_CASE_LETTERS_DETECTION = '#(?!(?<=^)|(?<=\\\))[A-Z]#';

public static function publish(Message $class): void
public static function publish(Message $class, array $headers = []): void
{
$routingKey = str_replace(
'\\',
Expand All @@ -26,45 +26,51 @@ public static function publish(Message $class): void
)
)
);
$message = [
'data' => $class->serializeToJsonString(),

$message = [
'data' => $class->serializeToJsonString(),
'headers' => $headers,
];

publish($routingKey, $message);
}

/**
* @throws InvalidMessageException
*/
public static function decode(string $expectedEvent, string $message): mixed
{
try {
$event = new $expectedEvent();
$event->mergeFromJsonString($message);

return $event;
} catch (Exception) {
throw new InvalidMessageException(
"The message is not a valid {$expectedEvent} message"
);
}
}

public static function decorateListener(string $listenerClass): \Closure
{
return static function (string $event, array $message) use ($listenerClass) {
try {
$listener = resolve($listenerClass);

if (method_exists($listener, 'setHeaders')) {
$listener->setHeaders($message[0]['headers']);
}

$eventParameter = new ReflectionParameter([$listenerClass, 'handle'], 0);
$className = $eventParameter->getType()->getName();

$payload = ExternalEvents::decode($className, $message[0]['data']);

return resolve($listenerClass)->handle($payload);
return $listener->handle($payload);
} catch (ReflectionException $e) {
throw new BadMethodCallException(
"$listenerClass must have a handle method with a single parameter of type object child of \Google\Protobuf\Internal\Message"
);
}
};
}

/**
* @throws InvalidMessageException
*/
public static function decode(string $expectedEvent, string $message): mixed
{
try {
$event = new $expectedEvent();
$event->mergeFromJsonString($message);

return $event;
} catch (Exception $e) {
throw new InvalidMessageException("The message is not a valid {$expectedEvent} message", 0, $e);
}
}
}
65 changes: 61 additions & 4 deletions tests/ExternalEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@
function publish($routingKey, $message)
{
assertSame('softonic.laravel_protobuf_events.fake_proto.fake_message', $routingKey);
assertSame(['data' => '{"content":":content:"}'], $message);

if (empty($message['headers'])) {
$expectedMessage = [
'data' => '{"content":":content:"}',
'headers' => [],
];
} else {
$expectedMessage = [
'data' => '{"content":":content:"}',
'headers' => ['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'],
];
}
assertSame($expectedMessage, $message);
}

class ExternalEventsTest extends TestCase
Expand All @@ -26,6 +38,7 @@ public function whenDecodeMessageItShouldReturnTheMessageObject(): void
$codedMessage = $message->serializeToJsonString();

$decodedMessage = ExternalEvents::decode(FakeMessage::class, $codedMessage);

self::assertSame(':content:', $decodedMessage->getContent());
}

Expand All @@ -45,14 +58,27 @@ public function whenDecodeAnInvalidMessageItShouldThrowAnException(): void
/**
* @test
*/
public function whenPublishMessageItShouldPublishIt(): void
public function whenPublishMessageWithoutHeadersItShouldPublishIt(): void
{
$message = new FakeMessage();
$message->setContent(':content:');

ExternalEvents::publish($message);
}

/**
* @test
*/
public function whenPublishMessageWithHeadersItShouldPublishIt(): void
{
$headers = ['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'];

$message = new FakeMessage();
$message->setContent(':content:');

ExternalEvents::publish($message, $headers);
}

/**
* @test
*/
Expand All @@ -63,7 +89,7 @@ public function process()
{
}
};
$class = $invalidListener::class;
$class = $invalidListener::class;

$this->expectException(BadMethodCallException::class);
$this->expectErrorMessage(
Expand All @@ -76,7 +102,7 @@ public function process()
/**
* @test
*/
public function whenDecoratingAListenerItShouldExecuteIt(): void
public function whenDecoratingAListenerWithoutHeadersItShouldExecuteIt(): void
{
$listener = new class() {
public function handle(FakeMessage $message)
Expand All @@ -90,4 +116,35 @@ public function handle(FakeMessage $message)

ExternalEvents::decorateListener($listener::class)(':event:', [['data' => $message->serializeToJsonString()]]);
}

/**
* @test
*/
public function whenDecoratingAListenerWithHeadersItShouldExecuteIt(): void
{
$listener = new class() {
public function setHeaders(array $headers)
{
assertSame(['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'], $headers);
}

public function handle(FakeMessage $message)
{
assertSame(':content:', $message->getContent());
}
};

$message = new FakeMessage();
$message->setContent(':content:');

ExternalEvents::decorateListener($listener::class)(
':event:',
[
[
'data' => $message->serializeToJsonString(),
'headers' => ['xRequestId' => '7b15d663-8d55-4e2f-82cc-4473576a4a17'],
],
]
);
}
}

0 comments on commit 169e9ef

Please sign in to comment.