8000 [SecurityBundle] bootstrapped functional test suite · lidaa/symfony@1ca4dca · GitHub
[go: up one dir, main page]

Skip to content

Commit 1ca4dca

Browse files
committed
[SecurityBundle] bootstrapped functional test suite
1 parent 5ed136b commit 1ca4dca

File tree

12 files changed

+296
-0
lines changed

12 files changed

+296
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Controller;
4+
5+
use Symfony\Component\HttpFoundation\Response;
6+
use Symfony\Component\Security\Core\SecurityContext;
7+
use Symfony\Component\DependencyInjection\ContainerAware;
8+
9+
class LoginController extends ContainerAware
10+
{
11+
public function loginAction()
12+
{
13+
// get the login error if there is one
14+
if ($this->container->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
15+
$error = $this->container->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
16+
} else {
17+
$error = $this->container->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
18+
}
19+
20+
return $this->container->get('templating')->renderResponse('FormLoginBundle:Login:login.html.twig', array(
21+
// last username entered by the user
22+
'last_username' => $this->container->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
23+
'error' => $error,
24+
));
25+
}
26+
27+
public function loginCheckAction()
28+
{
29+
return new Response('', 400);
30+
}
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class FormLoginBundle extends Bundle
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
form_login:
2+
pattern: /login
3+
defaults: { _controller: FormLoginBundle:Login:login }
4+
5+
form_login_check:
6+
pattern: /login_check
7+
defaults: { _controller: FormLoginBundle:Login:loginCheck }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{% extends "::base.html.twig" %}
2+
3+
{% block body %}
4+
5+
{% if error %}
6+
<div>{{ error.message }}</div>
7+
{% endif %}
8+
9+
<form action="{{ path('form_login_check') }}" method="post">
10+
<label for="username">Username:</label>
11+
<input type="text" id="username" name="_username" value="{{ last_username }}" />
12+
13+
<label for="password">Password:</label>
14+
<input type="password" id="password" name="_password" />
15+
16+
<input type="hidden" name="_target_path" value="" />
17+
18+
<input type="submit" name="login" />
19 F438 +
</form>
20+
21+
{% endblock %}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
4+
5+
/**
6+
* @group functional
7+
*/
8+
class FormLoginTest extends WebTestCase
9+
{
10+
public function testFormLogin()
11+
{
12+
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
13+
14+
$form = $client->request('GET', '/login')->selectButton('login')->form();
15+
$form['_username'] = 'johannes';
16+
$form['_password'] = 'test';
17+
$client->submit($form);
18+
19+
$this->assertRedirect($client->getResponse(), '/');
20+
}
21+
22+
public function testFormLoginWithCustomTargetPath()
23+
{
24+
$client = $this->createClient(array('test_case' => 'StandardFormLogin'));
25+
26+
$form = $client->request('GET', '/login')->selectButton('login')->form();
27+
$form['_username'] = 'johannes';
28+
$form['_password'] = 'test';
29+
$form['_target_path'] = '/foo';
30+
$client->submit($form);
31+
32+
$this->assertRedirect($client->getResponse(), '/foo');
33+
}
34+
35+
protected function tearDown()
36+
{
37+
parent::tearDown();
38+
$this->deleteTmpDir('StandardFormLogin');
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
4+
5+
use Symfony\Component\HttpKernel\Util\Filesystem;
6+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
7+
8+
class WebTestCase extends BaseWebTestCase
9+
{
10+
public static function assertRedirect($response, $location)
11+
{
12+
self::assertTrue($response->isRedirect());
13+
self::assertEquals('http://localhost'.$location, $response->headers->get('Location'));
14+
}
15+
16+
protected function deleteTmpDir($testCase)
17+
{
18+
if (!file_exists($dir = sys_get_temp_dir().'/'.$testCase)) {
19+
return;
20+
}
21+
22+
$fs = new Filesystem();
23+
$fs->remove($testCase);
24+
}
25+
26+
protected function getKernelClass()
27+
{
28+
require_once __DIR__.'/app/AppKernel.php';
29+
30+
return 'Symfony\Bundle\SecurityBundle\Tests\Functional\AppKernel';
31+
}
32+
33+
protected function createKernel(array $options = array())
34+
{
35+
$class = $this->getKernelClass();
36+
37+
if (!isset($options['test_case'])) {
38+
throw new \InvalidArgumentException('The option "test_case" must be set.');
39+
}
40+
41+
return new $class(
42+
$options['test_case'],
43+
isset($options['environment']) ? $options['environment'] : 'test',
44+
isset($options['debug']) ? $options['debug'] : true
45+
);
46+
}
47+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
4+
5+
use Symfony\Component\HttpKernel\Util\Filesystem;
6+
7+
use Symfony\Component\Config\Loader\LoaderInterface;
8+
use Symfony\Component\HttpKernel\Kernel;
9+
10+
/**
11+
* App Test Kernel for functional tests.
12+
*
13+
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
14+
*/
15+
class AppKernel extends Kernel
16+
{
17+
private $testCase;
18+
19+
public function __construct($testCase, $environment, $debug)
20+
{
21+
if (!is_dir(__DIR__.'/'.$testCase)) {
22+
throw new \InvalidArgumentException(sprintf('The test case "%s" does not exist.', $testCase));
23+
}
24+
$this->testCase = $testCase;
25+
26+
parent::__construct($environment, $debug);
27+
}
28+
29+
public function registerBundles()
30+
{
31+
if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/bundles.php')) {
32+
throw new \RuntimeException(sprintf('The bundles file "%s" does not exist.', $filename));
33+
}
34+
35+
return include $filename;
36+
}
37+
38+
public function getRootDir()
39+
{
40+
return __DIR__;
41+
}
42+
43+
public function getCacheDir()
44+
{
45+
return sys_get_temp_dir().'/'.$this->testCase.'/cache/'.$this->environment;
46+
}
47+
48+
public function getLogDir()
49+
{
50+
return sys_get_temp_dir().'/'.$this->testCase.'/logs';
51+
}
52+
53+
public function registerContainerConfiguration(LoaderInterface $loader)
54+
{
55+
if (!file_exists($filename = $this->getRootDir().'/'.$this->testCase.'/config.yml')) {
56+
throw new \RuntimeException(sprintf('The config file "%s" does not exist.', $filename));
57+
}
58+
59+
$loader->load($filename);
60+
}
61+
62+
protected function getKernelParameters()
63+
{
64+
$parameters = parent::getKernelParameters();
65+
$parameters['kernel.test_case'] = $this->testCase;
66+
67+
return $parameters;
68+
}
69+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>{% block title %}Welcome!{% endblock %}</title>
6+
{% block stylesheets %}{% endblock %}
7+
<link rel="shortcut icon" href="{{ asset('favicon.ico') }}" />
8+
</head>
9+
<body>
10+
{% block body %}{% endblock %}
11+
{% block javascripts %}{% endblock %}
12+
</body>
13+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\FormLoginBundle;
4+
use Symfony\Bundle\TwigBundle\TwigBundle;
5+
use Symfony\Bundle\SecurityBundle\SecurityBundle;
6+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
7+
8+
return array(
9+
new FrameworkBundle(),
10+
new SecurityBundle(),
11+
new TwigBundle(),
12+
new FormLoginBundle(),
13+
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
imports:
2+
- { type: file, resource: ./../config/default.yml }
3+
4+
security:
5+
encoders:
6+
Symfony\Component\Security\Core\User\User: plaintext
7+
8+
providers:
9+
in_memory:
10+
users:
11+
johannes: { password: test, roles: [ROLE_USER] }
12+
13+
firewalls:
14+
login_form:
15+
pattern: ^/login$
16+
security: false
17+
18+
default:
19+
form_login:
20+
check_path: /login_check
21+
22+
access_control:
23+
- { path: .*, roles: IS_AUTHENTICATED_FULLY }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_form_login_bundle:
2+
resource: @FormLoginBundle/Resources/config/routing.yml
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
framework:
2+
charset: UTF-8
3+
secret: test
4+
csrf_protection:
5+
enabled: true
6+
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
7+
validation: { enabled: true, enable_annotations: true }
8+
templating: { engines: ['twig'] }
9+
form: ~
10+
test: ~
11+
session:
12+
default_locale: en
13+
lifetime: 3600
14+
auto_start: true
15+
storage_id: session.storage.filesystem
16+
17+
# Twig Configuration
18+
twig:
19+
debug: %kernel.debug%
20+
strict_variables: %kernel.debug%
21+
extensions: []

0 commit comments

Comments
 (0)
0