12
12
namespace Symfony \Bundle \FrameworkBundle \Test ;
13
13
14
14
use Symfony \Bundle \FrameworkBundle \Client ;
15
- use Symfony \Component \Finder \Finder ;
16
- use Symfony \Component \HttpKernel \KernelInterface ;
17
15
18
16
/**
19
17
* WebTestCase is the base class for functional tests.
20
18
*
21
19
* @author Fabien Potencier <[email protected] >
22
20
*/
23
- abstract class WebTestCase extends \PHPUnit_Framework_TestCase
21
+ abstract class WebTestCase extends KernelTestCase
24
22
{
25
- protected static $ class ;
26
-
27
- /**
28
- * @var KernelInterface
29
- */
30
- protected static $ kernel ;
31
-
32
23
/**
33
24
* Creates a Client.
34
25
*
@@ -39,139 +30,11 @@ abstract class WebTestCase extends \PHPUnit_Framework_TestCase
39
30
*/
40
31
protected static function createClient (array $ options = array (), array $ server = array ())
41
32
{
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 );
48
34
49
35
$ client = static ::$ kernel ->getContainer ()->get ('test.client ' );
50
36
$ client ->setServerParameters ($ server );
51
37
52
38
return $ client ;
53
39
}
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
- }
177
40
}
0 commit comments