Skip to content

Commit 972730b

Browse files
committed
add functional tests
1 parent 681c134 commit 972730b

File tree

17 files changed

+430
-0
lines changed

17 files changed

+430
-0
lines changed

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,23 @@
1919
"symfony/http-kernel": "~2.8|~3.0|~4.0",
2020
"symfony/routing": "~2.8|~3.0|~4.0"
2121
},
22+
"require-dev": {
23+
"phpunit/phpunit": "~4.0",
24+
"symfony/asset": "~2.8|~3.0|~4.0",
25+
"symfony/browser-kit": "~2.8|~3.0|~4.0",
26+
"symfony/filesystem": "~2.8|~3.0|~4.0",
27+
"symfony/http-foundation": "~2.8|~3.0|~4.0"
28+
},
2229
"autoload": {
2330
"psr-4": {
2431
"PHPMentors\\RouteBasedSessionConfigurationBundle\\": "src/"
2532
}
2633
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"PHPMentors\\RouteBasedSessionConfigurationBundle\\": "tests/"
37+
}
38+
},
2739
"extra": {
2840
"branch-alias": {
2941
"dev-master": "1.1.x-dev"

phpunit.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="phpunit.xsd"
4+
bootstrap="tests/bootstrap.php"
5+
backupGlobals="false"
6+
verbose="true"
7+
colors="true">
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory suffix="Test.php">./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
14+
<filter>
15+
<whitelist>
16+
<directory>./src</directory>
17+
</whitelist>
18+
</filter>
19+
</phpunit>

tests/Functional/AbstractTestCase.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
4+
* All rights reserved.
5+
*
6+
* This file is part of RouteBasedSessionConfigurationBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\RouteBasedSessionConfigurationBundle\Functional;
14+
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
16+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17+
use Symfony\Component\Filesystem\Filesystem;
18+
19+
/**
20+
* @since Class available since Release 1.1.0
21+
*/
22+
abstract class AbstractTestCase extends WebTestCase
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function setUp()
28+
{
29+
parent::setUp();
30+
31+
$_SERVER['KERNEL_DIR'] = __DIR__.'/app';
32+
require_once $_SERVER['KERNEL_DIR'].'/AppKernel.php';
33+
$_SERVER['KERNEL_CLASS'] = 'AppKernel';
34+
35+
if (self::$kernel !== null) {
36+
$this->removeCacheDir(self::$kernel->getCacheDir());
37+
}
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
protected function tearDown()
44+
{
45+
parent::tearDown();
46+
47+
if (self::$kernel !== null) {
48+
$this->removeCacheDir(self::$kernel->getCacheDir());
49+
}
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected static function createKernel(array $options = array())
56+
{
57+
$kernel = KernelTestCase::createKernel($options);
58+
if (array_key_exists('config', $options)) {
59+
$kernel->setConfig($options['config']);
60+
}
61+
62+
return $kernel;
63+
}
64+
65+
protected function removeCacheDir($cacheDir)
66+
{
67+
$fileSystem = new Filesystem();
68+
$fileSystem->remove($cacheDir);
69+
}
70+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
4+
* All rights reserved.
5+
*
6+
* This file is part of RouteBasedSessionConfigurationBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\RouteBasedSessionConfigurationBundle\Functional\Bundle\TestBundle\Controller;
14+
15+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\HttpFoundation\JsonResponse;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
19+
20+
/**
21+
* @since Class available since Release 1.1.0
22+
*/
23+
class RuntimeSessionConfigurationController extends Controller
24+
{
25+
private $sessionStorage;
26+
27+
public function __construct(SessionStorageInterface $sessionStorage)
28+
{
29+
$this->sessionStorage = $sessionStorage;
30+
}
31+
32+
/**
33+
* @param Request $request
34+
*
35+
* @return JsonResponse
36+
*/
37+
public function indexAction(Request $request)
38+
{
39+
return new JsonResponse(array(
40+
'session' => array(
41+
'options' => $this->sessionStorage->getOptions()
42+
)
43+
));
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
4+
* All rights reserved.
5+
*
6+
* This file is part of RouteBasedSessionConfigurationBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\RouteBasedSessionConfigurationBundle\Functional\Bundle\TestBundle\DependencyInjection\Compiler;
14+
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
18+
/**
19+
* @since Class available since Release 1.1.0
20+
*/
21+
class ReplaceSessionStorageDefinitionPass implements CompilerPassInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function process(ContainerBuilder $container)
27+
{
28+
if ($container->hasDefinition('session.storage.mock_file')) {
29+
$container->getDefinition('phpmentors_route_based_session_configuration_test.mock_file_session_storage')->setArguments($container->getDefinition('session.storage.mock_file')->getArguments());
30+
$container->setAlias('session.storage.mock_file', 'phpmentors_route_based_session_configuration_test.mock_file_session_storage');
31+
}
32+
}
33+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test_runtime_session_configuration_index:
2+
path: /runtime-session-configuration/
3+
defaults: { _controller: "phpmentors_route_based_session_configuration_test.runtime_session_configuration_controller:indexAction" }
4+
methods: [GET]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test_runtime_session_configuration_index_admin:
2+
path: /runtime-session-configuration/
3+
defaults: { _controller: "phpmentors_route_based_session_configuration_test.runtime_session_configuration_controller:indexAction" }
4+
methods: [GET]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
4+
* All rights reserved.
5+
*
6+
* This file is part of RouteBasedSessionConfigurationBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\RouteBasedSessionConfigurationBundle\Functional\Bundle\TestBundle\Session\Storage;
14+
15+
/**
16+
* @since Class available since Release 1.1.0
17+
*/
18+
class MockFileSessionStorage extends \Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage
19+
{
20+
private $options = array();
21+
22+
public function setOptions(array $options)
23+
{
24+
$this->options = $options;
25+
}
26+
27+
public function getOptions()
28+
{
29+
return $this->options;
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
4+
* All rights reserved.
5+
*
6+
* This file is part of RouteBasedSessionConfigurationBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\RouteBasedSessionConfigurationBundle\Functional\Bundle\TestBundle;
14+
15+
use PHPMentors\RouteBasedSessionConfigurationBundle\Functional\Bundle\TestBundle\DependencyInjection\Compiler\ReplaceSessionStorageDefinitionPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\HttpKernel\Bundle\Bundle;
18+
19+
/**
20+
* @since Class available since Release 1.1.0
21+
*/
22+
class TestBundle extends Bundle
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function build(ContainerBuilder $container)
28+
{
29+
$container->addCompilerPass(new ReplaceSessionStorageDefinitionPass());
30+
}
31+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/*
3+
* Copyright (c) KUBO Atsuhiro <kubo@iteman.jp>,
4+
* All rights reserved.
5+
*
6+
* This file is part of RouteBasedSessionConfigurationBundle.
7+
*
8+
* This program and the accompanying materials are made available under
9+
* the terms of the BSD 2-Clause License which accompanies this
10+
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
11+
*/
12+
13+
namespace PHPMentors\RouteBasedSessionConfigurationBundle\Functional;
14+
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\HttpKernel\Kernel;
17+
18+
/**
19+
* @since Class available since Release 1.1.0
20+
*/
21+
class RuntimeSessionConfigurationTest extends AbstractTestCase
22+
{
23+
public function startSessionByRouteConfigurationData()
24+
{
25+
return array(
26+
array('https://www.example.com/runtime-session-configuration/', 'USER_SESSION'),
27+
array('https://www.example.com/admin/runtime-session-configuration/', 'ADMIN_SESSION'),
28+
);
29+
}
30+
31+
/**
32+
* @test
33+
* @dataProvider startSessionByRouteConfigurationData
34+
*
35+
* @param string $url
36+
* @param string $sessionName
37+
*/
38+
public function startSessionByRouteConfiguration($url, $sessionName)
39+
{
40+
$client = $this->createClient(array('config' => function (ContainerBuilder $container) {
41+
$container->loadFromExtension('framework', array(
42+
'secret' => '$ecret',
43+
));
44+
}));
45+
46+
$client->request('GET', $url);
47+
48+
$this->assertThat($client->getResponse()->getStatusCode(), $this->equalTo(200));
49+
50+
$content = json_decode($client->getResponse()->getContent(), true);
51+
52+
$this->assertThat($content['session']['options']['name'], $this->equalTo($sessionName));
53+
}
54+
}

0 commit comments

Comments
 (0)