Skip to content

Commit

Permalink
Implement clearMessage & clearMessages()
Browse files Browse the repository at this point in the history
Based on MrSam's code in #30.
  • Loading branch information
akrabat committed Oct 22, 2017
1 parent 3018642 commit dc82bd3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,40 @@ public function hasMessage($key)
$messages = $this->getMessages();
return isset($messages[$key]);
}

/**
* Clear all messages
*
* @return void
*/
public function clearMessages()
{
if (isset($this->storage[$this->storageKey])) {
$this->storage[$this->storageKey] = [];
}

$this->fromPrevious = [];
$this->forNow = [];
}

/**
* Clear specific message
*
* @param String $key The key to clear
* @return void
*/
public function clearMessage($key)
{
if (isset($this->storage[$this->storageKey][$key])) {
unset($this->storage[$this->storageKey][$key]);
}

if (isset($this->fromPrevious[$key])) {
unset($this->fromPrevious[$key]);
}

if (isset($this->forNow[$key])) {
unset($this->forNow[$key]);
}
}
}
35 changes: 35 additions & 0 deletions tests/MessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,39 @@ public function testHasMessage()
$flash = new Messages($storage);
$this->assertTrue($flash->hasMessage('Test'));
}

public function testClearMessages()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$storage = ['slimFlash' => [ 'Test' => ['Test']]];
$flash = new Messages($storage);
$flash->addMessageNow('Now', 'hear this');
$this->assertTrue($flash->hasMessage('Test'));
$this->assertTrue($flash->hasMessage('Now'));

$flash->clearMessages();
$this->assertFalse($flash->hasMessage('Test'));
$this->assertFalse($flash->hasMessage('Now'));
}

public function testClearMessage()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$storage = ['slimFlash' => [ 'Test' => ['Test'], 'Foo' => ['Bar']]];
$flash = new Messages($storage);
$flash->addMessageNow('Now', 'hear this');
$this->assertTrue($flash->hasMessage('Test'));
$this->assertTrue($flash->hasMessage('Foo'));
$this->assertTrue($flash->hasMessage('Now'));

$flash->clearMessage('Test');
$flash->clearMessage('Now');
$this->assertFalse($flash->hasMessage('Test'));
$this->assertFalse($flash->hasMessage('Now'));
$this->assertTrue($flash->hasMessage('Foo'));
}
}

0 comments on commit dc82bd3

Please sign in to comment.