From ba318f05ba8bbc0e62281b5bd4cceaecc1ca20d8 Mon Sep 17 00:00:00 2001 From: Tim Lieberman Date: Thu, 1 Apr 2021 13:54:33 -0700 Subject: [PATCH 1/4] Permit zero hops to allow messages visible only in the current request. Ref: #7 Signed-off-by: Tim Lieberman --- src/FlashMessages.php | 7 ++++++- test/FlashMessagesTest.php | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/FlashMessages.php b/src/FlashMessages.php index c2d65ec..e910f88 100644 --- a/src/FlashMessages.php +++ b/src/FlashMessages.php @@ -92,12 +92,17 @@ public function flash(string $key, $value, int $hops = 1): void * using this method, you may make the value available during the current * request as well. * + * If you want the value to be visible only in the current request, you may + * pass zero as the third argument. + * * @param mixed $value */ public function flashNow(string $key, $value, int $hops = 1): void { $this->currentMessages[$key] = $value; - $this->flash($key, $value, $hops); + if ($hops > 0) { + $this->flash($key, $value, $hops); + } } /** diff --git a/test/FlashMessagesTest.php b/test/FlashMessagesTest.php index 530f9bc..ce2627f 100644 --- a/test/FlashMessagesTest.php +++ b/test/FlashMessagesTest.php @@ -365,7 +365,14 @@ public function testCreationAggregatesThrowsExceptionIfInvalidNumberOfHops() $this->anything() ); + $flash = FlashMessages::createFromSession($this->session); + $flash->flash('test', 'value', 0); + } + + public function testFlashNowAcceptsZeroHops() + { $flash = FlashMessages::createFromSession($this->session); $flash->flashNow('test', 'value', 0); + $this->assertSame('value', $flash->getFlash('test')); } } From d4d9670d2e111e8a61b1401b65c07ccc00e9e604 Mon Sep 17 00:00:00 2001 From: Tim Lieberman Date: Wed, 7 Apr 2021 15:22:17 -0700 Subject: [PATCH 2/4] Document change allowing zero hops value for flashNow() Signed-off-by: Tim Lieberman --- docs/book/messages.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/book/messages.md b/docs/book/messages.md index 0de9475..1aecc2b 100644 --- a/docs/book/messages.md +++ b/docs/book/messages.md @@ -136,6 +136,10 @@ the flash message will persist for. The default value is `1`, indicating a single hop. This value is provided when you call `flash()` as an optional third argument. +The `$hops` value passed to `flash()` must be greater than zero. Passing a value +less than one will result in `flash()` throwing an exception of type +`Mezzio\Flash\Exception\InvalidHopsValueException`. + To have a message persist for three "hops", you might call `flash()` as follows: ```php @@ -169,4 +173,10 @@ $flashMessages->flashNow($messageName, $messageValue); ``` The signature of this method is the same as for `flash()`, and allows you to -optionally provide a `$hops` value as well. +optionally provide a `$hops` value as well. Unlike `flash()`, `flashNow()` will +accept a value of zero for `$hops`, which is useful if you want your message to +be visible _exclusively_ in the current request. + +```php +$flashMessages->flashNow($messageName, 'One night (request) only!', 0); +``` From 9c346af4b931800cc13ac11bdeb98c18a6cb1986 Mon Sep 17 00:00:00 2001 From: Tim Lieberman Date: Wed, 14 Apr 2021 12:59:28 -0700 Subject: [PATCH 3/4] Explicitly test that nothing is set in session when flashNow receives 0 $hops Signed-off-by: Tim Lieberman --- test/FlashMessagesTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/FlashMessagesTest.php b/test/FlashMessagesTest.php index ce2627f..737a24a 100644 --- a/test/FlashMessagesTest.php +++ b/test/FlashMessagesTest.php @@ -371,8 +371,13 @@ public function testCreationAggregatesThrowsExceptionIfInvalidNumberOfHops() public function testFlashNowAcceptsZeroHops() { + $this->session + ->expects($this->never()) + ->method('set'); + $flash = FlashMessages::createFromSession($this->session); $flash->flashNow('test', 'value', 0); + $this->assertSame('value', $flash->getFlash('test')); } } From 3513492148621d34b293feff52346db896c98c28 Mon Sep 17 00:00:00 2001 From: Tim Lieberman Date: Wed, 14 Apr 2021 14:04:41 -0700 Subject: [PATCH 4/4] Split zero-hops test to be more explicit/focused. Signed-off-by: Tim Lieberman --- test/FlashMessagesTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/FlashMessagesTest.php b/test/FlashMessagesTest.php index 737a24a..a16fcdf 100644 --- a/test/FlashMessagesTest.php +++ b/test/FlashMessagesTest.php @@ -370,6 +370,14 @@ public function testCreationAggregatesThrowsExceptionIfInvalidNumberOfHops() } public function testFlashNowAcceptsZeroHops() + { + $flash = FlashMessages::createFromSession($this->session); + $flash->flashNow('test', 'value', 0); + + $this->assertSame('value', $flash->getFlash('test')); + } + + public function testFlashNowWithZeroHopsShouldNotSetValueToSession() { $this->session ->expects($this->never()) @@ -377,7 +385,5 @@ public function testFlashNowAcceptsZeroHops() $flash = FlashMessages::createFromSession($this->session); $flash->flashNow('test', 'value', 0); - - $this->assertSame('value', $flash->getFlash('test')); } }