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

Change what type of types can be assigned #12

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ $app->get('/bar', function ($req, $res, $args) {
$app->run();
```

> Please note that a message could be a string, object or array. Please check what your storage can handle.

## Testing

``` bash
Expand Down
6 changes: 3 additions & 3 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ public function __construct(&$storage = null)
* Add flash message
*
* @param string $key The key to store the message under
* @param string $message Message to show on next request
* @param mixed $message Message to show on next request
*/
public function addMessage($key, $message)
{
//Create Array for this key
if (!isset($this->storage[$this->storageKey][$key])) {
$this->storage[$this->storageKey][$key] = array();
}

//Push onto the array
$this->storage[$this->storageKey][$key][] = (string)$message;
$this->storage[$this->storageKey][$key][] = $message;
}

/**
Expand Down
58 changes: 58 additions & 0 deletions tests/MessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,64 @@ public function testGetMessagesFromPrevRequest()
$this->assertEquals(['Test'], $flash->getMessages());
}

// Test a string can be added to a message array for the next request
public function testAddMessageFromAnIntegerForNextRequest()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$flash->addMessage('key', 46);
$flash->addMessage('key', 48);

$this->assertArrayHasKey('slimFlash', $storage);
$this->assertEquals(['46', '48'], $storage['slimFlash']['key']);
}

// Test a string can be added to a message array for the next request
public function testAddMessageFromStringForNextRequest()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$flash->addMessage('key', 'value');

$this->assertArrayHasKey('slimFlash', $storage);
$this->assertEquals(['value'], $storage['slimFlash']['key']);
}

// Test an array can be added to a message array for the next request
public function testAddMessageFromArrayForNextRequest()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$formData = [
'username' => 'Scooby Doo',
'emailAddress' => 'scooby@mysteryinc.org',
];

$flash->addMessage('old', $formData);

$this->assertArrayHasKey('slimFlash', $storage);
$this->assertEquals($formData, $storage['slimFlash']['old'][0]);
}

// Test an object can be added to a message array for the next request
public function testAddMessageFromObjectForNextRequest()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$user = new \stdClass();
$user->name = 'Scooby Doo';
$user->emailAddress = 'scooby@mysteryinc.org';

$flash->addMessage('user', $user);

$this->assertArrayHasKey('slimFlash', $storage);
$this->assertInstanceOf(\stdClass::class, $storage['slimFlash']['user'][0]);
}

// Test get empty messages from previous request
public function testGetEmptyMessagesFromPrevRequest()
{
Expand Down