Skip to content

Commit 8a47c30

Browse files
committed
Extract new base test class KernelTestClass
Ideal base class that needs access to a Kernel but not a Client such as when testing a Command.
1 parent fa1e994 commit 8a47c30

File tree

2 files changed

+176
-139
lines changed

2 files changed

+176
-139
lines changed

Test/KernelTestCase.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use Symfony\Component\Finder\Finder;
15+
use Symfony\Component\HttpKernel\KernelInterface;
16+
17+
/**
18+
* KernelTestCase is the base class for tests needing a Kernel.
19+
*
20+
* @author Fabien Potencier <[email protected]>
21+
*/
22+
abstract class KernelTestCase extends \PHPUnit_Framework_TestCase
23+
{
24+
protected static $class;
25+
26+
/**
27+
* @var KernelInterface
28+
*/
29+
protected static $kernel;
30+
31+
/**
32+
* Finds the directory where the phpunit.xml(.dist) is stored.
33+
*
34+
* If you run tests with the PHPUnit CLI tool, everything will work as
35+
* expected. If not, override this method in your test classes.
36+
*
37+
* @return string The directory where phpunit.xml(.dist) is stored
38+
*
39+
* @throws \RuntimeException
40+
*/
41+
protected static function getPhpUnitXmlDir()
42+
{
43+
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
44+
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
45+
}
46+
47+
$dir = static::getPhpUnitCliConfigArgument();
48+
if ($dir === null &&
49+
(is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
50+
is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
51+
$dir = getcwd();
52+
}
53+
54+
// Can't continue
55+
if ($dir === null) {
56+
throw new \RuntimeException('Unable to guess the Kernel directory.');
57+
}
58+
59+
if (!is_dir($dir)) {
60+
$dir = dirname($dir);
61+
}
62+
63+
return $dir;
64+
}
65+
66+
/**
67+
* Finds the value of the CLI configuration option.
68+
*
69+
* PHPUnit will use the last configuration argument on the command line, so
70+
* this only returns the last configuration argument.
71+
*
72+
* @return string The value of the PHPUnit cli configuration option
73+
*/
74+
private static function getPhpUnitCliConfigArgument()
75+
{
76+
$dir = null;
77+
$reversedArgs = array_reverse($_SERVER['argv']);
78+
foreach ($reversedArgs as $argIndex => $testArg) {
79+
if (preg_match('/^-[^ \-]*c$/', $testArg) || $testArg === '--configuration') {
80+
$dir = realpath($reversedArgs[$argIndex - 1]);
81+
break;
82+
} elseif (strpos($testArg, '--configuration=') === 0) {
83+
$argPath = substr($testArg, strlen('--configuration='));
84+
$dir = realpath($argPath);
85+
break;
86+
}
87+
}
88+
89+
return $dir;
90+
}
91+
92+
/**
93+
* Attempts to guess the Kernel location.
94+
*
95+
* When the Kernel is located, the file is required.
96+
*
97+
* @return string The Kernel class name
98+
*
99+
* @throws \RuntimeException
100+
*/
101+
protected static function getKernelClass()
102+
{
103+
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir();
104+
105+
$finder = new Finder();
106+
$finder->name('*Kernel.php')->depth(0)->in($dir);
107+
$results = iterator_to_array($finder);
108+
if (!count($results)) {
109+
throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.');
110+
}
111+
112+
$file = current($results);
113+
$class = $file->getBasename('.php');
114+
115+
require_once $file;
116+
117+
return $class;
118+
}
119+
120+
/**
121+
* Boots the Kernel for this test.
122+
*
123+
* @param array $options
124+
*/
125+
protected static function bootKernel(array $options = array())
126+
{
127+
static::ensureKernelShutdown();
128+
129+
static::$kernel = static::createKernel($options);
130+
static::$kernel->boot();
131+
}
132+
133+
/**
134+
* Creates a Kernel.
135+
*
136+
* Available options:
137+
*
138+
* * environment
139+
* * debug
140+
*
141+
* @param array $options An array of options
142+
*
143+
* @return KernelInterface A KernelInterface instance
144+
*/
145+
protected static function createKernel(array $options = array())
146+
{
147+
if (null === static::$class) {
148+
static::$class = static::getKernelClass();
149+
}
150+
151+
return new static::$class(
152+
isset($options['environment']) ? $options['environment'] : 'test',
153+
isset($options['debug']) ? $options['debug'] : true
154+
);
155+
}
156+
157+
/**
158+
* Shutdown the Kernel.
159+
*/
160+
protected static function ensureKernelShutdown()
161+
{
162+
if (null !== static::$kernel) {
163+
static::$kernel->shutdown();
164+
}
165+
}
166+
167+
/**
168+
* Clean up Kernel usage in this test.
169+
*/
170+
protected function tearDown()
171+
{
172+
static::ensureKernelShutdown();
173+
}
174+
}

Test/WebTestCase.php

Lines changed: 2 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,14 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Test;
1313

1414
use Symfony\Bundle\FrameworkBundle\Client;
15-
use Symfony\Component\Finder\Finder;
16-
use Symfony\Component\HttpKernel\KernelInterface;
1715

1816
/**
1917
* WebTestCase is the base class for functional tests.
2018
*
2119
* @author Fabien Potencier <[email protected]>
2220
*/
23-
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
21+
abstract class WebTestCase extends KernelTestCase
2422
{
25-
protected static $class;
26-
27-
/**
28-
* @var KernelInterface
29-
*/
30-
protected static $kernel;
31-
3223
/**
3324
* Creates a Client.
3425
*
@@ -39,139 +30,11 @@ abstract class WebTestCase extends \PHPUnit_Framework_TestCase
3930
*/
4031
protected static function createClient(array $options = array(), array $server = array())
4132
{
42-
if (null !== static::$kernel) {
43-
static::$kernel->shutdown();
44-
}
45-
46-
static::$kernel = static::createKernel($options);
47-
static::$kernel->boot();
33+
static::bootKernel($options);
4834

4935
$client = static::$kernel->getContainer()->get('test.client');
5036
$client->setServerParameters($server);
5137

5238
return $client;
5339
}
54-
55-
/**
56-
* Finds the directory where the phpunit.xml(.dist) is stored.
57-
*
58-
* If you run tests with the PHPUnit CLI tool, everything will work as expected.
59-
* If not, override this method in your test classes.
60-
*
61-
* @return string The directory where phpunit.xml(.dist) is stored
62-
*
63-
* @throws \RuntimeException
64-
*/
65-
protected static function getPhpUnitXmlDir()
66-
{
67-
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
68-
throw new \RuntimeException('You must override the WebTestCase::createKernel() method.');
69-
}
70-
71-
$dir = static::getPhpUnitCliConfigArgument();
72-
if ($dir === null &&
73-
(is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml') ||
74-
is_file(getcwd().DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
75-
$dir = getcwd();
76-
}
77-
78-
// Can't continue
79-
if ($dir === null) {
80-
throw new \RuntimeException('Unable to guess the Kernel directory.');
81-
}
82-
83-
if (!is_dir($dir)) {
84-
$dir = dirname($dir);
85-
}
86-
87-
return $dir;
88-
}
89-
90-
/**
91-
* Finds the value of the CLI configuration option.
92-
*
93-
* PHPUnit will use the last configuration argument on the command line, so this only returns
94-
* the last configuration argument.
95-
*
96-
* @return string The value of the PHPUnit cli configuration option
97-
*/
98-
private static function getPhpUnitCliConfigArgument()
99-
{
100-
$dir = null;
101-
$reversedArgs = array_reverse($_SERVER['argv']);
102-
foreach ($reversedArgs as $argIndex => $testArg) {
103-
if (preg_match('/^-[^ \-]*c$/', $testArg) || $testArg === '--configuration') {
104-
$dir = realpath($reversedArgs[$argIndex - 1]);
105-
break;
106-
} elseif (strpos($testArg, '--configuration=') === 0) {
107-
$argPath = substr($testArg, strlen('--configuration='));
108-
$dir = realpath($argPath);
109-
break;
110-
}
111-
}
112-
113-
return $dir;
114-
}
115-
116-
/**
117-
* Attempts to guess the kernel location.
118-
*
119-
* When the Kernel is located, the file is required.
120-
*
121-
* @return string The Kernel class name
122-
*
123-
* @throws \RuntimeException
124-
*/
125-
protected static function getKernelClass()
126-
{
127-
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir();
128-
129-
$finder = new Finder();
130-
$finder->name('*Kernel.php')->depth(0)->in($dir);
131-
$results = iterator_to_array($finder);
132-
if (!count($results)) {
133-
throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to http://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.');
134-
}
135-
136-
$file = current($results);
137-
$class = $file->getBasename('.php');
138-
139-
require_once $file;
140-
141-
return $class;
142-
}
143-
144-
/**
145-
* Creates a Kernel.
146-
*
147-
* Available options:
148-
*
149-
* * environment
150-
* * debug
151-
*
152-
* @param array $options An array of options
153-
*
154-
* @return KernelInterface A KernelInterface instance
155-
*/
156-
protected static function createKernel(array $options = array())
157-
{
158-
if (null === static::$class) {
159-
static::$class = static::getKernelClass();
160-
}
161-
162-
return new static::$class(
163-
isset($options['environment']) ? $options['environment'] : 'test',
164-
isset($options['debug']) ? $options['debug'] : true
165-
);
166-
}
167-
168-
/**
169-
* Shuts the kernel down if it was used in the test.
170-
*/
171-
protected function tearDown()
172-
{
173-
if (null !== static::$kernel) {
174-
static::$kernel->shutdown();
175-
}
176-
}
17740
}

0 commit comments

Comments
 (0)