Skip to content

Commit

Permalink
Permit zero hops to allow messages visible only in the current reques…
Browse files Browse the repository at this point in the history
…t. Ref: mezzio#7

Signed-off-by: Tim Lieberman <[email protected]>
  • Loading branch information
timdev committed Apr 1, 2021
1 parent 460b5ad commit 144602e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Exception/InvalidHopsValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
Expand Down
2 changes: 1 addition & 1 deletion src/FlashMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions test/FlashMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 144602e

Please sign in to comment.