Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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
12 changes: 6 additions & 6 deletions src/Message/Handler/HandlerResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function __construct(
*
* @throws HandlerNotFoundException If no handler exists for the message type.
* @throws InvalidHandlerConfigurationException If the handler definition is configured incorrectly.
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*/
public function resolve(string $messageType): HandlerInterface
{
Expand All @@ -72,7 +71,6 @@ public function resolve(string $messageType): HandlerInterface
/**
* @throws HandlerNotFoundException
* @throws InvalidHandlerConfigurationException
* @throws ContainerExceptionInterface
*/
private function internalResolve(string $messageType): HandlerInterface
{
Expand All @@ -96,15 +94,18 @@ private function internalResolve(string $messageType): HandlerInterface
/**
* @throws HandlerNotFoundException
* @throws InvalidHandlerConfigurationException
* @throws ContainerExceptionInterface
*/
private function getHandlerFromContainer(string $messageType, string $id): HandlerInterface
{
if (!$this->container->has($id)) {
throw new HandlerNotFoundException($messageType);
}

$handler = $this->container->get($id);
try {
$handler = $this->container->get($id);
} catch (ContainerExceptionInterface $exception) {
throw new InvalidHandlerConfigurationException($messageType, $exception->getMessage(), $exception);
}

if ($handler instanceof HandlerInterface) {
return $handler;
Expand All @@ -126,13 +127,12 @@ private function getHandlerFromContainer(string $messageType, string $id): Handl

/**
* @throws InvalidHandlerConfigurationException
* @throws ContainerExceptionInterface
*/
private function createCallableHandler(string $messageType, mixed $definition): CallableHandler
{
try {
$callable = $this->callableFactory->create($definition);
} catch (InvalidCallableConfigurationException $exception) {
} catch (InvalidCallableConfigurationException|ContainerExceptionInterface $exception) {
throw new InvalidHandlerConfigurationException($messageType, $exception->getMessage(), $exception);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/Message/Handler/Resolver/HandlerResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Test\Support\Container\Exception\NotFoundException;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Queue\Message\Handler\HandlerNotFoundException;
use Yiisoft\Queue\Message\Handler\HandlerResolver;
Expand Down Expand Up @@ -181,6 +182,40 @@ public function handle(): void {}

$resolver->resolve('invalid');
}

public function testResolveThrowsWrappedContainerExceptionWhenGettingHandlerFromContainer(): void
{
$container = new SimpleContainer(
factory: static fn(string $id): mixed => throw new NotFoundException($id),
hasCallback: static fn(string $id): bool => true,
);
$resolver = new HandlerResolver([], $container);

$this->expectException(InvalidHandlerConfigurationException::class);
$this->expectExceptionMessage(
'Queue handler for message type "simple" is configured incorrectly. No definition or class found for "simple".',
);
$resolver->resolve('simple');
}

public function testResolveThrowsWrappedContainerExceptionWhenCreatingCallableHandler(): void
{
$container = new SimpleContainer(
factory: static fn(string $id): mixed => throw new NotFoundException($id),
hasCallback: static fn(string $id): bool => true,
);
$resolver = new HandlerResolver(
['simple' => ['NonExistentClassName', 'handle']],
$container,
);

$this->expectException(InvalidHandlerConfigurationException::class);
$this->expectExceptionMessage(
'Queue handler for message type "simple" is configured incorrectly. No definition or class found for "NonExistentClassName".',
);

$resolver->resolve('simple');
}
}

function namedFunctionHandler(MessageInterface $message): void
Expand Down
Loading