Skip to content

Commit

Permalink
fix accessing session bags
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jun 22, 2019
1 parent 4c088b6 commit 7a4570d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpFoundation/Session/Session.php
Expand Up @@ -253,7 +253,9 @@ public function registerBag(SessionBagInterface $bag)
*/
public function getBag($name)
{
return $this->storage->getBag($name)->getBag();
$bag = $this->storage->getBag($name);

return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionBagProxy;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;

/**
Expand Down Expand Up @@ -260,4 +261,28 @@ public function testIsEmpty()
$flash->get('hello');
$this->assertTrue($this->session->isEmpty());
}

public function testGetBagWithBagImplementingGetBag()
{
$bag = new AttributeBag();
$bag->setName('foo');

$storage = new MockArraySessionStorage();
$storage->registerBag($bag);

$this->assertSame($bag, (new Session($storage))->getBag('foo'));
}

public function testGetBagWithBagNotImplementingGetBag()
{
$data = [];

$bag = new AttributeBag();
$bag->setName('foo');

$storage = new MockArraySessionStorage();
$storage->registerBag(new SessionBagProxy($bag, $data, $usageIndex));

$this->assertSame($bag, (new Session($storage))->getBag('foo'));
}
}

0 comments on commit 7a4570d

Please sign in to comment.