From 144602e29d26c0fbe70e5088c5feca4eca6bb433 Mon Sep 17 00:00:00 2001 From: Tim Lieberman Date: Thu, 1 Apr 2021 13:54:33 -0700 Subject: [PATCH] Permit zero hops to allow messages visible only in the current request. Ref: #7 Signed-off-by: Tim Lieberman --- src/Exception/InvalidHopsValueException.php | 2 +- src/FlashMessages.php | 2 +- test/FlashMessagesTest.php | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Exception/InvalidHopsValueException.php b/src/Exception/InvalidHopsValueException.php index 77d64a6..9d927c2 100644 --- a/src/Exception/InvalidHopsValueException.php +++ b/src/Exception/InvalidHopsValueException.php @@ -19,7 +19,7 @@ class InvalidHopsValueException extends InvalidArgumentException implements Exce public static function valueTooLow(string $key, int $hops): self { return new self(sprintf( - 'Hops value specified for flash message "%s" was too low; must be greater than 0, received %d', + 'Hops value specified for flash message "%s" was too low; must be non-negative, received %d', $key, $hops )); diff --git a/src/FlashMessages.php b/src/FlashMessages.php index c2d65ec..3dde9e1 100644 --- a/src/FlashMessages.php +++ b/src/FlashMessages.php @@ -73,7 +73,7 @@ public static function createFromSession( */ public function flash(string $key, $value, int $hops = 1): void { - if ($hops < 1) { + if ($hops < 0) { throw Exception\InvalidHopsValueException::valueTooLow($key, $hops); } diff --git a/test/FlashMessagesTest.php b/test/FlashMessagesTest.php index 530f9bc..40fa75c 100644 --- a/test/FlashMessagesTest.php +++ b/test/FlashMessagesTest.php @@ -365,6 +365,23 @@ public function testCreationAggregatesThrowsExceptionIfInvalidNumberOfHops() $this->anything() ); + $flash = FlashMessages::createFromSession($this->session); + $flash->flashNow('test', 'value', -1); + } + + public function testFlashNowAcceptsZeroHops() + { + $this->session + ->expects($this->once()) + ->method('has') + ->with(FlashMessagesInterface::FLASH_NEXT) + ->willReturn(false); + $this->session + ->expects($this->once()) + ->method('get') + ->with(FlashMessagesInterface::FLASH_NEXT, []) + ->willReturn([]); + $flash = FlashMessages::createFromSession($this->session); $flash->flashNow('test', 'value', 0); }