From 7390a1e0dc7f0f1135f809ef76bccafad701e329 Mon Sep 17 00:00:00 2001 From: Drak Date: Fri, 4 Nov 2011 23:15:51 +0545 Subject: [PATCH 1/5] [HttpFoundation] Move flash messages out of Session class and change to bucket system. --- .../Component/HttpFoundation/FlashBag.php | 150 ++++++++++++++++++ .../HttpFoundation/FlashBagInterface.php | 81 ++++++++++ .../Component/HttpFoundation/Session.php | 140 ++++------------ 3 files changed, 261 insertions(+), 110 deletions(-) create mode 100644 src/Symfony/Component/HttpFoundation/FlashBag.php create mode 100644 src/Symfony/Component/HttpFoundation/FlashBagInterface.php diff --git a/src/Symfony/Component/HttpFoundation/FlashBag.php b/src/Symfony/Component/HttpFoundation/FlashBag.php new file mode 100644 index 0000000000000..645c3a507ee2c --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/FlashBag.php @@ -0,0 +1,150 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * FlashBag flash message container. + */ +class FlashBag implements FlashBagInterface +{ + /** + * Flash messages. + * + * @var array + */ + private $flashes = array(); + + /** + * Old flash messages to be purged. + * + * @var array + */ + private $oldFlashes = array(); + + /** + * @var boolean + */ + private $initialized = false; + + /** + * Initializes the FlashBag. + * + * @param array $flashes + */ + public function initialize(array $flashes) + { + if ($this->initialized) { + return; + } + + $this->flashes = $flashes; + $this->oldFlashes = $flashes; + $this->initialized = true; + } + + /** + * Adds a flash to the stack for a given type. + */ + public function add($type, $message) + { + $this->flashes[$type][] = $message; + } + + /** + * Gets flashes for a given type. + * + * @return array + */ + public function get($type) + { + if (!$this->has($type)) { + throw new \InvalidArgumentException(sprintf('Specified $type %s does not exist')); + } + + return $this->flashes[$type]; + } + + /** + * Sets an array of flash messages for a given type. + * + * @param string $type + * @param array $array + */ + public function set($type, array $array) + { + $this->flashes[$type] = $array; + } + + /** + * Has messages for a given type? + * + * @return boolean + */ + public function has($type) + { + return array_key_exists($type, $this->flashes); + } + + /** + * Returns a list of all defined types. + * + * @return array + */ + public function getTypes() + { + return array_keys($this->flashes); + } + + /** + * Gets all flashes. + * + * @return array + */ + public function all() + { + return $this->flashes; + } + + /** + * Clears flash messages for a given type. + */ + public function clear($type) + { + if (isset($this->flashes[$type])) { + unset($this->flashes[$type]); + } + + if (isset($this->oldFlashes[$type])) { + unset($this->oldFlashes[$type]); + } + } + + /** + * Clears all flash messages. + */ + public function clearAll() + { + $this->flashes = array(); + $this->oldFlashes = array(); + } + + /** + * Removes flash messages set in a previous request. + */ + public function purgeOldFlashes() + { + foreach ($this->oldFlashes as $type => $flashes) { + $this->flashes[$type] = array_diff_key($flashes[$type], $this->oldFlashes[$type]); + } + } + +} diff --git a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php new file mode 100644 index 0000000000000..f76b6219f76d7 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation; + +/** + * + */ +interface FlashBagInterface +{ + /** + * Initializes the FlashBag. + * + * @param array $flashes + */ + public function initialize(array $flashes); + + /** + * Adds a flash to the stack for a given type. + */ + public function add($type, $message); + + /** + * Gets flash messages for a given type. + * + * @return array + */ + public function get($type); + + /** + * Sets an array of flash messages for a given type. + * + * @param string $type + * @param array $array + */ + public function set($type, array $array); + + /** + * Hass flash messages for a given type? + * + * @return boolean + */ + public function has($type); + + /** + * Returns a list of all defined types. + * + * @return array + */ + public function getTypes(); + + /** + * Gets all flash messages. + * + * @return array + */ + public function all(); + + /** + * Clears flash messages for a given type. + */ + public function clear($type); + + /** + * Clears all flash messages. + */ + public function clearAll(); + + /** + * Removes flash messages set in a previous request. + */ + public function purgeOldFlashes(); +} diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index 721a6c7240b99..1bc26457493a7 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -25,24 +25,43 @@ class Session implements \Serializable protected $storage; protected $started; protected $attributes; + + /** + * Flash message container. + * + * @var \Symfony\Component\HttpFoundation\FlashBagInterface + */ protected $flashes; - protected $oldFlashes; protected $closed; - + /** * Constructor. * - * @param SessionStorageInterface $storage A SessionStorageInterface instance + * @param SessionStorageInterface $storage A SessionStorageInterface instance. + * @param FlashBagInterface $flashes A FlashBagInterface instance. */ - public function __construct(SessionStorageInterface $storage) + public function __construct(SessionStorageInterface $storage, FlashBagInterface $flashes) { $this->storage = $storage; - $this->flashes = array(); - $this->oldFlashes = array(); + $this->flashes = $flashes; $this->attributes = array(); $this->started = false; $this->closed = false; } + + /** + * Get the flashbag driver. + * + * @return FlashBagInterface + */ + public function getFlashBag() + { + if (false === $this->started) { + $this->start(); + } + + return $this->flashes; + } /** * Starts the session storage. @@ -61,10 +80,7 @@ public function start() if (isset($attributes['attributes'])) { $this->attributes = $attributes['attributes']; - $this->flashes = $attributes['flashes']; - - // flag current flash messages to be removed at shutdown - $this->oldFlashes = $this->flashes; + $this->flashes->initialize($attributes['flashes']); } $this->started = true; @@ -174,7 +190,7 @@ public function clear() } $this->attributes = array(); - $this->flashes = array(); + $this->flashes->clearAll(); } /** @@ -215,114 +231,18 @@ public function getId() return $this->storage->getId(); } - /** - * Gets the flash messages. - * - * @return array - */ - public function getFlashes() - { - return $this->flashes; - } - - /** - * Sets the flash messages. - * - * @param array $values - */ - public function setFlashes($values) - { - if (false === $this->started) { - $this->start(); - } - - $this->flashes = $values; - $this->oldFlashes = array(); - } - - /** - * Gets a flash message. - * - * @param string $name - * @param string|null $default - * - * @return string - */ - public function getFlash($name, $default = null) - { - return array_key_exists($name, $this->flashes) ? $this->flashes[$name] : $default; - } - - /** - * Sets a flash message. - * - * @param string $name - * @param string $value - */ - public function setFlash($name, $value) - { - if (false === $this->started) { - $this->start(); - } - - $this->flashes[$name] = $value; - unset($this->oldFlashes[$name]); - } - - /** - * Checks whether a flash message exists. - * - * @param string $name - * - * @return Boolean - */ - public function hasFlash($name) - { - if (false === $this->started) { - $this->start(); - } - - return array_key_exists($name, $this->flashes); - } - - /** - * Removes a flash message. - * - * @param string $name - */ - public function removeFlash($name) - { - if (false === $this->started) { - $this->start(); - } - - unset($this->flashes[$name]); - } - - /** - * Removes the flash messages. - */ - public function clearFlashes() - { - if (false === $this->started) { - $this->start(); - } - - $this->flashes = array(); - $this->oldFlashes = array(); - } - public function save() { if (false === $this->started) { $this->start(); } - $this->flashes = array_diff_key($this->flashes, $this->oldFlashes); + // expire old flashes + $this->flashes->purgeOldFlashes(); $this->storage->write('_symfony2', array( 'attributes' => $this->attributes, - 'flashes' => $this->flashes, + 'flashes' => $this->flashes->all(), )); } From b60e84ebfcdcc2ecd7caf59587aa79d294dfc8b4 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 10 Nov 2011 15:29:06 +0545 Subject: [PATCH 2/5] [HttpFoundation] Added some tests and fixed logic and CS accordingly. --- CHANGELOG-2.1.md | 4 +- .../Component/HttpFoundation/FlashBag.php | 4 +- .../HttpFoundation/FlashBagInterface.php | 22 ++++--- .../Component/HttpFoundation/Session.php | 20 +++--- .../Component/HttpFoundation/SessionTest.php | 63 ++++++------------- 5 files changed, 47 insertions(+), 66 deletions(-) diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index 087085e2d0de0..e0c67c1a9dc36 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -88,6 +88,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * removed the ContentTypeMimeTypeGuesser class as it is deprecated and never used on PHP 5.3 * added ResponseHeaderBag::makeDisposition() (implements RFC 6266) * made mimetype to extension conversion configurable + * [BC BREAK] Flashes are now stored as a bucket of messages per $type. Moved flash messages + out of the session class. Must use $session->getFlashBag() to get FlashBag instance. ### HttpKernel @@ -117,4 +119,4 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c * added a Size validator * added a SizeLength validator * improved the ImageValidator with min width, max width, min height, and max height constraints - * added support for MIME with wildcard in FileValidator + * added support for MIME with wildcard in FileValidator \ No newline at end of file diff --git a/src/Symfony/Component/HttpFoundation/FlashBag.php b/src/Symfony/Component/HttpFoundation/FlashBag.php index 645c3a507ee2c..7e669c75e892e 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/FlashBag.php @@ -67,7 +67,7 @@ public function add($type, $message) public function get($type) { if (!$this->has($type)) { - throw new \InvalidArgumentException(sprintf('Specified $type %s does not exist')); + throw new \InvalidArgumentException(sprintf('Specified $type %s does not exist', $type)); } return $this->flashes[$type]; @@ -143,7 +143,7 @@ public function clearAll() public function purgeOldFlashes() { foreach ($this->oldFlashes as $type => $flashes) { - $this->flashes[$type] = array_diff_key($flashes[$type], $this->oldFlashes[$type]); + $this->flashes[$type] = array_diff($this->flashes[$type], $flashes); } } diff --git a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php index f76b6219f76d7..47d2d2603c52e 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBagInterface.php +++ b/src/Symfony/Component/HttpFoundation/FlashBagInterface.php @@ -12,7 +12,9 @@ namespace Symfony\Component\HttpFoundation; /** + * FlashBagInterface. * + * @author Drak */ interface FlashBagInterface { @@ -21,19 +23,19 @@ interface FlashBagInterface * * @param array $flashes */ - public function initialize(array $flashes); + function initialize(array $flashes); /** * Adds a flash to the stack for a given type. */ - public function add($type, $message); + function add($type, $message); /** * Gets flash messages for a given type. * * @return array */ - public function get($type); + function get($type); /** * Sets an array of flash messages for a given type. @@ -41,41 +43,41 @@ public function get($type); * @param string $type * @param array $array */ - public function set($type, array $array); + function set($type, array $array); /** * Hass flash messages for a given type? * * @return boolean */ - public function has($type); + function has($type); /** * Returns a list of all defined types. * * @return array */ - public function getTypes(); + function getTypes(); /** * Gets all flash messages. * * @return array */ - public function all(); + function all(); /** * Clears flash messages for a given type. */ - public function clear($type); + function clear($type); /** * Clears all flash messages. */ - public function clearAll(); + function clearAll(); /** * Removes flash messages set in a previous request. */ - public function purgeOldFlashes(); + function purgeOldFlashes(); } diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index 1bc26457493a7..589ec8ad82fcd 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -31,19 +31,19 @@ class Session implements \Serializable * * @var \Symfony\Component\HttpFoundation\FlashBagInterface */ - protected $flashes; + protected $flashBag; protected $closed; /** * Constructor. * - * @param SessionStorageInterface $storage A SessionStorageInterface instance. - * @param FlashBagInterface $flashes A FlashBagInterface instance. + * @param SessionStorageInterface $storage A SessionStorageInterface instance. + * @param FlashBagInterface $flashBag A FlashBagInterface instance. */ - public function __construct(SessionStorageInterface $storage, FlashBagInterface $flashes) + public function __construct(SessionStorageInterface $storage, FlashBagInterface $flashBag = null) { $this->storage = $storage; - $this->flashes = $flashes; + $this->flashBag = $flashBag; $this->attributes = array(); $this->started = false; $this->closed = false; @@ -60,7 +60,7 @@ public function getFlashBag() $this->start(); } - return $this->flashes; + return $this->flashBag; } /** @@ -80,7 +80,7 @@ public function start() if (isset($attributes['attributes'])) { $this->attributes = $attributes['attributes']; - $this->flashes->initialize($attributes['flashes']); + $this->flashBag->initialize($attributes['flashes']); } $this->started = true; @@ -190,7 +190,7 @@ public function clear() } $this->attributes = array(); - $this->flashes->clearAll(); + $this->flashBag->clearAll(); } /** @@ -238,11 +238,11 @@ public function save() } // expire old flashes - $this->flashes->purgeOldFlashes(); + $this->flashBag->purgeOldFlashes(); $this->storage->write('_symfony2', array( 'attributes' => $this->attributes, - 'flashes' => $this->flashes->all(), + 'flashes' => $this->flashBag->all(), )); } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php index 8318101e665d1..b8721bc98d779 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php @@ -12,6 +12,8 @@ namespace Symfony\Tests\Component\HttpFoundation; use Symfony\Component\HttpFoundation\Session; +use Symfony\Component\HttpFoundation\FlashBag; +use Symfony\Component\HttpFoundation\FlashBagInterface; use Symfony\Component\HttpFoundation\SessionStorage\ArraySessionStorage; /** @@ -24,54 +26,29 @@ class SessionTest extends \PHPUnit_Framework_TestCase { protected $storage; protected $session; + + /** + * @var \Symfony\Component\HttpFoundation\FlashBagInterface + */ + protected $flashBag; public function setUp() { $this->storage = new ArraySessionStorage(); + $this->flashBag = new FlashBag(); $this->session = $this->getSession(); } protected function tearDown() { $this->storage = null; + $this->flashBag = null; $this->session = null; } - - public function testFlash() + + public function getFlashBag() { - $this->session->clearFlashes(); - - $this->assertSame(array(), $this->session->getFlashes()); - - $this->assertFalse($this->session->hasFlash('foo')); - - $this->session->setFlash('foo', 'bar'); - - $this->assertTrue($this->session->hasFlash('foo')); - $this->assertSame('bar', $this->session->getFlash('foo')); - - $this->session->removeFlash('foo'); - - $this->assertFalse($this->session->hasFlash('foo')); - - $flashes = array('foo' => 'bar', 'bar' => 'foo'); - - $this->session->setFlashes($flashes); - - $this->assertSame($flashes, $this->session->getFlashes()); - } - - public function testFlashesAreFlushedWhenNeeded() - { - $this->session->setFlash('foo', 'bar'); - $this->session->save(); - - $this->session = $this->getSession(); - $this->assertTrue($this->session->hasFlash('foo')); - $this->session->save(); - - $this->session = $this->getSession(); - $this->assertFalse($this->session->hasFlash('foo')); + $this->assetTrue($this->getFlashBag() instanceof FlashBagInterface); } public function testAll() @@ -109,26 +86,26 @@ public function testAll() public function testMigrateAndInvalidate() { $this->session->set('foo', 'bar'); - $this->session->setFlash('foo', 'bar'); + $this->session->getFlashBag()->set('foo', array('bar')); $this->assertSame('bar', $this->session->get('foo')); - $this->assertSame('bar', $this->session->getFlash('foo')); + $this->assertEquals(array('bar'), $this->session->getFlashBag()->get('foo')); $this->session->migrate(); $this->assertSame('bar', $this->session->get('foo')); - $this->assertSame('bar', $this->session->getFlash('foo')); + $this->assertEquals(array('bar'), $this->session->getFlashBag()->get('foo')); $this->session = $this->getSession(); $this->session->invalidate(); $this->assertSame(array(), $this->session->all()); - $this->assertSame(array(), $this->session->getFlashes()); + $this->assertEquals(array(), $this->session->getFlashBag()->all()); } public function testSerialize() { - $this->session = new Session($this->storage); + $this->session = new Session($this->storage, $this->flashBag); $compare = serialize($this->storage); @@ -145,7 +122,7 @@ public function testSerialize() public function testSave() { $this->storage = new ArraySessionStorage(); - $this->session = new Session($this->storage); + $this->session = new Session($this->storage, $this->flashBag); $this->session->set('foo', 'bar'); $this->session->save(); @@ -167,7 +144,7 @@ public function testStart() { $this->session->start(); - $this->assertSame(array(), $this->session->getFlashes()); + $this->assertSame(array(), $this->session->getFlashBag()->all()); $this->assertSame(array(), $this->session->all()); } @@ -227,6 +204,6 @@ public function testStorageRemove() protected function getSession() { - return new Session($this->storage); + return new Session($this->storage, $this->flashBag); } } From 1b3d8d497766dc2a7ac0b241cfa4e5a72b2ca228 Mon Sep 17 00:00:00 2001 From: Drak Date: Sun, 13 Nov 2011 14:00:49 +0545 Subject: [PATCH 3/5] [HttpFoundation] Added tests. --- .../Component/HttpFoundation/FlashBagTest.php | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php diff --git a/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php new file mode 100644 index 0000000000000..754bb80e7e561 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php @@ -0,0 +1,122 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Component\HttpFoundation; + +use Symfony\Component\HttpFoundation\FlashBag; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * FlashBagTest + * + * @author Drak + */ +class FlashBagTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Symfony\Component\HttpFoundation\FlashBagInterface + */ + private $flashBag; + + public function setUp() + { + parent::setUp(); + $this->flashBag = new FlashBag(); + $this->flashBag->initialize(array('status' => array('A previous flash message'))); + } + + public function tearDown() + { + $this->flashBag = null; + parent::tearDown(); + } + + public function testInitialize() + { + $this->flashBag->initialize(array()); + $this->flashBag->initialize(array()); + } + + /** + * @todo Implement testAdd(). + */ + public function testAdd() + { + $this->flashBag->add('status', 'Something new'); + $this->flashBag->add('error', 'Smile, it might work next time'); + $this->assertEquals(array('A previous flash message', 'Something new'), $this->flashBag->get('status')); + $this->assertEquals(array('Smile, it might work next time'), $this->flashBag->get('error')); + } + + public function testGet() + { + $this->assertEquals(array('A previous flash message'), $this->flashBag->get('status')); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testGetException() + { + $bang = $this->flashBag->get('bang'); + } + + public function testSet() + { + $this->flashBag->set('status', array('Foo', 'Bar')); + $this->assertEquals(array('Foo', 'Bar'), $this->flashBag->get('status')); + } + + public function testHas() + { + $this->assertFalse($this->flashBag->has('nothing')); + $this->assertTrue($this->flashBag->has('status')); + } + + /** + * @todo Implement testGetTypes(). + */ + public function testGetTypes() + { + $this->assertEquals(array('status'), $this->flashBag->getTypes()); + } + + public function testAll() + { + // nothing to do here + } + + public function testClear() + { + $this->assertTrue($this->flashBag->has('status')); + $this->flashBag->clear('status'); + $this->assertFalse($this->flashBag->has('status')); + } + + public function testClearAll() + { + $this->assertTrue($this->flashBag->has('status')); + $this->flashBag->add('error', 'Smile, it might work next time'); + $this->assertTrue($this->flashBag->has('error')); + $this->flashBag->clearAll(); + $this->assertFalse($this->flashBag->has('status')); + $this->assertFalse($this->flashBag->has('error')); + } + + public function testPurgeOldFlashes() + { + $this->flashBag->add('status', 'Foo'); + $this->flashBag->add('error', 'Bar'); + $this->flashBag->purgeOldFlashes(); + $this->assertEquals(array(1 => 'Foo'), $this->flashBag->get('status')); + } + +} \ No newline at end of file From cedc1f01c2a05f2ddce839674ae2a731cdfe9131 Mon Sep 17 00:00:00 2001 From: Drak Date: Mon, 14 Nov 2011 09:17:51 +0545 Subject: [PATCH 4/5] [HttpFoundation] remove optional driver for now. --- src/Symfony/Component/HttpFoundation/Session.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Session.php b/src/Symfony/Component/HttpFoundation/Session.php index 589ec8ad82fcd..46be05925d060 100644 --- a/src/Symfony/Component/HttpFoundation/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session.php @@ -40,7 +40,7 @@ class Session implements \Serializable * @param SessionStorageInterface $storage A SessionStorageInterface instance. * @param FlashBagInterface $flashBag A FlashBagInterface instance. */ - public function __construct(SessionStorageInterface $storage, FlashBagInterface $flashBag = null) + public function __construct(SessionStorageInterface $storage, FlashBagInterface $flashBag) { $this->storage = $storage; $this->flashBag = $flashBag; From 91342f77997437e6d1bccf2879e3771830e10c34 Mon Sep 17 00:00:00 2001 From: Drak Date: Mon, 21 Nov 2011 20:12:27 +0545 Subject: [PATCH 5/5] [HttpFoundation] Added some sensible defaults for flash types. Changed the order of addFlash() a sensible default. --- .gitignore | 1 + .../Component/HttpFoundation/FlashBag.php | 8 +++- .../Component/HttpFoundation/FlashBagTest.php | 40 +++++++++---------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 422f1ce840571..e75f9a112448a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ phpunit.xml autoload.php /vendor/ +/nbproject/private/ \ No newline at end of file diff --git a/src/Symfony/Component/HttpFoundation/FlashBag.php b/src/Symfony/Component/HttpFoundation/FlashBag.php index 7e669c75e892e..7d4420a4e7047 100644 --- a/src/Symfony/Component/HttpFoundation/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/FlashBag.php @@ -16,6 +16,9 @@ */ class FlashBag implements FlashBagInterface { + const STATUS = 'status'; + const ERROR = 'error'; + /** * Flash messages. * @@ -53,8 +56,11 @@ public function initialize(array $flashes) /** * Adds a flash to the stack for a given type. + * + * @param string $message Message. + * @param string $type Message category */ - public function add($type, $message) + public function add($message, $type = self::STATUS) { $this->flashes[$type][] = $message; } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php index 754bb80e7e561..c053e0d007e11 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/FlashBagTest.php @@ -50,15 +50,15 @@ public function testInitialize() */ public function testAdd() { - $this->flashBag->add('status', 'Something new'); - $this->flashBag->add('error', 'Smile, it might work next time'); - $this->assertEquals(array('A previous flash message', 'Something new'), $this->flashBag->get('status')); - $this->assertEquals(array('Smile, it might work next time'), $this->flashBag->get('error')); + $this->flashBag->add('Something new', FlashBag::STATUS); + $this->flashBag->add('Smile, it might work next time', FlashBag::ERROR); + $this->assertEquals(array('A previous flash message', 'Something new'), $this->flashBag->get(FlashBag::STATUS)); + $this->assertEquals(array('Smile, it might work next time'), $this->flashBag->get(FlashBag::ERROR)); } public function testGet() { - $this->assertEquals(array('A previous flash message'), $this->flashBag->get('status')); + $this->assertEquals(array('A previous flash message'), $this->flashBag->get(FlashBag::STATUS)); } /** @@ -71,14 +71,14 @@ public function testGetException() public function testSet() { - $this->flashBag->set('status', array('Foo', 'Bar')); - $this->assertEquals(array('Foo', 'Bar'), $this->flashBag->get('status')); + $this->flashBag->set(FlashBag::STATUS, array('Foo', 'Bar')); + $this->assertEquals(array('Foo', 'Bar'), $this->flashBag->get(FlashBag::STATUS)); } public function testHas() { $this->assertFalse($this->flashBag->has('nothing')); - $this->assertTrue($this->flashBag->has('status')); + $this->assertTrue($this->flashBag->has(FlashBag::STATUS)); } /** @@ -86,7 +86,7 @@ public function testHas() */ public function testGetTypes() { - $this->assertEquals(array('status'), $this->flashBag->getTypes()); + $this->assertEquals(array(FlashBag::STATUS), $this->flashBag->getTypes()); } public function testAll() @@ -96,27 +96,27 @@ public function testAll() public function testClear() { - $this->assertTrue($this->flashBag->has('status')); - $this->flashBag->clear('status'); - $this->assertFalse($this->flashBag->has('status')); + $this->assertTrue($this->flashBag->has(FlashBag::STATUS)); + $this->flashBag->clear(FlashBag::STATUS); + $this->assertFalse($this->flashBag->has(FlashBag::STATUS)); } public function testClearAll() { - $this->assertTrue($this->flashBag->has('status')); - $this->flashBag->add('error', 'Smile, it might work next time'); - $this->assertTrue($this->flashBag->has('error')); + $this->assertTrue($this->flashBag->has(FlashBag::STATUS)); + $this->flashBag->add('Smile, it might work next time', FlashBag::ERROR); + $this->assertTrue($this->flashBag->has(FlashBag::ERROR)); $this->flashBag->clearAll(); - $this->assertFalse($this->flashBag->has('status')); - $this->assertFalse($this->flashBag->has('error')); + $this->assertFalse($this->flashBag->has(FlashBag::STATUS)); + $this->assertFalse($this->flashBag->has(FlashBag::ERROR)); } public function testPurgeOldFlashes() { - $this->flashBag->add('status', 'Foo'); - $this->flashBag->add('error', 'Bar'); + $this->flashBag->add('Foo', FlashBag::STATUS); + $this->flashBag->add('Bar', FlashBag::ERROR); $this->flashBag->purgeOldFlashes(); - $this->assertEquals(array(1 => 'Foo'), $this->flashBag->get('status')); + $this->assertEquals(array(1 => 'Foo'), $this->flashBag->get(FlashBag::STATUS)); } } \ No newline at end of file