Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flash messages for current request #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class Messages
*/
protected $fromPrevious = [];

/**
* Messages for current request
*
* @var string[]
*/
protected $forNow = [];

/**
* Messages for next request
*
Expand Down Expand Up @@ -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
Expand All @@ -83,14 +90,43 @@ 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
*
* @return array Messages to show for current request
*/
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;
}

/**
Expand All @@ -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;
}
}
101 changes: 101 additions & 0 deletions tests/MessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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'));
}
}