Skip to content

Commit

Permalink
bug #32137 [HttpFoundation] fix accessing session bags (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[HttpFoundation] fix accessing session bags

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #30682
| License       | MIT
| Doc PR        |

Commits
-------

7a4570d fix accessing session bags
  • Loading branch information
fabpot committed Jun 26, 2019
2 parents c511e46 + 7a4570d commit c042b5b
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 c042b5b

Please sign in to comment.