From 6a64957d7486aa6b8fcfee1c05d7015d70414af6 Mon Sep 17 00:00:00 2001 From: thinksalot Date: Sat, 9 Jan 2016 08:25:50 +0545 Subject: [PATCH] flash messages for current request --- src/Messages.php | 43 ++++++++++++++++-- tests/MessagesTest.php | 101 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 3 deletions(-) diff --git a/src/Messages.php b/src/Messages.php index 4133fff..20e7b54 100644 --- a/src/Messages.php +++ b/src/Messages.php @@ -17,6 +17,13 @@ class Messages */ protected $fromPrevious = []; + /** + * Messages for current request + * + * @var string[] + */ + protected $forNow = []; + /** * Messages for next request * @@ -67,7 +74,7 @@ public function __construct(&$storage = null) } /** - * Add flash message + * Add flash message for the next request * * @param string $key The key to store the message under * @param mixed $message Message to show on next request @@ -83,6 +90,23 @@ public function addMessage($key, $message) $this->storage[$this->storageKey][$key][] = $message; } + /** + * Add flash message for current request + * + * @param string $key The key to store the message under + * @param mixed $message Message to show on next request + */ + public function addMessageNow($key, $message){ + + //Create Array for this key + if(!isset($this->forNow[$key])){ + $this->forNow[$key] = array(); + } + + //Push onto the array + $this->forNow[$key][] = $message; + } + /** * Get flash messages * @@ -90,7 +114,19 @@ public function addMessage($key, $message) */ public function getMessages() { - return $this->fromPrevious; + $messages = $this->fromPrevious; + + foreach($this->forNow as $key => $values){ + if(!isset($messages[$key])){ + $messages[$key] = array(); + } + + foreach($values as $value){ + array_push($messages[$key], $value); + } + } + + return $messages; } /** @@ -101,7 +137,8 @@ public function getMessages() */ public function getMessage($key) { + $messages = $this->getMessages(); //If the key exists then return all messages or null - return (isset($this->fromPrevious[$key])) ? $this->fromPrevious[$key] : null; + return (isset($messages[$key])) ? $messages[$key] : null; } } diff --git a/tests/MessagesTest.php b/tests/MessagesTest.php index 37b2506..a01764a 100644 --- a/tests/MessagesTest.php +++ b/tests/MessagesTest.php @@ -16,6 +16,76 @@ public function testGetMessagesFromPrevRequest() $this->assertEquals(['Test'], $flash->getMessages()); } + // Test a string can be added to a message array for the current request + public function testAddMessageFromAnIntegerForCurrentRequest() + { + $storage = ['slimFlash' => []]; + $flash = new Messages($storage); + + $flash->addMessageNow('key', 46); + $flash->addMessageNow('key', 48); + + $messages = $flash->getMessages(); + $this->assertEquals(['46','48'], $messages['key']); + + $this->assertArrayHasKey('slimFlash', $storage); + $this->assertEmpty($storage['slimFlash']); + } + + // Test a string can be added to a message array for the current request + public function testAddMessageFromStringForCurrentRequest() + { + $storage = ['slimFlash' => []]; + $flash = new Messages($storage); + + $flash->addMessageNow('key', 'value'); + + $messages = $flash->getMessages(); + $this->assertEquals(['value'], $messages['key']); + + $this->assertArrayHasKey('slimFlash', $storage); + $this->assertEmpty($storage['slimFlash']); + } + + // Test an array can be added to a message array for the current request + public function testAddMessageFromArrayForCurrentRequest() + { + $storage = ['slimFlash' => []]; + $flash = new Messages($storage); + + $formData = [ + 'username' => 'Scooby Doo', + 'emailAddress' => 'scooby@mysteryinc.org', + ]; + + $flash->addMessageNow('old', $formData); + + $messages = $flash->getMessages(); + $this->assertEquals($formData, $messages['old'][0]); + + $this->assertArrayHasKey('slimFlash', $storage); + $this->assertEmpty($storage['slimFlash']); + } + + // Test an object can be added to a message array for the current request + public function testAddMessageFromObjectForCurrentRequest() + { + $storage = ['slimFlash' => []]; + $flash = new Messages($storage); + + $user = new \stdClass(); + $user->name = 'Scooby Doo'; + $user->emailAddress = 'scooby@mysteryinc.org'; + + $flash->addMessageNow('user', $user); + + $messages = $flash->getMessages(); + $this->assertInstanceOf(\stdClass::class, $messages['user'][0]); + + $this->assertArrayHasKey('slimFlash', $storage); + $this->assertEmpty($storage['slimFlash']); + } + // Test a string can be added to a message array for the next request public function testAddMessageFromAnIntegerForNextRequest() { @@ -83,6 +153,25 @@ public function testGetEmptyMessagesFromPrevRequest() $this->assertEquals([], $flash->getMessages()); } + // Test set messages for current request + public function testSetMessagesForCurrentRequest() + { + $storage = ['slimFlash' => [ 'error' => ['An error']]]; + + $flash = new Messages($storage); + $flash->addMessageNow('error', 'Another error'); + $flash->addMessageNow('success', 'A success'); + $flash->addMessageNow('info', 'An info'); + + $messages = $flash->getMessages(); + $this->assertEquals(['An error', 'Another error'], $messages['error']); + $this->assertEquals(['A success'], $messages['success']); + $this->assertEquals(['An info'], $messages['info']); + + $this->assertArrayHasKey('slimFlash', $storage); + $this->assertEmpty([], $storage['slimFlash']); + } + // Test set messages for next request public function testSetMessagesForNextRequest() { @@ -104,4 +193,16 @@ public function testGetMessageFromKey() $this->assertEquals(['Test', 'Test2'], $flash->getMessage('Test')); } + + //Test getting the message from the key + public function testGetMessageFromKeyIncludingCurrent() + { + $storage = ['slimFlash' => [ 'Test' => ['Test', 'Test2']]]; + $flash = new Messages($storage); + $flash->addMessageNow('Test', 'Test3'); + + $messages = $flash->getMessages(); + + $this->assertEquals(['Test', 'Test2','Test3'], $flash->getMessage('Test')); + } }