You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #9739 [FrameworkBundle] Extract KernelTestCase from WebTestCase (johnkary)
This PR was merged into the 2.5-dev branch.
Discussion
----------
[FrameworkBundle] Extract KernelTestCase from WebTestCase
| Q | A
| ------------- | ---
| Bug fix? | No
| New feature? | Yes
| BC breaks? | No
| Deprecations? | No
| Tests pass? | Yes
| Fixed tickets | None
| License | MIT
| Doc PR | symfony/symfony-docs#3311
Previous discussion is in #7704. Opened new PR to target master branch.
This idea came to me while reading the Cookbook article [How to create a Console Command - Testing Commands](http://symfony.com/doc/master/cookbook/console/console_command.html#testing-commands) and I wanted to hear feedback from others before submitting a patch.
Basically the Cookbook article states that when testing a Command that extending `WebTestCase` is a good way to get access to the Kernel and thus the Container. This struck me as weird because I was wanting to test a Command, which doesn't really have anything to do with "the web."
Currently `WebTestCase` doesn't do anything internally that looks like web-specific work, and the class docblock itself states "WebTestCase is the base class for functional tests.".
After a suggestion in #7704 by @beberlei, I decided to take his advice and now have the following implementation:
Extracted a new class `KernelTestCase` from `WebTestCase` and instead allow WebTestCase to extend KernelTestCase. Pulled all methods up into KernelTestCase except `createClient()` because `createClient()` is focused solely on creating a Client for issuing web-based requests.
Benjamin's solution provides us a clear extension point from `KernelTestCase` for Command-based tests and also provides 100% backwards-compatibility without the need to deprecate WebTestCase.
Commits
-------
c4f14fb Extract new base test class KernelTestClass
thrownew \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.');
* When the Kernel is located, the file is required.
120
-
*
121
-
* @return string The Kernel class name
122
-
*
123
-
* @throws \RuntimeException
124
-
*/
125
-
protectedstaticfunctiongetKernelClass()
126
-
{
127
-
$dir = $phpUnitDir = static::getPhpUnitXmlDir();
128
-
129
-
if (isset($_SERVER['KERNEL_DIR'])) {
130
-
$dir = $_SERVER['KERNEL_DIR'];
131
-
132
-
if (!is_dir($dir) && is_dir("$phpUnitDir/$dir")) {
133
-
$dir = "$phpUnitDir/$dir";
134
-
}
135
-
}
136
-
137
-
$finder = newFinder();
138
10000
-
$finder->name('*Kernel.php')->depth(0)->in($dir);
139
-
$results = iterator_to_array($finder);
140
-
if (!count($results)) {
141
-
thrownew \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.');
142
-
}
143
-
144
-
$file = current($results);
145
-
$class = $file->getBasename('.php');
146
-
147
-
require_once$file;
148
-
149
-
return$class;
150
-
}
151
-
152
-
/**
153
-
* Creates a Kernel.
154
-
*
155
-
* Available options:
156
-
*
157
-
* * environment
158
-
* * debug
159
-
*
160
-
* @param array $options An array of options
161
-
*
162
-
* @return KernelInterface A KernelInterface instance
0 commit comments