Skip to content

Commit

Permalink
Merge pull request #96 from spiral/bugfix/undefined-handler
Browse files Browse the repository at this point in the history
* Exception handling added when searching for a handler
  • Loading branch information
butschster committed Dec 28, 2023
2 parents feb0aab + 7ceba49 commit fd53f42
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/GRPC/ServiceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getServices(): array
$result = [];

foreach ($this->classes->getClasses(ServiceInterface::class) as $service) {
if (! $service->isInstantiable()) {
if (!$service->isInstantiable()) {
continue;
}

Expand Down
7 changes: 4 additions & 3 deletions src/Queue/PayloadDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public function deserialize(ReceivedTaskInterface $task): mixed
return $serializer->unserialize($payload, $class);
}

$class = $this->detectTypeFromJobHandler(
$this->registry->getHandler($name),
);
try {
$class = $this->detectTypeFromJobHandler($this->registry->getHandler($name));
} catch (\Throwable) {
}

if ($class === 'string') {
return $payload;
Expand Down
27 changes: 27 additions & 0 deletions tests/src/Queue/PayloadDeserializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Mockery as m;
use PHPUnit\Framework\Attributes\DataProvider;
use Spiral\Queue\Exception\JobException;
use Spiral\Queue\HandlerInterface;
use Spiral\Queue\HandlerRegistryInterface;
use Spiral\Queue\SerializerRegistryInterface;
Expand Down Expand Up @@ -124,6 +125,32 @@ public function testGetClassFromHandler(HandlerInterface $handler): void
$this->assertSame($unserialized, $this->deserializer->deserialize($task));
}

public function testGetClassFromHandlerShouldIgnoreMissedHandler(): void
{
$task = m::mock(ReceivedTaskInterface::class);

$task->shouldReceive('hasHeader')
->once()
->with(Queue::SERIALIZED_CLASS_HEADER_KEY)
->andReturnFalse();

$task->shouldReceive('getName')->once()->andReturn($name = 'foo-task');
$task->shouldReceive('getPayload')->once()->andReturn($payload = '{"foo":"bar"}');

$this->serializer->shouldReceive('getSerializer')->once()->with($name)
->andReturn($serializer = m::mock(SerializerInterface::class));

$this->registry->shouldReceive('getHandler')
->once()
->with($name)
->andThrow(JobException::class);

$serializer->shouldReceive('unserialize')->once()->with($payload)
->andReturn($unserialized = 'unserialized-payload');

$this->assertSame($unserialized, $this->deserializer->deserialize($task));
}

#[DataProvider('getInvalidHandlersDataProvider')]
public function testGetClassFromInvalidHandler(HandlerInterface $handler): void
{
Expand Down

0 comments on commit fd53f42

Please sign in to comment.