Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e216c0d

Browse files
Add compatibility for phpunit 8
1 parent 5e9373b commit e216c0d

File tree

4 files changed

+195
-216
lines changed

4 files changed

+195
-216
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
use Symfony\Component\HttpKernel\KernelInterface;
17+
use Symfony\Contracts\Service\ResetInterface;
18+
19+
/**
20+
* KernelTestCase is the base class for tests needing a Kernel.
21+
*
22+
* @author Fabien Potencier <[email protected]>
23+
*
24+
* @internal
25+
*/
26+
abstract class BaseKernelTestCase extends TestCase
27+
{
28+
protected static $class;
29+
30+
/**
31+
* @var KernelInterface
32+
*/
33+
protected static $kernel;
34+
35+
/**
36+
* @var ContainerInterface
37+
*/
38+
protected static $container;
39+
40+
/**
41+
* @return string The Kernel class name
42+
*
43+
* @throws \RuntimeException
44+
* @throws \LogicException
45+
*/
46+
protected static function getKernelClass()
47+
{
48+
if (!isset($_SERVER['KERNEL_CLASS']) && !isset($_ENV['KERNEL_CLASS'])) {
49+
throw new \LogicException(sprintf('You must set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel in phpunit.xml / phpunit.xml.dist or override the %1$s::createKernel() or %1$s::getKernelClass() method.', static::class));
50+
}
51+
52+
if (!class_exists($class = $_ENV['KERNEL_CLASS'] ?? $_SERVER['KERNEL_CLASS'])) {
53+
throw new \RuntimeException(sprintf('Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method.', $class, static::class));
54+
}
55+
56+
return $class;
57+
}
58+
59+
/**
60+
* Boots the Kernel for this test.
61+
*
62+
* @return KernelInterface A KernelInterface instance
63+
*/
64+
protected static function bootKernel(array $options = [])
65+
{
66+
static::ensureKernelShutdown();
67+
68+
static::$kernel = static::createKernel($options);
69+
static::$kernel->boot();
70+
71+
$container = static::$kernel->getContainer();
72+
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
73+
74+
return static::$kernel;
75+
}
76+
77+
/**
78+
* Creates a Kernel.
79+
*
80+
* Available options:
81+
*
82+
* * environment
83+
* * debug
84+
*
85+
* @return KernelInterface A KernelInterface instance
86+
*/
87+
protected static function createKernel(array $options = [])
88+
{
89+
if (null === static::$class) {
90+
static::$class = static::getKernelClass();
91+
}
92+
93+
if (isset($options['environment'])) {
94+
$env = $options['environment'];
95+
} elseif (isset($_ENV['APP_ENV'])) {
96+
$env = $_ENV['APP_ENV'];
97+
} elseif (isset($_SERVER['APP_ENV'])) {
98+
$env = $_SERVER['APP_ENV'];
99+
} else {
100+
$env = 'test';
101+
}
102+
103+
if (isset($options['debug'])) {
104+
$debug = $options['debug'];
105+
} elseif (isset($_ENV['APP_DEBUG'])) {
106+
$debug = $_ENV['APP_DEBUG'];
107+
} elseif (isset($_SERVER['APP_DEBUG'])) {
108+
$debug = $_SERVER['APP_DEBUG'];
109+
} else {
110+
$debug = true;
111+
}
112+
113+
return new static::$class($env, $debug);
114+
}
115+
116+
/**
117+
* Shuts the kernel down if it was used in the test.
118+
*/
119+
protected static function ensureKernelShutdown()
120+
{
121+
if (null !== static::$kernel) {
122+
$container = static::$kernel->getContainer();
123+
static::$kernel->shutdown();
124+
if ($container instanceof ResetInterface) {
125+
$container->reset();
126+
}
127+
}
128+
static::$container = null;
129+
}
130+
}

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 5 additions & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -11,221 +11,10 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Test;
1313

14-
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
16-
use Symfony\Component\Finder\Finder;
17-
use Symfony\Component\HttpKernel\KernelInterface;
14+
use PHPUnit\Runner\Version;
1815

19-
/**
20-
* KernelTestCase is the base class for tests needing a Kernel.
21-
*
22-
* @author Fabien Potencier <[email protected]>
23-
*/
24-
abstract class KernelTestCase extends TestCase
25-
{
26-
protected static $class;
27-
28-
/**
29-
* @var KernelInterface
30-
*/
31-
protected static $kernel;
32-
33-
/**
34-
* Finds the directory where the phpunit.xml(.dist) is stored.
35-
*
36-
* If you run tests with the PHPUnit CLI tool, everything will work as expected.
37-
* If not, override this method in your test classes.
38-
*
39-
* @return string The directory where phpunit.xml(.dist) is stored
40-
*
41-
* @throws \RuntimeException
42-
*
43-
* @deprecated since 3.4 and will be removed in 4.0.
44-
*/
45-
protected static function getPhpUnitXmlDir()
46-
{
47-
@trigger_error(sprintf('The %s() method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
48-
49-
if (!isset($_SERVER['argv']) || false === strpos($_SERVER['argv'][0], 'phpunit')) {
50-
throw new \RuntimeException('You must override the KernelTestCase::createKernel() method.');
51-
}
52-
53-
$dir = static::getPhpUnitCliConfigArgument();
54-
if (null === $dir &&
55-
(is_file(getcwd().\DIRECTORY_SEPARATOR.'phpunit.xml') ||
56-
is_file(getcwd().\DIRECTORY_SEPARATOR.'phpunit.xml.dist'))) {
57-
$dir = getcwd();
58-
}
59-
60-
// Can't continue
61-
if (null === $dir) {
62-
throw new \RuntimeException('Unable to guess the Kernel directory.');
63-
}
64-
65-
if (!is_dir($dir)) {
66-
$dir = \dirname($dir);
67-
}
68-
69-
return $dir;
70-
}
71-
72-
/**
73-
* Finds the value of the CLI configuration option.
74-
*
75-
* PHPUnit will use the last configuration argument on the command line, so this only returns
76-
* the last configuration argument.
77-
*
78-
* @return string The value of the PHPUnit CLI configuration option
79-
*
80-
* @deprecated since 3.4 and will be removed in 4.0.
81-
*/
82-
private static function getPhpUnitCliConfigArgument()
83-
{
84-
@trigger_error(sprintf('The %s() method is deprecated since Symfony 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
85-
86-
$dir = null;
87-
$reversedArgs = array_reverse($_SERVER['argv']);
88-
foreach ($reversedArgs as $argIndex => $testArg) {
89-
if (preg_match('/^-[^ \-]*c$/', $testArg) || '--configuration' === $testArg) {
90-
$dir = realpath($reversedArgs[$argIndex - 1]);
91-
break;
92-
} elseif (0 === strpos($testArg, '--configuration=')) {
93-
$argPath = substr($testArg, \strlen('--configuration='));
94-
$dir = realpath($argPath);
95-
break;
96-
} elseif (0 === strpos($testArg, '-c')) {
97-
$argPath = substr($testArg, \strlen('-c'));
98-
$dir = realpath($argPath);
99-
break;
100-
}
101-
}
102-
103-
return $dir;
104-
}
105-
106-
/**
107-
* Attempts to guess the kernel location.
108-
*
109-
* When the Kernel is located, the file is required.
110-
*
111-
* @return string The Kernel class name
112-
*
113-
* @throws \RuntimeException
114-
*/
115-
protected static function getKernelClass()
116-
{
117-
if (isset($_SERVER['KERNEL_CLASS']) || isset($_ENV['KERNEL_CLASS'])) {
118-
$class = isset($_ENV['KERNEL_CLASS']) ? $_ENV['KERNEL_CLASS'] : $_SERVER['KERNEL_CLASS'];
119-
if (!class_exists($class)) {
120-
throw new \RuntimeException(sprintf('Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method.', $class, static::class));
121-
}
122-
123-
return $class;
124-
} else {
125-
@trigger_error(sprintf('Using the KERNEL_DIR environment variable or the automatic guessing based on the phpunit.xml / phpunit.xml.dist file location is deprecated since Symfony 3.4. Set the KERNEL_CLASS environment variable to the fully-qualified class name of your Kernel instead. Not setting the KERNEL_CLASS environment variable will throw an exception on 4.0 unless you override the %1$::createKernel() or %1$::getKernelClass() method.', static::class), E_USER_DEPRECATED);
126-
}
127-
128-
if (isset($_SERVER['KERNEL_DIR']) || isset($_ENV['KERNEL_DIR'])) {
129-
$dir = isset($_ENV['KERNEL_DIR']) ? $_ENV['KERNEL_DIR'] : $_SERVER['KERNEL_DIR'];
130-
131-
if (!is_dir($dir)) {
132-
$phpUnitDir = static::getPhpUnitXmlDir();
133-
if (is_dir("$phpUnitDir/$dir")) {
134-
$dir = "$phpUnitDir/$dir";
135-
}
136-
}
137-
} else {
138-
$dir = static::getPhpUnitXmlDir();
139-
}
140-
141-
$finder = new Finder();
142-
$finder->name('*Kernel.php')->depth(0)->in($dir);
143-
$results = iterator_to_array($finder);
144-
if (!\count($results)) {
145-
throw new \RuntimeException('Either set KERNEL_DIR in your phpunit.xml according to https://symfony.com/doc/current/book/testing.html#your-first-functional-test or override the WebTestCase::createKernel() method.');
146-
}
147-
148-
$file = current($results);
149-
$class = $file->getBasename('.php');
150-
151-
require_once $file;
152-
153-
return $class;
154-
}
155-
156-
/**
157-
* Boots the Kernel for this test.
158-
*
159-
* @return KernelInterface A KernelInterface instance
160-
*/
161-
protected static function bootKernel(array $options = [])
162-
{
163-
static::ensureKernelShutdown();
164-
165-
static::$kernel = static::createKernel($options);
166-
static::$kernel->boot();
167-
168-
return static::$kernel;
169-
}
170-
171-
/**
172-
* Creates a Kernel.
173-
*
174-
* Available options:
175-
*
176-
* * environment
177-
* * debug
178-
*
179-
* @return KernelInterface A KernelInterface instance
180-
*/
181-
protected static function createKernel(array $options = [])
182-
{
183-
if (null === static::$class) {
184-
static::$class = static::getKernelClass();
185-
}
186-
187-
if (isset($options['environment'])) {
188-
$env = $options['environment'];
189-
} elseif (isset($_ENV['APP_ENV'])) {
190-
$env = $_ENV['APP_ENV'];
191-
} elseif (isset($_SERVER['APP_ENV'])) {
192-
$env = $_SERVER['APP_ENV'];
193-
} else {
194-
$env = 'test';
195-
}
196-
197-
if (isset($options['debug'])) {
198-
$debug = $options['debug'];
199-
} elseif (isset($_ENV['APP_DEBUG'])) {
200-
$debug = $_ENV['APP_DEBUG'];
201-
} elseif (isset($_SERVER['APP_DEBUG'])) {
202-
$debug = $_SERVER['APP_DEBUG'];
203-
} else {
204-
$debug = true;
205-
}
206-
207-
return new static::$class($env, $debug);
208-
}
209-
210-
/**
211-
* Shuts the kernel down if it was used in the test.
212-
*/
213-
protected static function ensureKernelShutdown()
214-
{
215-
if (null !== static::$kernel) {
216-
$container = static::$kernel->getContainer();
217-
static::$kernel->shutdown();
218-
if ($container instanceof ResettableContainerInterface) {
219-
$container->reset();
220-
}
221-
}
222-
}
223-
224-
/**
225-
* Clean up Kernel usage in this test.
226-
*/
227-
protected function tearDown()
228-
{
229-
static::ensureKernelShutdown();
230-
}
16+
if (class_exists(Version::class) && version_compare(Version::id(), '8.0.0') >= 0) {
17+
class_alias(KernelTestCaseNew::class, KernelTestCase::class, true);
18+
} else {
19+
class_alias(KernelTestCaseOld::class, KernelTestCase::class, true);
23120
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
/**
15+
* KernelTestCaseNew is the base class for tests needing a Kernel >= PHPUNIT 8.
16+
*
17+
* @author Fabien Potencier <[email protected]>
18+
*
19+
* @internal
20+
*/
21+
abstract class KernelTestCaseNew extends BaseKernelTestCase
22+
{
23+
/**
24+
* Clean up Kernel usage in this test.
25+
*/
26+
protected function tearDown(): void
27+
{
28+
static::ensureKernelShutdown();
29+
}
30+
}

0 commit comments

Comments
 (0)