mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-03-31 15:07:47 +01:00
Fix ResponseManager filter criteria handling and update tests
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
* fix [Flasher] Remove redundant callable check in FlasherContainer::getContainer()
|
||||
* fix [Flasher] Fix NotificationStorageMethods::resolveResourceName() return type from ?string to string (never returns null)
|
||||
* fix [Flasher] Fix parameter name inconsistency in NotificationBuilderInterface::options() - changed $merge to $append to match implementation
|
||||
* fix [Flasher] Add type validation for callable presenter return values in ResponseManager with descriptive error messages
|
||||
|
||||
## [v2.1.3](https://github.com/php-flasher/php-flasher/compare/v2.1.2...v2.1.3) - 2025-01-25
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@ final class ResponseManager implements ResponseManagerInterface
|
||||
|
||||
/**
|
||||
* @throws PresenterNotFoundException
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
private function createPresenter(string $alias): PresenterInterface
|
||||
{
|
||||
@@ -77,7 +78,20 @@ final class ResponseManager implements ResponseManagerInterface
|
||||
|
||||
$presenter = $this->presenters[$alias];
|
||||
|
||||
return \is_callable($presenter) ? $presenter() : $presenter;
|
||||
if (\is_callable($presenter)) {
|
||||
$presenter = $presenter();
|
||||
|
||||
if (!$presenter instanceof PresenterInterface) {
|
||||
throw new \InvalidArgumentException(\sprintf(
|
||||
'Presenter callable for "%s" must return an instance of %s, %s returned.',
|
||||
$alias,
|
||||
PresenterInterface::class,
|
||||
\get_debug_type($presenter)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $presenter;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -169,4 +169,58 @@ final class ResponseManagerTest extends TestCase
|
||||
});
|
||||
JAVASCRIPT;
|
||||
}
|
||||
|
||||
public function testAddPresenterWithCallable(): void
|
||||
{
|
||||
$resourceManager = $this->createMock(ResourceManagerInterface::class);
|
||||
$resourceManager->method('populateResponse')->willReturnArgument(0);
|
||||
|
||||
$storageManager = $this->createMock(StorageManagerInterface::class);
|
||||
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
|
||||
$responseManager = new ResponseManager($resourceManager, $storageManager, $eventDispatcher);
|
||||
|
||||
$customPresenter = new \Flasher\Prime\Response\Presenter\ArrayPresenter();
|
||||
$responseManager->addPresenter('custom', fn () => $customPresenter);
|
||||
|
||||
$result = $responseManager->render('custom');
|
||||
|
||||
$this->assertIsArray($result);
|
||||
}
|
||||
|
||||
public function testAddPresenterWithCallableReturningInvalidTypeThrowsException(): void
|
||||
{
|
||||
$resourceManager = $this->createMock(ResourceManagerInterface::class);
|
||||
$resourceManager->method('populateResponse')->willReturnArgument(0);
|
||||
|
||||
$storageManager = $this->createMock(StorageManagerInterface::class);
|
||||
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
|
||||
$responseManager = new ResponseManager($resourceManager, $storageManager, $eventDispatcher);
|
||||
|
||||
$responseManager->addPresenter('invalid', fn () => 'not a presenter');
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage('Presenter callable for "invalid" must return an instance of');
|
||||
|
||||
$responseManager->render('invalid');
|
||||
}
|
||||
|
||||
public function testAddPresenterWithDirectInstance(): void
|
||||
{
|
||||
$resourceManager = $this->createMock(ResourceManagerInterface::class);
|
||||
$resourceManager->method('populateResponse')->willReturnArgument(0);
|
||||
|
||||
$storageManager = $this->createMock(StorageManagerInterface::class);
|
||||
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
||||
|
||||
$responseManager = new ResponseManager($resourceManager, $storageManager, $eventDispatcher);
|
||||
|
||||
$customPresenter = new \Flasher\Prime\Response\Presenter\ArrayPresenter();
|
||||
$responseManager->addPresenter('direct', $customPresenter);
|
||||
|
||||
$result = $responseManager->render('direct');
|
||||
|
||||
$this->assertIsArray($result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user