Skip to content

Commit

Permalink
Add danger() method to FlashMessage and make error() an alias for dan…
Browse files Browse the repository at this point in the history
…ger()

Fixes #313
  • Loading branch information
Florian Eckerstorfer committed Oct 24, 2014
1 parent f4ba62a commit c91ec7e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Resources/views/flash.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>
{% endfor %}

{% for flashMessage in app.session.flashbag.get('error') %}
{% for flashMessage in app.session.flashbag.get('danger') %}
<div class="alert alert-danger">
{% if close %}<button type="button" class="close" data-dismiss="alert">&times;</button>{% endif %}
{{ flashMessage|trans({}, translation_domain) }}
Expand Down
26 changes: 13 additions & 13 deletions Session/FlashMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,36 @@ public function __construct(SessionInterface $session)
* Sets an alert message.
*
* @param string $message The message
*
* @return void
*/
public function alert($message)
{
$this->session->getFlashBag()->add('alert', $message);
}

/**
* Sets an error message.
* Alias for `danger()`.
*
* @param string $message The message
*
* @return void
*/
public function error($message)
{
$this->session->getFlashBag()->add('error', $message);
$this->danger($message);
}

/**
* Sets a danger message.
*
* @param $message
*/
public function danger($message)
{
$this->session->getFlashBag()->add('danger', $message);
}

/**
* Sets an info message.
*
* @param string $message The message
*
* @return void
*/
public function info($message)
{
Expand All @@ -74,18 +78,14 @@ public function info($message)
* Sets a success message.
*
* @param string $message The message
*
* @return void
*/
public function success($message)
{
$this->session->getFlashBag()->add('success', $message);
}

/**
* Resets the flash bag.
*
* @return void
*/
public function reset()
{
Expand Down
22 changes: 19 additions & 3 deletions Tests/Session/FlashMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*/
class FlashMessageTest extends \PHPUnit_Framework_TestCase
{
/** @var \Symfony\Component\HttpFoundation\Session\SessionInterface */
/** @var \Symfony\Component\HttpFoundation\Session\SessionInterface|\Mockery\MockInterface */
private $session;

/** @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface */
/** @var \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface|\Mockery\MockInterface */
private $flashBag;

/** @var FlashMessage */
Expand Down Expand Up @@ -61,12 +61,28 @@ public function testError()
{
$this->flashBag
->shouldReceive('add')
->with('error', 'Foobar Error')
->with('danger', 'Foobar Error')
->once();

$this->flash->error('Foobar Error');
}

/**
* Tests the danger() method.
*
* @covers Braincrafted\Bundle\BootstrapBundle\Session\FlashMessage::__construct()
* @covers Braincrafted\Bundle\BootstrapBundle\Session\FlashMessage::danger()
*/
public function testDanger()
{
$this->flashBag
->shouldReceive('add')
->with('danger', 'Foobar Error')
->once();

$this->flash->danger('Foobar Error');
}

/**
* Tests the info() method.
*
Expand Down

0 comments on commit c91ec7e

Please sign in to comment.