Skip to content

Commit

Permalink
[HttpFoundation] Add back get defaults and small clean-up.
Browse files Browse the repository at this point in the history
Changed read-only method names from get*() to peek*()

Typo
  • Loading branch information
Drak committed Feb 11, 2012
1 parent 5b7ef11 commit dad60ef
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 116 deletions.
Expand Up @@ -4,7 +4,7 @@

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Symfony\Component\HttpFoundation\Session\Storage\AbstractStorage;
use Symfony\Component\HttpFoundation\Session\Storage\SessionSaveHandlerInterface;
use Symfony\Component\HttpFoundation\Session\Storage\SaveHandlerInterface;
use Doctrine\DBAL\Driver\Connection;

/**
Expand All @@ -13,7 +13,7 @@
* @author Fabien Potencier <fabien@symfony.com>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class DbalStorage extends AbstractStorage implements SessionSaveHandlerInterface
class DbalStorage extends AbstractStorage implements SaveHandlerInterface
{
/**
* @var Connection
Expand Down
Expand Up @@ -47,14 +47,14 @@ public function get($name, $default = null)
return $this->session->get($name, $default);
}

public function getFlash($type)
public function getFlash($type, $default = null)
{
return $this->session->getFlashes()->get($type);
return $this->session->getFlashes()->pop($type);
}

public function getFlashes()
{
return $this->session->getFlashes()->all();
return $this->session->getFlashes()->popAll();
}

public function hasFlash($type)
Expand Down
Expand Up @@ -45,7 +45,11 @@ public function testFlash()
$this->assertTrue($helper->hasFlash(FlashBag::NOTICE));

$this->assertEquals('bar', $helper->getFlash(FlashBag::NOTICE));
}

public function testGetFlashes()
{
$helper = new SessionHelper($this->request);
$this->assertEquals(array(FlashBag::NOTICE => 'bar'), $helper->getFlashes());
}

Expand Down
Expand Up @@ -75,30 +75,26 @@ public function initialize(array &$flashes)
/**
* {@inheritdoc}
*/
public function get($type)
public function peek($type, $default = null)
{
if (!$this->has($type)) {
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
}

return $this->flashes['display'][$type];
return $this->has($type) ? $this->flashes['display'][$type] : $default;
}

/**
* {@inheritdoc}
*/
public function all()
public function peekAll()
{
return array_key_exists('display', $this->flashes) ? (array)$this->flashes['display'] : array();
}

/**
* {@inheritdoc}
*/
public function pop($type)
public function pop($type, $default = null)
{
if (!$this->has($type)) {
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
return $default;
}

$return = null;
Expand Down
34 changes: 15 additions & 19 deletions src/Symfony/Component/HttpFoundation/Session/Flash/FlashBag.php
Expand Up @@ -68,41 +68,29 @@ public function initialize(array &$flashes)
/**
* {@inheritdoc}
*/
public function get($type)
public function peek($type, $default = null)
{
if (!$this->has($type)) {
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
}

return $this->flashes[$type];
return $this->has($type) ? $this->flashes[$type] : $default;
}

/**
* {@inheritdoc}
*/
public function set($type, $message)
{
$this->flashes[$type] = $message;
}

/**
* {@inheritdoc}
*/
public function all()
public function peekAll()
{
return $this->flashes;
}

/**
* {@inheritdoc}
*/
public function pop($type)
public function pop($type, $default = null)
{
if (!$this->has($type)) {
throw new \InvalidArgumentException(sprintf('Flash type %s not found', $type));
return $default;
}

$return = $this->get($type);
$return = $this->peek($type);
unset($this->flashes[$type]);

return $return;
Expand All @@ -113,12 +101,20 @@ public function pop($type)
*/
public function popAll()
{
$return = $this->all();
$return = $this->peekAll();
$this->flashes = array();

return $return;
}

/**
* {@inheritdoc}
*/
public function set($type, $message)
{
$this->flashes[$type] = $message;
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -36,27 +36,29 @@ function set($type, $message);
/**
* Gets flash message for a given type.
*
* @param string $type Message category type.
* @param string $type Message category type.
* @param string $default Default value if $type doee not exist.
*
* @return string
*/
function get($type);
function peek($type, $default = null);

/**
* Gets all flash messages.
*
* @return array
*/
function all();
function peekAll();

/**
* Pops and clears flash from the stack.
*
* @param string $type
* @param string $default Default value if $type doee not exist.
*
* @return string
*/
function pop($type);
function pop($type, $default = null);

/**
* Pops and clears flashes from the stack.
Expand Down
Expand Up @@ -108,11 +108,4 @@ function remove($name);
* Clears all attributes.
*/
function clear();

/**
* Gets the flashbag interface.
*
* @return FlashBagInterface
*/
function getFlashes();
}
Expand Up @@ -50,34 +50,28 @@ public function testInitialize()
$bag = new FlashBag();
$array = array('new' => array(FlashBag::NOTICE => 'A previous flash message'));
$bag->initialize($array);
$this->assertEquals('A previous flash message', $bag->get(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $bag->peek(FlashBag::NOTICE));
$array = array('new' => array(
FlashBag::NOTICE => 'Something else',
FlashBag::ERROR => 'a',
));
$bag->initialize($array);
$this->assertEquals('Something else', $bag->get(FlashBag::NOTICE));
$this->assertEquals('a', $bag->get(FlashBag::ERROR));
$this->assertEquals('Something else', $bag->peek(FlashBag::NOTICE));
$this->assertEquals('a', $bag->peek(FlashBag::ERROR));
}

public function testGet()
public function testPeek()
{
$this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE));
$this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testGetException()
{
$this->bag->get('non_existing_type');
$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));
}

public function testSet()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->assertNotEquals('Foo', $this->bag->get(FlashBag::NOTICE));
$this->assertNotEquals('Foo', $this->bag->peek(FlashBag::NOTICE));
}

public function testHas()
Expand All @@ -91,7 +85,7 @@ public function testKeys()
$this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys());
}

public function testAll()
public function testPeekAll()
{
$array = array(
'new' => array(
Expand All @@ -104,25 +98,22 @@ public function testAll()
$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
), $this->bag->all()
), $this->bag->peekAll()
);

$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
), $this->bag->peekAll()
);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testPop()
{
$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->bag->pop(FlashBag::NOTICE);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testPopException()
{
$this->bag->pop('non_existing_type');
$this->assertNull($this->bag->pop(FlashBag::NOTICE));
}

public function testPopAll()
Expand Down
Expand Up @@ -49,45 +49,26 @@ public function testInitialize()
{
$bag = new FlashBag();
$bag->initialize($this->array);
$this->assertEquals($this->array, $bag->all());
$this->assertEquals($this->array, $bag->peekAll());
$array = array('should' => array('change'));
$bag->initialize($array);
$this->assertEquals($array, $bag->all());
$this->assertEquals($array, $bag->peekAll());
}

public function testGet()
public function testPeek()
{
$this->assertEquals('A previous flash message', $this->bag->get(FlashBag::NOTICE));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testGetException()
{
$this->bag->get('non_existing_type');
$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));
}

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));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testPopException()
{
$this->assertEquals('A previous flash message', $this->bag->pop(FlashBag::NOTICE));
$this->bag->pop(FlashBag::NOTICE);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testPopExceptionNotExisting()
{
$this->bag->pop('non_existing_type');
$this->assertNull($this->bag->pop(FlashBag::NOTICE));
}

public function testPopAll()
Expand All @@ -102,11 +83,11 @@ public function testPopAll()
$this->assertEquals(array(), $this->bag->popAll());
}

public function testset()
public function testSet()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::NOTICE, 'Bar');
$this->assertEquals('Bar', $this->bag->get(FlashBag::NOTICE));
$this->assertEquals('Bar', $this->bag->peek(FlashBag::NOTICE));
}

public function testHas()
Expand All @@ -120,21 +101,21 @@ public function testKeys()
$this->assertEquals(array(FlashBag::NOTICE), $this->bag->keys());
}

public function testAll()
public function testPeekAll()
{
$this->bag->set(FlashBag::NOTICE, 'Foo');
$this->bag->set(FlashBag::ERROR, 'Bar');
$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
), $this->bag->all()
), $this->bag->peekAll()
);
$this->assertTrue($this->bag->has(FlashBag::NOTICE));
$this->assertTrue($this->bag->has(FlashBag::ERROR));
$this->assertEquals(array(
FlashBag::NOTICE => 'Foo',
FlashBag::ERROR => 'Bar',
), $this->bag->all()
), $this->bag->peekAll()
);
}
}

0 comments on commit dad60ef

Please sign in to comment.