Skip to content

Commit

Permalink
Merge pull request #8 from softonic/Fix-headers-error
Browse files Browse the repository at this point in the history
Fix headers error
  • Loading branch information
xaviapa authored Mar 16, 2022
2 parents 169e9ef + 3da075d commit 1dca2b3
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 1dca2b3

Please sign in to comment.