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); }