Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[HttpFoundation] Added some tests and fixed logic and CS accordingly.
  • Loading branch information
Drak committed Nov 10, 2011
commit b60e84ebfcdcc2ecd7caf59587aa79d294dfc8b4
4 changes: 3 additions & 1 deletion CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpFoundation/FlashBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/Symfony/Component/HttpFoundation/FlashBagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Symfony\Component\HttpFoundation;

/**
* FlashBagInterface.
*
* @author Drak <[email protected]>
*/
interface FlashBagInterface
{
Expand All @@ -21,61 +23,61 @@ interface FlashBagInterface
*
* @param array $flashes
*/
public function initialize(array $flashes);
function initialize(array $flashes);

/**
* Adds a flash to the stack for a given type.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here.

*/
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.
*
* @param string $type
* @param array $array
*/
public function set($type, array $array);
function set($type, array $array);

/**
* Hass flash messages for a given type?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo here.

*
* @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();
}
20 changes: 10 additions & 10 deletions src/Symfony/Component/HttpFoundation/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ class Session implements \Serializable
*
* @var \Symfony\Component\HttpFoundation\FlashBagInterface
*/
protected $flashes;
protected $flashBag;
protected $closed;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not add trailing slashes here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see any trailing slashes, what am I missing? I think my IDE must have truncated the line or changed the line ending?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, not slashes but spaces

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it applies to all the blank lines of your patch as most of them are not really blank lines.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is my IDE stripping spaces from empty lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it is your IDE adding them. Stripping them is what you should do to follow the coding standards

/**
* 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indicates that flashBag's are optional, but it's not clear what the expected behaviour is if it's null. The rest of the code appears to assume that flashBag is always set, and I don't see any guards or exceptions thrown.

{
$this->storage = $storage;
$this->flashes = $flashes;
$this->flashBag = $flashBag;
$this->attributes = array();
$this->started = false;
$this->closed = false;
Expand All @@ -60,7 +60,7 @@ public function getFlashBag()
$this->start();
}

return $this->flashes;
return $this->flashBag;
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -190,7 +190,7 @@ public function clear()
}

$this->attributes = array();
$this->flashes->clearAll();
$this->flashBag->clearAll();
}

/**
Expand Down Expand Up @@ -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(),
));
}

Expand Down
63 changes: 20 additions & 43 deletions tests/Symfony/Tests/Component/HttpFoundation/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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()
Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -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());
}

Expand Down Expand Up @@ -227,6 +204,6 @@ public function testStorageRemove()

protected function getSession()
{
return new Session($this->storage);
return new Session($this->storage, $this->flashBag);
}
}