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

Skip to content

Commit e8b4d35

Browse files
warslettfabpot
authored andcommitted
[FrameworkBundle] start session on flashbag injection
1 parent e44a3f5 commit e8b4d35

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
<service id="session" class="Symfony\Component\HttpFoundation\Session\Session" public="true">
1515
<argument type="service" id="session.storage" />
16-
<argument type="service" id="session.attribute_bag" />
17-
<argument type="service" id="session.flash_bag" />
1816
</service>
1917

2018
<service id="Symfony\Component\HttpFoundation\Session\SessionInterface" alias="session" />
@@ -37,10 +35,14 @@
3735
<argument type="service" id="session.storage.metadata_bag" />
3836
</service>
3937

40-
<service id="session.flash_bag" class="Symfony\Component\HttpFoundation\Session\Flash\FlashBag" />
38+
<service id="session.flash_bag" class="Symfony\Component\HttpFoundation\Session\Flash\FlashBag">
39+
<factory service="session" method="getFlashBag"/>
40+
</service>
4141
<service id="Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface" alias="session.flash_bag" />
4242

43-
<service id="session.attribute_bag" class="Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag" />
43+
<service id="session.attribute_bag" class="Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag">
44+
<factory service="session" method="getAttributeBag"/>
45+
</service>
4446

4547
<service id="session.storage.mock_file" class="Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage">
4648
<argument>%kernel.cache_dir%/sessions</argument>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
4+
5+
use Symfony\Component\HttpFoundation\RedirectResponse;
6+
use Symfony\Component\HttpFoundation\Request;
7+
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
8+
use Symfony\Component\Routing\RouterInterface;
9+
10+
class InjectedFlashbagSessionController
11+
{
12+
/**
13+
* @var FlashBagInterface
14+
*/
15+
private $flashBag;
16+
17+
/**
18+
* @var RouterInterface
19+
*/
20+
private $router;
21+
22+
public function __construct(
23+
FlashBagInterface $flashBag,
24+
RouterInterface $router
25+
) {
26+
$this->flashBag = $flashBag;
27+
$this->router = $router;
28+
}
29+
30+
public function setFlashAction(Request $request, $message)
31+
{
32+
$this->flashBag->add('notice', $message);
33+
34+
return new RedirectResponse($this->router->generate('session_showflash'));
35+
}
36+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ session_setflash:
1818
path: /session_setflash/{message}
1919
defaults: { _controller: TestBundle:Session:setFlash}
2020

21+
injected_flashbag_session_setflash:
22+
path: injected_flashbag/session_setflash/{message}
23+
defaults: { _controller: TestBundle:InjectedFlashbagSession:setFlash}
24+
2125
session_showflash:
2226
path: /session_showflash
2327
defaults: { _controller: TestBundle:Session:showFlash}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,29 @@ public function testFlash($config, $insulate)
6969
$this->assertStringContainsString('No flash was set.', $crawler->text());
7070
}
7171

72+
/**
73+
* Tests flash messages work when flashbag service is injected to the constructor.
74+
*
75+
* @dataProvider getConfigs
76+
*/
77+
public function testFlashOnInjectedFlashbag($config, $insulate)
78+
{
79+
$client = $this->createClient(['test_case' => 'Session', 'root_config' => $config]);
80+
if ($insulate) {
81+
$client->insulate();
82+
}
83+
84+
// set flash
85+
$client->request('GET', '/injected_flashbag/session_setflash/Hello%20world.');
86+
87+
// check flash displays on redirect
88+
$this->assertStringContainsString('Hello world.', $client->followRedirect()->text());
89+
90+
// check flash is gone
91+
$crawler = $client->request('GET', '/session_showflash');
92+
$this->assertStringContainsString('No flash was set.', $crawler->text());
93+
}
94+
7295
/**
7396
* See if two separate insulated clients can run without
7497
* polluting each other's session data.

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Session/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ services:
55
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\SubRequestController:
66
tags:
77
- { name: controller.service_arguments, action: indexAction, argument: handler, id: fragment.handler }
8+
9+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\InjectedFlashbagSessionController:
10+
autowire: true
11+
tags: ['controller.service_arguments']

0 commit comments

Comments
 (0)