From 0d2745f750db638d0e63252b1c56ac55ff82abb6 Mon Sep 17 00:00:00 2001 From: Drak Date: Fri, 10 Feb 2012 15:38:21 +0545 Subject: [PATCH] [HttpFoundation] Remove constants from FlashBagInterface As requested by fabpot. Corrected a few mistakes in the documentation. --- CHANGELOG-2.1.md | 3 -- UPGRADE-2.1.md | 24 +++------- .../Templating/Helper/SessionHelperTest.php | 8 ++-- .../Session/Flash/FlashBagInterface.php | 5 -- .../Session/Flash/AutoExpireFlashBagTest.php | 48 +++++++++---------- .../Session/Flash/FlashBagTest.php | 44 ++++++++--------- 6 files changed, 56 insertions(+), 76 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index bcf7a4a4f1..69e632d86f 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -231,9 +231,6 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * made mimetype to extension conversion configurable * [BC BREAK] Moved all session related classes and interfaces into own namespace, as `Symfony\Component\HttpFoudation\Session` and renamed classes accordingly. - * Flashes are now stored as a bucket of messages per `$type` so there can be multiple messages per type. - There are four interface constants for type, `FlashBagInterface::INFO`, `FlashBagInterface::NOTICE`, - `FlashBagInterface::WARNING` and `FlashBagInterface::ERROR`. * Added `FlashBag` (default). Flashes expire when retrieved by `pop()` or `popAll()`. This makes the implementation ESI compatible. * Added `AutoExpireFlashBag` to replicate Symfony 2.0.x auto expire behaviour of messages auto expiring diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 8cccf48f00..76204f8bfd 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -259,21 +259,14 @@ UPGRADE FROM 2.0 to 2.1 - If you wanted to process all flash types you could also make use of the `getFlashes()->all()` API: + If you wanted to process all flash types you could also make use of the `getFlashes()->popAll()` API: - getFlashes()->all() as $type => $flash): ?> + getFlashes()->popAll() as $type => $flash): ?>
-.. note:: - - The Flash Message API provides constants which you can optionally use. For example - `Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE`, which can also be abbreviated to - `FlashBag::NOTICE` providing you declare `` - at the beginning of the PHP template. - Before (Twig): {% if app.session.hasFlash('notice') %} @@ -284,25 +277,20 @@ UPGRADE FROM 2.0 to 2.1 After (Twig): - {% if app.session.getFlashes.has('notice') %} + {% if app.session.getFlashes().has('notice') %}
- {{ app.session.getFlashes.pop('notice') }} + {{ app.session.getFlashes().pop('notice') }}
{% endif %} Again you can process all flash messages in one go with - {% for type, flashMessage in app.session.getFlashes.popAll() %} + {% for type, flashMessage in app.session.getFlashes().popAll() %}
{{ flashMessage }}
{% endforeach %} -.. note:: - - You can optionally use constants in Twig templates using `constant()` e.g. - `constant('Symfony\Component\HttpFoundation\Session\Flash\FlashBag::NOTICE')`. - * Session object The methods, `setFlash()`, `setFlashes()`, `getFlash()`, `hasFlash()`, and `removeFlash()` @@ -318,7 +306,7 @@ UPGRADE FROM 2.0 to 2.1 `StorageInterface`. Any session storage driver that wants to use custom save handlers should - implement `Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface` + implement `Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface` ### [FrameworkBundle] diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php index e492829897..b95161c2ec 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/SessionHelperTest.php @@ -28,7 +28,7 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase $session = new Session(new MockArrayStorage()); $session->set('foobar', 'bar'); - $session->getFlashes()->set(FlashBag::NOTICE, 'bar'); + $session->getFlashes()->set('notice', 'bar'); $this->request->setSession($session); } @@ -42,15 +42,15 @@ class SessionHelperTest extends \PHPUnit_Framework_TestCase { $helper = new SessionHelper($this->request); - $this->assertTrue($helper->hasFlash(FlashBag::NOTICE)); + $this->assertTrue($helper->hasFlash('notice')); - $this->assertEquals('bar', $helper->getFlash(FlashBag::NOTICE)); + $this->assertEquals('bar', $helper->getFlash('notice')); } public function testGetFlashes() { $helper = new SessionHelper($this->request); - $this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes()); + $this->assertEquals(array('notice' => 'bar'), $helper->getFlashes()); } public function testGet() diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php index 51b4927a6b..5cca797f7d 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBagInterface.php @@ -20,11 +20,6 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; */ interface FlashBagInterface extends SessionBagInterface { - const INFO = 'info'; - const NOTICE = 'notice'; - const WARNING = 'warning'; - const ERROR = 'error'; - /** * Registers a message for a given type. * diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php index b7850ef165..a81017c677 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/AutoExpireFlashBagTest.php @@ -35,7 +35,7 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->bag = new FlashBag(); - $this->array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); + $this->array = array('new' => array('notice' => 'A previous flash message')); $this->bag->initialize($this->array); } @@ -48,62 +48,62 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase public function testInitialize() { $bag = new FlashBag(); - $array = array('new' => array(FlashBag::NOTICE => 'A previous flash message')); + $array = array('new' => array('notice' => 'A previous flash message')); $bag->initialize($array); - $this->assertEquals('A previous flash message', $bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $bag->peek('notice')); $array = array('new' => array( - FlashBag::NOTICE => 'Something else', - FlashBag::ERROR => 'a', + 'notice' => 'Something else', + 'error' => 'a', )); $bag->initialize($array); - $this->assertEquals('Something else', $bag->peek(FlashBag::NOTICE)); - $this->assertEquals('a', $bag->peek(FlashBag::ERROR)); + $this->assertEquals('Something else', $bag->peek('notice')); + $this->assertEquals('a', $bag->peek('error')); } public function testPeek() { $this->assertNull($this->bag->peek('non_existing')); $this->assertEquals('default', $this->bag->peek('non_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); } public function testSet() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->assertNotEquals('Foo', $this->bag->peek(FlashBag::NOTICE)); + $this->bag->set('notice', 'Foo'); + $this->assertNotEquals('Foo', $this->bag->peek('notice')); } public function testHas() { $this->assertFalse($this->bag->has('nothing')); - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->assertTrue($this->bag->has('notice')); } public function testKeys() { - $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); + $this->assertEquals(array('notice'), $this->bag->keys()); } public function testPeekAll() { $array = array( 'new' => array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), ); $this->bag->initialize($array); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); } @@ -112,16 +112,16 @@ class AutoExpireFlashBagTest extends \PHPUnit_Framework_TestCase { $this->assertNull($this->bag->pop('non_existing')); $this->assertEquals('default', $this->bag->pop('non_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); - $this->assertNull($this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->pop('notice')); + $this->assertNull($this->bag->pop('notice')); } public function testPopAll() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::ERROR, 'Bar'); + $this->bag->set('notice', 'Foo'); + $this->bag->set('error', 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => 'A previous flash message', + 'notice' => 'A previous flash message', ), $this->bag->popAll() ); diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php index 8f173a7827..524106f509 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php @@ -35,7 +35,7 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->bag = new FlashBag(); - $this->array = array(FlashBag::NOTICE => 'A previous flash message'); + $this->array = array('notice' => 'A previous flash message'); $this->bag->initialize($this->array); } @@ -59,25 +59,25 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase { $this->assertNull($this->bag->peek('non_existing')); $this->assertEquals('default', $this->bag->peek('not_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); - $this->assertEquals('A previous flash message', $this->bag->peek(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); + $this->assertEquals('A previous flash message', $this->bag->peek('notice')); } public function testPop() { $this->assertNull($this->bag->pop('non_existing')); $this->assertEquals('default', $this->bag->pop('not_existing', 'default')); - $this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE)); - $this->assertNull($this->bag->pop(FlashBag::NOTICE)); + $this->assertEquals('A previous flash message', $this->bag->pop('notice')); + $this->assertNull($this->bag->pop('notice')); } public function testPopAll() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::ERROR, 'Bar'); + $this->bag->set('notice', 'Foo'); + $this->bag->set('error', 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar'), $this->bag->popAll() + 'notice' => 'Foo', + 'error' => 'Bar'), $this->bag->popAll() ); $this->assertEquals(array(), $this->bag->popAll()); @@ -85,36 +85,36 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase public function testSet() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::NOTICE, 'Bar'); - $this->assertEquals('Bar', $this->bag->peek(FlashBag::NOTICE)); + $this->bag->set('notice', 'Foo'); + $this->bag->set('notice', 'Bar'); + $this->assertEquals('Bar', $this->bag->peek('notice')); } public function testHas() { $this->assertFalse($this->bag->has('nothing')); - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); + $this->assertTrue($this->bag->has('notice')); } public function testKeys() { - $this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys()); + $this->assertEquals(array('notice'), $this->bag->keys()); } public function testPeekAll() { - $this->bag->set(FlashBag::NOTICE, 'Foo'); - $this->bag->set(FlashBag::ERROR, 'Bar'); + $this->bag->set('notice', 'Foo'); + $this->bag->set('error', 'Bar'); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); - $this->assertTrue($this->bag->has(FlashBag::NOTICE)); - $this->assertTrue($this->bag->has(FlashBag::ERROR)); + $this->assertTrue($this->bag->has('notice')); + $this->assertTrue($this->bag->has('error')); $this->assertEquals(array( - FlashBag::NOTICE => 'Foo', - FlashBag::ERROR => 'Bar', + 'notice' => 'Foo', + 'error' => 'Bar', ), $this->bag->peekAll() ); }