From bd0255428960a92877dabf766a6b3c5a7c5b37d9 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Thu, 15 Mar 2012 16:41:06 -0400 Subject: [PATCH] [HttpFoundation] SPL IteratorAggregate+Countable on *Bags Added the IteratorAggregate and Countable SPL Interfaces on all the *Bag classes in HttpFoundation --- .../Component/HttpFoundation/HeaderBag.php | 22 ++++++++++++- .../Component/HttpFoundation/ParameterBag.php | 2 +- .../Session/Attribute/AttributeBag.php | 22 ++++++++++++- .../HttpFoundation/Session/Flash/FlashBag.php | 22 ++++++++++++- .../HttpFoundation/Session/Session.php | 22 ++++++++++++- .../HttpFoundation/HeaderBagTest.php | 28 ++++++++++++++++ .../Session/Attribute/AttributeBagTest.php | 22 +++++++++++++ .../Session/Flash/FlashBagTest.php | 33 +++++++++++++++++++ .../HttpFoundation/Session/SessionTest.php | 30 +++++++++++++++++ 9 files changed, 198 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/HeaderBag.php b/src/Symfony/Component/HttpFoundation/HeaderBag.php index f614b094fd..b581dcca82 100644 --- a/src/Symfony/Component/HttpFoundation/HeaderBag.php +++ b/src/Symfony/Component/HttpFoundation/HeaderBag.php @@ -18,7 +18,7 @@ namespace Symfony\Component\HttpFoundation; * * @api */ -class HeaderBag +class HeaderBag implements \IteratorAggregate, \Countable { protected $headers; protected $cacheControl; @@ -267,6 +267,26 @@ class HeaderBag $this->set('Cache-Control', $this->getCacheControlHeader()); } + /** + * IteratorAggregate method for looping the instance + * + * @return array An array of parameters + */ + public function getIterator() + { + return new \ArrayIterator($this->headers); + } + + /** + * Countable method returning the number of headers + * + * @return int Number of parameters held in the bag + */ + public function count() + { + return count($this->headers); + } + protected function getCacheControlHeader() { $parts = array(); diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index 12f7253b77..e6a67978fa 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -282,7 +282,7 @@ class ParameterBag implements \IteratorAggregate, \Countable /** * IteratorAggregate method for looping the instance * - * @return array An array of parameters + * @return ArrayIterator An array of parameters wrapped in an ArrayIterator */ public function getIterator() { diff --git a/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php index d1bcb0ffb6..2a49a5398a 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Attribute/AttributeBag.php @@ -14,7 +14,7 @@ namespace Symfony\Component\HttpFoundation\Session\Attribute; /** * This class relates to session attribute storage */ -class AttributeBag implements AttributeBagInterface +class AttributeBag implements AttributeBagInterface, \IteratorAggregate, \Countable { private $name = 'attributes'; @@ -134,4 +134,24 @@ class AttributeBag implements AttributeBagInterface return $return; } + + /** + * + * + * @return ArrayIterator + */ + public function getIterator() + { + return new \ArrayIterator($this->attributes); + } + + /** + * + * + * @return int + */ + public function count() + { + return count($this->attributes); + } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php index c0b4ee57a0..03880c71df 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php +++ b/src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php @@ -16,7 +16,7 @@ namespace Symfony\Component\HttpFoundation\Session\Flash; * * @author Drak */ -class FlashBag implements FlashBagInterface +class FlashBag implements FlashBagInterface, \IteratorAggregate, \Countable { private $name = 'flashes'; @@ -155,4 +155,24 @@ class FlashBag implements FlashBagInterface { return $this->all(); } + + /** + * IteratorAggregate + * + * @return ArrayIterator + */ + public function getIterator() + { + return new \ArrayIterator($this->all()); + } + + /** + * + * + * @return int + */ + public function count() + { + return count($this->flashes); + } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Session.php b/src/Symfony/Component/HttpFoundation/Session/Session.php index 63dda8d6bd..4c0422488f 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Session.php +++ b/src/Symfony/Component/HttpFoundation/Session/Session.php @@ -26,7 +26,7 @@ use Symfony\Component\HttpFoundation\Session\SessionBagInterface; * * @api */ -class Session implements SessionInterface +class Session implements SessionInterface, \IteratorAggregate, \Countable { /** * Storage driver. @@ -260,4 +260,24 @@ class Session implements SessionInterface { return $this->getBag('flashes')->clear(); } + + /** + * IteratorAggregate method for looping through the session attributes + * + * @return ArrayIterator + */ + public function getIterator() + { + return new \ArrayIterator($this->storage->getBag('attributes')->all()); + } + + /** + * Get the number of attributes in the session + * + * @return int Number of attributes + */ + public function count() + { + return count($this->storage->getBag('attributes')->all()); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php index 229c00a451..c9f4b433df 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/HeaderBagTest.php @@ -127,4 +127,32 @@ class HeaderBagTest extends \PHPUnit_Framework_TestCase $this->assertTrue($bag->hasCacheControlDirective('max-age')); $this->assertEquals(10, $bag->getCacheControlDirective('max-age')); } + + /** + * @covers Symfony\Component\HttpFoundation\HeaderBag::getIterator + */ + public function testGetIterator() + { + $headers = array('foo' => 'bar', 'hello' => 'world', 'third' => 'charm'); + $headerBag = new HeaderBag($headers); + + $i = 0; + foreach ($headerBag as $key => $val) { + $i++; + $this->assertEquals(array($headers[$key]), $val); + } + + $this->assertEquals(count($headers), $i); + } + + /** + * @covers Symfony\Component\HttpFoundation\HeaderBag::count + */ + public function testCount() + { + $headers = array('foo' => 'bar', 'HELLO' => 'WORLD'); + $headerBag = new HeaderBag($headers); + + $this->assertEquals(count($headers), count($headerBag)); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php index 5bf62110d9..8a937db23d 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Attribute/AttributeBagTest.php @@ -159,4 +159,26 @@ class AttributeBagTest extends \PHPUnit_Framework_TestCase array('bye/for/now', null, false), ); } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::getIterator + */ + public function testGetIterator() + { + $i = 0; + foreach ($this->bag as $key => $val) { + $this->assertEquals($this->array[$key], $val); + $i++; + } + + $this->assertEquals(count($this->array), $i); + } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag::count + */ + public function testCount() + { + $this->assertEquals(count($this->array), count($this->bag)); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php index 1ae73f8f0a..57fd9c4c9a 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Flash/FlashBagTest.php @@ -132,4 +132,37 @@ class FlashBagTest extends \PHPUnit_Framework_TestCase ), $this->bag->peekAll() ); } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Flash\FlashBag::count + */ + public function testCount() + { + $flashes = array('hello' => 'world', 'beep' => 'boop', 'notice' => 'nope'); + foreach ($flashes as $key => $val) { + $this->bag->set($key, $val); + } + + $this->assertEquals(count($flashes), count($this->bag)); + } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Flash\FlashBag::getIterator + */ + public function testGetIterator() + { + $flashes = array('hello' => 'world', 'beep' => 'boop', 'notice' => 'nope'); + foreach ($flashes as $key => $val) { + $this->bag->set($key, $val); + } + + $i = 0; + foreach ($this->bag as $key => $val) { + $this->assertEquals($flashes[$key], $val); + $i++; + } + + $this->assertEquals(count($flashes), $i); + $this->assertEquals(0, count($this->bag)); + } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php index 9141c79fb9..8da6885b26 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/SessionTest.php @@ -215,4 +215,34 @@ class SessionTest extends \PHPUnit_Framework_TestCase $this->assertFalse($this->session->hasFlash('notice')); $this->assertFalse($this->session->hasFlash('error')); } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Session::getIterator + */ + public function testGetIterator() + { + $attributes = array('hello' => 'world', 'symfony2' => 'rocks'); + foreach ($attributes as $key => $val) { + $this->session->set($key, $val); + } + + $i = 0; + foreach ($this->session as $key => $val) { + $this->assertEquals($attributes[$key], $val); + $i++; + } + + $this->assertEquals(count($attributes), $i); + } + + /** + * @covers Symfony\Component\HttpFoundation\Session\Session::count + */ + public function testGetCount() + { + $this->session->set('hello', 'world'); + $this->session->set('symfony2', 'rocks'); + + $this->assertEquals(2, count($this->session)); + } }