|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Elcodi package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2014 Elcodi.com |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* Feel free to edit as you please, and have fun. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Dan Kempster <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace Axstrad\Bundle\TestBundle\Functional; |
|
15
|
|
|
|
|
16
|
|
|
use Axstrad\Component\Test\Console\Output\BufferedOutput; |
|
17
|
|
|
use Exception; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
|
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase; |
|
21
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
|
24
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Axstrad\Bundle\TestBundle\Functional\WebTestCase |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class WebTestCase extends BaseWebTestCase |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Application |
|
34
|
|
|
* |
|
35
|
|
|
* application |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $application; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Client |
|
41
|
|
|
* |
|
42
|
|
|
* Client |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $client; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @todo [BC Break] Replace the non-static property with this one. |
|
48
|
|
|
* @var Client |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $_client; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var ContainerInterface |
|
54
|
|
|
* |
|
55
|
|
|
* Container |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $container; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Set up |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setUp() |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
|
|
$this->client = self::$_client = parent::createClient( |
|
|
|
|
|
|
66
|
|
|
$this->getKernelOptions(), |
|
67
|
|
|
$this->getServerParameters() |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
static::$application = new Application(static::$kernel); |
|
71
|
|
|
static::$application->setAutoExit(false); |
|
72
|
|
|
$this->container = static::$kernel->getContainer(); |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
catch (Exception $e) { |
|
76
|
|
|
throw new RuntimeException(sprintf( |
|
77
|
|
|
'Unable to start the application: %s', |
|
78
|
|
|
get_class($e).':'.$e->getMessage() |
|
79
|
|
|
)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->createSchema(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Tear down |
|
87
|
|
|
*/ |
|
88
|
|
|
public function tearDown() |
|
89
|
|
|
{ |
|
90
|
|
|
if (!static::$application) return; |
|
91
|
|
|
|
|
92
|
|
|
$output = new BufferedOutput(); |
|
93
|
|
|
|
|
94
|
|
|
static::$application->run(new ArrayInput(array( |
|
95
|
|
|
'command' => 'doctrine:database:drop', |
|
96
|
|
|
'--no-interaction' => true, |
|
97
|
|
|
'--force' => true, |
|
98
|
|
|
'--quiet' => true, |
|
99
|
|
|
)), $output); |
|
100
|
|
|
|
|
101
|
|
|
if (!$this->runCommandsQuietly()) { |
|
102
|
|
|
echo $output->fetch(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
parent::tearDown(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Load fixtures of these bundles |
|
110
|
|
|
* |
|
111
|
|
|
* @return boolean|array Bundles name where fixtures should be found |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function loadBundlesFixtures() |
|
114
|
|
|
{ |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function getKernelOptions() |
|
122
|
|
|
{ |
|
123
|
|
|
return array(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return array An array of server parameters to pass to the test client |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getServerParameters() |
|
130
|
|
|
{ |
|
131
|
|
|
return array(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Schema must be loaded in all test cases |
|
136
|
|
|
* |
|
137
|
|
|
* @return boolean Load schema |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function loadSchema() |
|
140
|
|
|
{ |
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Should schema be loaded quietly |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function runCommandsQuietly() |
|
148
|
|
|
{ |
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Creates schema |
|
154
|
|
|
* |
|
155
|
|
|
* Only creates schema if loadSchema() is set to true. |
|
156
|
|
|
* All other methods will be loaded if this one is loaded. |
|
157
|
|
|
* |
|
158
|
|
|
* Otherwise, will return. |
|
159
|
|
|
* |
|
160
|
|
|
* @return $this self Object |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function createSchema() |
|
163
|
|
|
{ |
|
164
|
|
|
if (!$this->loadSchema()) { |
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$output = new BufferedOutput(); |
|
169
|
|
|
|
|
170
|
|
|
static::$application->run(new ArrayInput(array( |
|
171
|
|
|
'command' => 'doctrine:database:drop', |
|
172
|
|
|
'--no-interaction' => true, |
|
173
|
|
|
'--force' => true, |
|
174
|
|
|
)), $output); |
|
175
|
|
|
|
|
176
|
|
|
static::$application->run(new ArrayInput(array( |
|
177
|
|
|
'command' => 'doctrine:database:create', |
|
178
|
|
|
'--no-interaction' => true, |
|
179
|
|
|
)), $output); |
|
180
|
|
|
|
|
181
|
|
|
static::$application->run(new ArrayInput(array( |
|
182
|
|
|
'command' => 'doctrine:schema:create', |
|
183
|
|
|
'--no-interaction' => true, |
|
184
|
|
|
)), $output); |
|
185
|
|
|
|
|
186
|
|
|
if (!$this->runCommandsQuietly()) { |
|
187
|
|
|
echo $output->fetch(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$this->loadFixtures(); |
|
191
|
|
|
|
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* load fixtures method |
|
197
|
|
|
* |
|
198
|
|
|
* This method is only called if create Schema is set to true |
|
199
|
|
|
* |
|
200
|
|
|
* Only load fixtures if loadFixtures() is set to true. |
|
201
|
|
|
* All other methods will be loaded if this one is loaded. |
|
202
|
|
|
* |
|
203
|
|
|
* Otherwise, will return. |
|
204
|
|
|
* |
|
205
|
|
|
* @return $this self Object |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function loadFixtures() |
|
208
|
|
|
{ |
|
209
|
|
|
if (!is_array($this->loadBundlesFixtures())) { |
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$bundles = static::$kernel->getBundles(); |
|
214
|
|
|
$formattedBundles = array_map(function ($bundle) use ($bundles) { |
|
215
|
|
|
return $bundles[$bundle]->getPath() . '/DataFixtures/ORM/'; |
|
216
|
|
|
}, $this->loadBundlesFixtures()); |
|
217
|
|
|
|
|
218
|
|
|
$output = new BufferedOutput(); |
|
219
|
|
|
|
|
220
|
|
|
self::$application->run(new ArrayInput(array( |
|
221
|
|
|
'command' => 'doctrine:fixtures:load', |
|
222
|
|
|
'--no-interaction' => true, |
|
223
|
|
|
'--fixtures' => $formattedBundles, |
|
224
|
|
|
)), $output); |
|
225
|
|
|
|
|
226
|
|
|
if (!$this->runCommandsQuietly()) { |
|
227
|
|
|
echo $output->fetch(); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
protected static function getBundleAndTestCaseName() |
|
234
|
|
|
{ |
|
235
|
|
|
$return = array( |
|
236
|
|
|
'bundleName' => '', |
|
237
|
|
|
'bundleNamespace' => '', |
|
238
|
|
|
'testCaseName' => 'Default', |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
$appKernelLoc = '\\app\\AppKernel'; |
|
|
|
|
|
|
242
|
|
|
$namespaceExploded = explode('\\Tests\\Functional\\', get_called_class(), 2); |
|
243
|
|
|
|
|
244
|
|
|
$return['bundleNamespace'] = $namespaceExploded[0]; |
|
245
|
|
|
$bundleNamespaceParts = explode('\\', $namespaceExploded[0]); |
|
246
|
|
|
$return['bundleName'] = array_shift($bundleNamespaceParts).array_pop($bundleNamespaceParts); |
|
247
|
|
|
|
|
248
|
|
|
$parts = explode('\\', $namespaceExploded[1]); |
|
249
|
|
|
if (count($parts) > 1) { |
|
250
|
|
|
$return['testCaseName'] = array_shift($parts); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $return; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Attempts to guess the kernel location. |
|
258
|
|
|
* |
|
259
|
|
|
* When the Kernel is located, the file is required. |
|
260
|
|
|
* |
|
261
|
|
|
* @return string The Kernel class name |
|
262
|
|
|
* |
|
263
|
|
|
* @throws \RuntimeException |
|
264
|
|
|
*/ |
|
265
|
|
|
protected static function getKernelClass() |
|
266
|
|
|
{ |
|
267
|
|
|
$testInfo = self::getBundleAndTestCaseName(); |
|
268
|
|
|
|
|
269
|
|
|
$namespaceExploded = explode('\\Tests\\Functional\\', get_called_class(), 2); |
|
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
$baseNamespace = $testInfo['bundleNamespace'].'\\Tests\\Functional'; |
|
272
|
|
|
|
|
273
|
|
|
if (isset($testInfo['testCaseName']) && |
|
274
|
|
|
class_exists($baseNamespace.'\\'.$testInfo['testCaseName'].'\\app\\AppKernel') |
|
275
|
|
|
) { |
|
276
|
|
|
$baseNamespace .= '\\'.$testInfo['testCaseName']; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
$kernelClass = $baseNamespace . '\\app\\AppKernel'; |
|
280
|
|
|
|
|
281
|
|
|
return $kernelClass; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Creates a Client. |
|
286
|
|
|
* |
|
287
|
|
|
* @param array $options An array of options to pass to the createKernel class |
|
288
|
|
|
* @param array $server An array of server parameters |
|
289
|
|
|
* |
|
290
|
|
|
* @return Client A Client instance |
|
291
|
|
|
*/ |
|
292
|
|
|
protected static function createClient(array $options = array(), array $server = array()) |
|
293
|
|
|
{ |
|
294
|
|
|
return self::$_client; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Creates a Kernel. |
|
299
|
|
|
* |
|
300
|
|
|
* Available options: |
|
301
|
|
|
* |
|
302
|
|
|
* * environment |
|
303
|
|
|
* * debug |
|
304
|
|
|
* |
|
305
|
|
|
* @param array $options An array of options |
|
306
|
|
|
* |
|
307
|
|
|
* @return KernelInterface A KernelInterface instance |
|
308
|
|
|
*/ |
|
309
|
|
|
protected static function createKernel(array $options = array()) |
|
310
|
|
|
{ |
|
311
|
|
|
$testInfo = self::getBundleAndTestCaseName(); |
|
312
|
|
|
$env = $testInfo['bundleName']; |
|
313
|
|
|
if (!empty($testInfo['testCaseName'])) { |
|
314
|
|
|
$env .= '_'.$testInfo['testCaseName']; |
|
315
|
|
|
} |
|
316
|
|
|
else { |
|
317
|
|
|
$env .= 'Test'; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
static::$class = static::getKernelClass(); |
|
321
|
|
|
return new static::$class($env, true, $testInfo); |
|
322
|
|
|
} |
|
323
|
|
|
} |
|
324
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.