Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 202cefd

Browse filesBrowse files
committed
merged branch drak/frameworkbundle_sessiontest (PR #2887)
Commits ------- ba7d810 [FrameworkBundle] Added functional tests. Discussion ---------- [FrameworkBundle] Added functional tests Bug fix: no Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - Tests session attributes and flash messages APIs in functional practice. These tests increase coverage of this important area and make any future work on sessions much more reliable.
2 parents dc03371 + ba7d810 commit 202cefd
Copy full SHA for 202cefd

File tree

11 files changed

+407
-0
lines changed
Filter options

11 files changed

+407
-0
lines changed
+73Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpFoundation\RedirectResponse;
17+
use Symfony\Component\Security\Core\SecurityContext;
18+
use Symfony\Component\DependencyInjection\ContainerAware;
19+
20+
class SessionController extends ContainerAware
21+
{
22+
public function welcomeAction($name=null)
23+
{
24+
$request = $this->container->get('request');
25+
$session = $request->getSession();
26+
27+
// new session case
28+
if (!$session->has('name')) {
29+
if (!$name) {
30+
return new Response('You are new here and gave no name.');
31+
}
32+
33+
// remember name
34+
$session->set('name', $name);
35+
36+
return new Response(sprintf('Hello %s, nice to meet you.', $name));
37+
}
38+
39+
// existing session
40+
$name = $session->get('name');
41+
42+
return new Response(sprintf('Welcome back %s, nice to meet you.', $name));
43+
}
44+
45+
public function logoutAction()
46+
{
47+
$request = $this->container->get('request')->getSession('session')->clear();
48+
return new Response('Session cleared.');
49+
}
50+
51+
public function setFlashAction($message)
52+
{
53+
$request = $this->container->get('request');
54+
$session = $request->getSession();
55+
$session->setFlash('notice', $message);
56+
57+
return new RedirectResponse($this->container->get('router')->generate('session_showflash'));
58+
}
59+
60+
public function showFlashAction()
61+
{
62+
$request = $this->container->get('request');
63+
$session = $request->getSession();
64+
65+
if ($session->hasFlash('notice')) {
66+
$output = $session->getFlash('notice');
67+
} else {
68+
$output = 'No flash was set.';
69+
}
70+
71+
return new Response($output);
72+
}
73+
}
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
session_welcome:
2+
pattern: /session
3+
defaults: { _controller: TestBundle:Session:welcome }
4+
5+
session_welcome_name:
6+
pattern: /session/{name}
7+
defaults: { _controller: TestBundle:Session:welcome }
8+
9+
session_logout:
10+
pattern: /session_logout
11+
defaults: { _controller: TestBundle:Session:logout}
12+
13+
session_setflash:
14+
pattern: /session_setflash/{message}
15+
defaults: { _controller: TestBundle:Session:setFlash}
16+
17+
session_showflash:
18+
pattern: /session_showflash
19+
defaults: { _controller: TestBundle:Session:showFlash}
20+
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle;
13+
14+
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
16+
class TestBundle extends Bundle
17+
{
18+
}
+87Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
/**
15+
* @group functional
16+
*/
17+
class SessionTest extends WebTestCase
18+
{
19+
/**
20+
* @dataProvider getConfigs
21+
*/
22+
public function testWelcome($config)
23+
{
24+
$client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
25+
$client->insulate();
26+
27+
// no session
28+
$crawler = $client->request('GET', '/session');
29+
$this->assertContains('You are new here and gave no name.', $crawler->text());
30+
31+
// remember name
32+
$crawler = $client->request('GET', '/session/drak');
33+
$this->assertContains('Hello drak, nice to meet you.', $crawler->text());
34+
35+
// prove remembered name
36+
$crawler = $client->request('GET', '/session');
37+
$this->assertContains('Welcome back drak, nice to meet you.', $crawler->text());
38+
39+
// clear session
40+
$crawler = $client->request('GET', '/session_logout');
41+
$this->assertContains('Session cleared.', $crawler->text());
42+
43+
// prove cleared session
44+
$crawler = $client->request('GET', '/session');
45+
$this->assertContains('You are new here and gave no name.', $crawler->text());
46+
}
47+
48+
/**
49+
* @dataProvider getConfigs
50+
*/
51+
public function testFlash($config)
52+
{
53+
$client = $this->createClient(array('test_case' => 'Session', 'root_config' => $config));
54+
$client->insulate();
55+
56+
// set flash
57+
$crawler = $client->request('GET', '/session_setflash/Hello%20world.');
58+
59+
// check flash displays on redirect
60+
$this->assertContains('Hello world.', $client->followRedirect()->text());
61+
62+
// check flash is gone
63+
$crawler = $client->request('GET', '/session_showflash');
64+
$this->assertContains('No flash was set.', $crawler->text());
65+
}
66+
67+
public function getConfigs()
68+
{
69+
return array(
70+
array('config.yml'),
71+
);
72+
}
73+
74+
protected function setUp()
75+
{
76+
parent::setUp();
77+
78+
$this->deleteTmpDir('SessionTest');
79+
}
80+
81+
protected function tearDown()
82+
{
83+
parent::tearDown();
84+
85+
$this->deleteTmpDir('SessionTest');
86+
}
87+
}
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Component\HttpKernel\Util\Filesystem;
15+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
16+
17+
class WebTestCase extends BaseWebTestCase
18+
{
19+
static public function assertRedirect($response, $location)
20+
{
21+
self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.substr($response, 0, 2000));
22+
self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
23+
}
24+
25+
protected function setUp()
26+
{
27+
if (!class_exists('Twig_Environment')) {
28+
$this->markTestSkipped('Twig is not available.');
29+
}
30+
31+
parent::setUp();
32+
}
33+
34+
protected function deleteTmpDir($testCase)
35+
{
36+
if (!file_exists($dir = sys_get_temp_dir().'/'.$testCase)) {
37+
return;
38+
}
39+
40+
$fs = new Filesystem();
41+
$fs->remove($dir);
42+
}
43+
44+
static protected function getKernelClass()
45+
{
46+
require_once __DIR__.'/app/AppKernel.php';
47+
48+
return 'Symfony\Bundle\FrameworkBundle\Tests\Functional\AppKernel';
49+
}
50+
51+
static protected function createKernel(array $options = array())
52+
{
53+
$class = self::getKernelClass();
54+
55+
if (!isset($options['test_case'])) {
56+
throw new \InvalidArgumentException('The option "test_case" must be set.');
57+
}
58+
59+
return new $class(
60+
$options['test_case'],
61+
isset($options['root_config']) ? $options['root_config'] : 'config.yml',
62+
isset($options['environment']) ? $options['environment'] : 'frameworkbundletest',
63+
isset($options['debug']) ? $options['debug'] : true
64+
);
65+
}
66+
}
+113Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
// get the autoload file
15+
$dir = __DIR__;
16+
$lastDir = null;
17+
while ($dir !== $lastDir) {
18+
$lastDir = $dir;
19+
20+
if (is_file($dir.'/autoload.php')) {
21+
require_once $dir.'/autoload.php';
22+
break;
23+
}
24+
25+
if (is_file($dir.'/autoload.php.dist')) {
26+
require_once $dir.'/autoload.php.dist';
27+
break;
28+
}
29+
30+
$dir = dirname($dir);
31+
}
32+
33+
use Symfony\Component\HttpKernel\Util\Filesystem;
34+
use Symfony\Component\Config\Loader\LoaderInterface;
35+
use Symfony\Component\HttpKernel\Kernel;
36+
37+
/**
38+
* App Test Kernel for functional tests.
39+
*
40+
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
41+
*/
42+
class AppKernel extends Kernel
43+
{
44+
private $testCase;
45+
private $rootConfig;
46+
47+
public function __construct($testCase, $rootConfig, $environment, $debug)
48+
{
49+
if (!is_dir(__DIR__.'/'.$testCase)) {
50+
throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase));
51+
}
52+
$this->testCase = $testCase;
53+
54+
$fs = new Filesystem();
55+
if (!$fs->isAbsolutePath($rootConfig) && !is_file($rootConfig = __DIR__.'/'.$testCase.'/'.$rootConfig)) {
56+
throw new \InvalidArgumentException(sprintf('The root config "%s" does not exist.', $rootConfig));
57+
}
58+
$this->rootConfig = $rootConfig;
59+
60+
parent::__construct($environment, $debug);
61+
}
62+
63+
public function registerBundles()
64+
{
65+
if (!is_file($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
66+
throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
67+
}
68+
69+
return include $filename;
70+
}
71+
72+
public function init()
73+
{
74+
}
75+
76+
public function getRootDir()
77+
{
78+
return __DIR__;
79+
}
80+
81+
public function getCacheDir()
82+
{
83+
return sys_get_temp_dir().'/'.$this->testCase.'/cache/'.$this->environment;
84+
}
85+
86+
public function getLogDir()
87+
{
88+
return sys_get_temp_dir().'/'.$this->testCase.'/logs';
89+
}
90+
91+
public function registerContainerConfiguration(LoaderInterface $loader)
92+
{
93+
$loader->load($this->rootConfig);
94+
}
95+
96+
public function serialize()
97+
{
98+
return serialize(array($this->testCase, $this->rootConfig, $this->getEnvironment(), $this->isDebug()));
99+
}
100+
101+
public function unserialize($str)
102+
{
103+
call_user_func_array(array($this, '__construct'), unserialize($str));
104+
}
105+
106+
protected function getKernelParameters()
107+
{
108+
$parameters = parent::getKernelParameters();
109+
$parameters['kernel.test_case'] = $this->testCase;
110+
111+
return $parameters;
112+
}
113+
}
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
4+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
5+
6+
return array(
7+
new FrameworkBundle(),
8+
new TestBundle(),
9+
);
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
imports:
2+
- { resource: ./../config/default.yml }
3+
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_sessiontest_bundle:
2+
resource: @TestBundle/Resources/config/routing.yml

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.