Skip to content

Commit

Permalink
Fix headers error
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviapa committed Mar 16, 2022
1 parent 169e9ef commit 3da075d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ExternalEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function decorateListener(string $listenerClass): \Closure
try {
$listener = resolve($listenerClass);

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

Expand Down
31 changes: 31 additions & 0 deletions tests/ExternalEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BadMethodCallException;
use Orchestra\Testbench\TestCase;
use function PHPUnit\Framework\assertSame;
use function PHPUnit\Framework\assertTrue;
use Softonic\LaravelProtobufEvents\Exceptions\InvalidMessageException;
use Softonic\LaravelProtobufEvents\FakeProto\FakeMessage;

Expand Down Expand Up @@ -117,6 +118,36 @@ public function handle(FakeMessage $message)
ExternalEvents::decorateListener($listener::class)(':event:', [['data' => $message->serializeToJsonString()]]);
}

/**
* @test
*/
public function whenDecoratingAListenerWithSetHeadersMethodButWithoutSendingHeadersItShouldExecuteIt(): void
{
$listener = new class() {
public function setHeaders(array $headers)
{
assertTrue(false, 'setHeaders() should not be executed if no headers are received');
}

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

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

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

/**
* @test
*/
Expand Down

0 comments on commit 3da075d

Please sign in to comment.