1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Liip/FunctionalTestBundle |
5
|
|
|
* |
6
|
|
|
* (c) Lukas Kahwe Smith <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\FunctionalTestBundle\Test; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
17
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
20
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
21
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
22
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
23
|
|
|
use Symfony\Component\HttpFoundation\Response; |
24
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
25
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
26
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
27
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
28
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
29
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
30
|
|
|
use Symfony\Bridge\Doctrine\ManagerRegistry; |
31
|
|
|
use Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader; |
32
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
33
|
|
|
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor; |
34
|
|
|
use Doctrine\Common\DataFixtures\ProxyReferenceRepository; |
35
|
|
|
use Doctrine\DBAL\Driver\PDOSqlite\Driver as SqliteDriver; |
36
|
|
|
use Doctrine\DBAL\Platforms\MySqlPlatform; |
37
|
|
|
use Doctrine\ORM\EntityManager; |
38
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
39
|
|
|
use Nelmio\Alice\Fixtures; |
40
|
|
|
use Liip\FunctionalTestBundle\Utils\FixturesLoader; |
41
|
|
|
use Liip\FunctionalTestBundle\Utils\HttpAssertions; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @author Lea Haensenberger |
45
|
|
|
* @author Lukas Kahwe Smith <[email protected]> |
46
|
|
|
* @author Benjamin Eberlei <[email protected]> |
47
|
|
|
*/ |
48
|
|
|
abstract class WebTestCase extends BaseWebTestCase |
49
|
|
|
{ |
50
|
|
|
protected $environment = 'test'; |
51
|
|
|
protected $containers; |
52
|
|
|
protected $kernelDir; |
53
|
|
|
// 5 * 1024 * 1024 KB |
54
|
|
|
protected $maxMemory = 5242880; |
55
|
|
|
|
56
|
|
|
// RUN COMMAND |
57
|
|
|
protected $verbosityLevel; |
58
|
|
|
protected $decorated; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
private $firewallLogins = array(); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private static $cachedMetadatas = array(); |
69
|
|
|
|
70
|
|
|
protected static function getKernelClass() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : static::getPhpUnitXmlDir(); |
73
|
|
|
|
74
|
|
|
list($appname) = explode('\\', get_called_class()); |
75
|
|
|
|
76
|
|
|
$class = $appname.'Kernel'; |
77
|
|
|
$file = $dir.'/'.strtolower($appname).'/'.$class.'.php'; |
78
|
|
|
if (!file_exists($file)) { |
79
|
|
|
return parent::getKernelClass(); |
80
|
|
|
} |
81
|
|
|
require_once $file; |
82
|
|
|
|
83
|
|
|
return $class; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Creates a mock object of a service identified by its id. |
88
|
|
|
* |
89
|
|
|
* @param string $id |
90
|
|
|
* |
91
|
|
|
* @return \PHPUnit_Framework_MockObject_MockBuilder |
92
|
|
|
*/ |
93
|
2 |
|
protected function getServiceMockBuilder($id) |
94
|
|
|
{ |
95
|
2 |
|
$service = $this->getContainer()->get($id); |
96
|
|
|
$class = get_class($service); |
97
|
|
|
|
98
|
|
|
return $this->getMockBuilder($class)->disableOriginalConstructor(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Builds up the environment to run the given command. |
103
|
|
|
* |
104
|
|
|
* @param string $name |
105
|
|
|
* @param array $params |
106
|
|
|
* @param bool $reuseKernel |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
13 |
|
protected function runCommand($name, array $params = array(), $reuseKernel = false) |
111
|
|
|
{ |
112
|
13 |
|
array_unshift($params, $name); |
113
|
|
|
|
114
|
13 |
|
if (!$reuseKernel) { |
115
|
13 |
|
if (null !== static::$kernel) { |
116
|
9 |
|
static::$kernel->shutdown(); |
117
|
9 |
|
} |
118
|
|
|
|
119
|
13 |
|
$kernel = static::$kernel = $this->createKernel(array('environment' => $this->environment)); |
120
|
13 |
|
$kernel->boot(); |
121
|
13 |
|
} else { |
122
|
2 |
|
$kernel = $this->getContainer()->get('kernel'); |
123
|
|
|
} |
124
|
|
|
|
125
|
13 |
|
$application = new Application($kernel); |
126
|
13 |
|
$application->setAutoExit(false); |
127
|
|
|
|
128
|
|
|
// @codeCoverageIgnoreStart |
129
|
|
|
if ('20301' === Kernel::VERSION_ID) { |
130
|
|
|
$params = $this->configureVerbosityForSymfony20301($params); |
131
|
|
|
} |
132
|
|
|
// @codeCoverageIgnoreEnd |
133
|
|
|
|
134
|
13 |
|
$input = new ArrayInput($params); |
135
|
13 |
|
$input->setInteractive(false); |
136
|
|
|
|
137
|
13 |
|
$fp = fopen('php://temp/maxmemory:'.$this->maxMemory, 'r+'); |
138
|
13 |
|
$output = new StreamOutput($fp, $this->getVerbosityLevel(), $this->getDecorated()); |
139
|
|
|
|
140
|
12 |
|
$application->run($input, $output); |
141
|
|
|
|
142
|
12 |
|
rewind($fp); |
143
|
|
|
|
144
|
12 |
|
return stream_get_contents($fp); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Retrieves the output verbosity level. |
149
|
|
|
* |
150
|
|
|
* @see Symfony\Component\Console\Output\OutputInterface for available levels |
151
|
|
|
* |
152
|
|
|
* @return int |
153
|
|
|
* |
154
|
|
|
* @throws \OutOfBoundsException If the set value isn't accepted |
155
|
|
|
*/ |
156
|
13 |
|
protected function getVerbosityLevel() |
157
|
|
|
{ |
158
|
|
|
// If `null`, is not yet set |
159
|
13 |
|
if (null === $this->verbosityLevel) { |
160
|
|
|
// Set the global verbosity level that is set as NORMAL by the TreeBuilder in Configuration |
161
|
7 |
|
$level = strtoupper($this->getContainer()->getParameter('liip_functional_test.command_verbosity')); |
162
|
7 |
|
$verbosity = '\Symfony\Component\Console\Output\StreamOutput::VERBOSITY_'.$level; |
163
|
|
|
|
164
|
7 |
|
$this->verbosityLevel = constant($verbosity); |
165
|
7 |
|
} |
166
|
|
|
|
167
|
|
|
// If string, it is set by the developer, so check that the value is an accepted one |
168
|
13 |
|
if (is_string($this->verbosityLevel)) { |
169
|
6 |
|
$level = strtoupper($this->verbosityLevel); |
170
|
6 |
|
$verbosity = '\Symfony\Component\Console\Output\StreamOutput::VERBOSITY_'.$level; |
171
|
|
|
|
172
|
6 |
|
if (!defined($verbosity)) { |
173
|
1 |
|
throw new \OutOfBoundsException( |
174
|
1 |
|
sprintf('The set value "%s" for verbosityLevel is not valid. Accepted are: "quiet", "normal", "verbose", "very_verbose" and "debug".', $level) |
175
|
1 |
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
5 |
|
$this->verbosityLevel = constant($verbosity); |
179
|
5 |
|
} |
180
|
|
|
|
181
|
12 |
|
return $this->verbosityLevel; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* In Symfony 2.3.1 the verbosity level has to be set through {Symfony\Component\Console\Input\ArrayInput} and not |
186
|
|
|
* in {Symfony\Component\Console\Output\OutputInterface}. |
187
|
|
|
* |
188
|
|
|
* This method builds $params to be passed to {Symfony\Component\Console\Input\ArrayInput}. |
189
|
|
|
* |
190
|
|
|
* @codeCoverageIgnore |
191
|
|
|
* |
192
|
|
|
* @param array $params |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
private function configureVerbosityForSymfony20301(array $params) |
197
|
|
|
{ |
198
|
|
|
switch ($this->getVerbosityLevel()) { |
199
|
|
|
case OutputInterface::VERBOSITY_QUIET: |
200
|
|
|
$params['-q'] = '-q'; |
201
|
|
|
break; |
202
|
|
|
|
203
|
|
|
case OutputInterface::VERBOSITY_VERBOSE: |
204
|
|
|
$params['-v'] = ''; |
205
|
|
|
break; |
206
|
|
|
|
207
|
|
|
case OutputInterface::VERBOSITY_VERY_VERBOSE: |
208
|
|
|
$params['-vv'] = ''; |
209
|
|
|
break; |
210
|
|
|
|
211
|
|
|
case OutputInterface::VERBOSITY_DEBUG: |
212
|
|
|
$params['-vvv'] = ''; |
213
|
|
|
break; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return $params; |
217
|
|
|
} |
218
|
|
|
|
219
|
6 |
|
public function setVerbosityLevel($level) |
220
|
|
|
{ |
221
|
6 |
|
$this->verbosityLevel = $level; |
222
|
6 |
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Retrieves the flag indicating if the output should be decorated or not. |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
12 |
|
protected function getDecorated() |
230
|
|
|
{ |
231
|
12 |
|
if (null === $this->decorated) { |
232
|
|
|
// Set the global decoration flag that is set to `true` by the TreeBuilder in Configuration |
233
|
5 |
|
$this->decorated = $this->getContainer()->getParameter('liip_functional_test.command_decoration'); |
234
|
5 |
|
} |
235
|
|
|
|
236
|
|
|
// Check the local decorated flag |
237
|
12 |
|
if (false === is_bool($this->decorated)) { |
238
|
|
|
throw new \OutOfBoundsException( |
239
|
|
|
sprintf('`WebTestCase::decorated` has to be `bool`. "%s" given.', gettype($this->decorated)) |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
|
243
|
12 |
|
return $this->decorated; |
244
|
|
|
} |
245
|
|
|
|
246
|
7 |
|
public function isDecorated($decorated) |
247
|
|
|
{ |
248
|
7 |
|
$this->decorated = $decorated; |
249
|
7 |
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get an instance of the dependency injection container. |
253
|
|
|
* (this creates a kernel *without* parameters). |
254
|
|
|
* |
255
|
|
|
* @return ContainerInterface |
256
|
|
|
*/ |
257
|
46 |
|
protected function getContainer() |
|
|
|
|
258
|
|
|
{ |
259
|
46 |
|
if (!empty($this->kernelDir)) { |
260
|
|
|
$tmpKernelDir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : null; |
261
|
|
|
$_SERVER['KERNEL_DIR'] = getcwd().$this->kernelDir; |
262
|
|
|
} |
263
|
|
|
|
264
|
46 |
|
$cacheKey = $this->kernelDir.'|'.$this->environment; |
265
|
46 |
|
if (empty($this->containers[$cacheKey])) { |
266
|
|
|
$options = array( |
267
|
45 |
|
'environment' => $this->environment, |
268
|
45 |
|
); |
269
|
45 |
|
$kernel = $this->createKernel($options); |
270
|
45 |
|
$kernel->boot(); |
271
|
|
|
|
272
|
45 |
|
$this->containers[$cacheKey] = $kernel->getContainer(); |
273
|
45 |
|
} |
274
|
|
|
|
275
|
46 |
|
if (isset($tmpKernelDir)) { |
276
|
|
|
$_SERVER['KERNEL_DIR'] = $tmpKernelDir; |
277
|
|
|
} |
278
|
|
|
|
279
|
46 |
|
return $this->containers[$cacheKey]; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Get metadatas from cache. |
284
|
|
|
* |
285
|
|
|
* @param ObjectManager $om |
286
|
|
|
* @param string $omName The name of object manager to use |
287
|
|
|
* |
288
|
|
|
* @return array |
289
|
|
|
*/ |
290
|
27 |
|
private function getCachedMetadatas(ObjectManager $om, $omName) |
291
|
|
|
{ |
292
|
27 |
|
if (!isset(self::$cachedMetadatas[$omName])) { |
293
|
10 |
|
self::$cachedMetadatas[$omName] = $om->getMetadataFactory()->getAllMetadata(); |
294
|
10 |
|
usort(self::$cachedMetadatas[$omName], function ($a, $b) { |
295
|
|
|
return strcmp($a->name, $b->name); |
296
|
10 |
|
}); |
297
|
10 |
|
} |
298
|
|
|
|
299
|
27 |
|
return self::$cachedMetadatas[$omName]; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Copy SQLite backup file. |
304
|
|
|
* |
305
|
|
|
* @param ObjectManager $om |
306
|
|
|
* @param string $executorClass |
307
|
|
|
* @param ProxyReferenceRepository $referenceRepository |
308
|
|
|
* @param string $backup Path of the source file. |
309
|
|
|
* @param string $name Path of the destination file. |
310
|
|
|
* |
311
|
|
|
* @return \Doctrine\Common\DataFixtures\Executor\ORMExecutor|\Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor |
312
|
|
|
*/ |
313
|
7 |
|
private function copySqliteBackup($om, $executorClass, |
314
|
|
|
$referenceRepository, $backup, $name) |
315
|
|
|
{ |
316
|
7 |
|
$this->preFixtureRestore($om, $referenceRepository); |
317
|
|
|
|
318
|
7 |
|
copy($backup, $name); |
319
|
|
|
|
320
|
7 |
|
$executor = new $executorClass($om); |
321
|
7 |
|
$executor->setReferenceRepository($referenceRepository); |
322
|
7 |
|
$executor->getReferenceRepository()->load($backup); |
323
|
|
|
|
324
|
7 |
|
$this->postFixtureRestore(); |
325
|
|
|
|
326
|
7 |
|
return $executor; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Purge database. |
331
|
|
|
* |
332
|
|
|
* @param ObjectManager $om |
333
|
|
|
* @param array $metadatas |
334
|
|
|
* @param string $executorClass |
335
|
|
|
* @param ProxyReferenceRepository $referenceRepository |
336
|
|
|
* |
337
|
|
|
* @return \Doctrine\Common\DataFixtures\Executor\ORMExecutor|\Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor |
338
|
|
|
*/ |
339
|
21 |
|
private function createSqliteSchema(ObjectManager $om, |
340
|
|
|
$metadatas, $executorClass, |
341
|
|
|
ProxyReferenceRepository $referenceRepository) |
342
|
|
|
{ |
343
|
|
|
// TODO: handle case when using persistent connections. Fail loudly? |
344
|
21 |
|
$schemaTool = new SchemaTool($om); |
|
|
|
|
345
|
21 |
|
$schemaTool->dropDatabase(); |
346
|
21 |
|
if (!empty($metadatas)) { |
347
|
21 |
|
$schemaTool->createSchema($metadatas); |
348
|
21 |
|
} |
349
|
21 |
|
$this->postFixtureSetup(); |
350
|
|
|
|
351
|
21 |
|
$executor = new $executorClass($om); |
352
|
21 |
|
$executor->setReferenceRepository($referenceRepository); |
353
|
|
|
|
354
|
21 |
|
return $executor; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Set the database to the provided fixtures. |
359
|
|
|
* |
360
|
|
|
* Drops the current database and then loads fixtures using the specified |
361
|
|
|
* classes. The parameter is a list of fully qualified class names of |
362
|
|
|
* classes that implement Doctrine\Common\DataFixtures\FixtureInterface |
363
|
|
|
* so that they can be loaded by the DataFixtures Loader::addFixture |
364
|
|
|
* |
365
|
|
|
* When using SQLite this method will automatically make a copy of the |
366
|
|
|
* loaded schema and fixtures which will be restored automatically in |
367
|
|
|
* case the same fixture classes are to be loaded again. Caveat: changes |
368
|
|
|
* to references and/or identities may go undetected. |
369
|
|
|
* |
370
|
|
|
* Depends on the doctrine data-fixtures library being available in the |
371
|
|
|
* class path. |
372
|
|
|
* |
373
|
|
|
* @param array $classNames List of fully qualified class names of fixtures to load |
374
|
|
|
* @param string $omName The name of object manager to use |
375
|
|
|
* @param string $registryName The service id of manager registry to use |
376
|
|
|
* @param int $purgeMode Sets the ORM purge mode |
377
|
|
|
* |
378
|
|
|
* @return null|AbstractExecutor |
379
|
|
|
*/ |
380
|
32 |
|
protected function loadFixtures(array $classNames, $omName = null, $registryName = 'doctrine', $purgeMode = null) |
381
|
|
|
{ |
382
|
32 |
|
$container = $this->getContainer(); |
383
|
|
|
/** @var ManagerRegistry $registry */ |
384
|
32 |
|
$registry = $container->get($registryName); |
385
|
|
|
/** @var ObjectManager $om */ |
386
|
32 |
|
$om = $registry->getManager($omName); |
387
|
32 |
|
$type = $registry->getName(); |
388
|
|
|
|
389
|
32 |
|
$executorClass = FixturesLoader::getExecutorClass($type); |
390
|
32 |
|
$referenceRepository = new ProxyReferenceRepository($om); |
391
|
32 |
|
$cacheDriver = $om->getMetadataFactory()->getCacheDriver(); |
392
|
|
|
|
393
|
32 |
|
if ($cacheDriver) { |
394
|
32 |
|
$cacheDriver->deleteAll(); |
395
|
32 |
|
} |
396
|
|
|
|
397
|
32 |
|
if ('ORM' === $type) { |
398
|
31 |
|
$connection = $om->getConnection(); |
399
|
31 |
|
if ($connection->getDriver() instanceof SqliteDriver) { |
400
|
27 |
|
$name = FixturesLoader::getNameParameter($connection); |
401
|
27 |
|
$metadatas = self::getCachedMetadatas($om, $omName); |
402
|
|
|
|
403
|
27 |
|
if ($container->getParameter('liip_functional_test.cache_sqlite_db')) { |
404
|
9 |
|
$backup = $container->getParameter('kernel.cache_dir').'/test_'.md5(serialize($metadatas).serialize($classNames)).'.db'; |
405
|
9 |
|
if (file_exists($backup) && file_exists($backup.'.ser') && FixturesLoader::isBackupUpToDate($classNames, $backup, $container)) { |
406
|
7 |
|
$om->flush(); |
407
|
7 |
|
$om->clear(); |
408
|
|
|
|
409
|
7 |
|
$executor = $this->copySqliteBackup($om, |
410
|
7 |
|
$executorClass, $referenceRepository, |
411
|
7 |
|
$backup, $name); |
412
|
|
|
|
413
|
7 |
|
return $executor; |
414
|
|
|
} |
415
|
3 |
|
} |
416
|
|
|
|
417
|
21 |
|
$executor = $this->createSqliteSchema($om, $metadatas, |
418
|
21 |
|
$executorClass, $referenceRepository); |
419
|
21 |
|
} |
420
|
25 |
|
} |
421
|
|
|
|
422
|
26 |
|
if (empty($executor)) { |
423
|
5 |
|
$executor = FixturesLoader::purgeDatabase($om, $type, $purgeMode, |
424
|
5 |
|
$executorClass, $referenceRepository, $container); |
425
|
5 |
|
} |
426
|
|
|
|
427
|
26 |
|
$loader = FixturesLoader::getFixtureLoader($container, $classNames); |
428
|
|
|
|
429
|
26 |
|
$executor->execute($loader->getFixtures(), true); |
430
|
|
|
|
431
|
26 |
|
if (isset($name) && isset($backup)) { |
432
|
3 |
|
$this->preReferenceSave($om, $executor, $backup); |
433
|
|
|
|
434
|
3 |
|
$executor->getReferenceRepository()->save($backup); |
435
|
3 |
|
copy($name, $backup); |
436
|
|
|
|
437
|
3 |
|
$this->postReferenceSave($om, $executor, $backup); |
438
|
3 |
|
} |
439
|
|
|
|
440
|
26 |
|
return $executor; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Clean database. |
445
|
|
|
* |
446
|
|
|
* @param ManagerRegistry $registry |
447
|
|
|
* @param EntityManager $om |
448
|
|
|
*/ |
449
|
6 |
|
private function cleanDatabase(ManagerRegistry $registry, EntityManager $om) |
450
|
|
|
{ |
451
|
6 |
|
$connection = $om->getConnection(); |
452
|
|
|
|
453
|
6 |
|
$mysql = ($registry->getName() === 'ORM' |
454
|
6 |
|
&& $connection->getDatabasePlatform() instanceof MySqlPlatform); |
455
|
|
|
|
456
|
6 |
|
if ($mysql) { |
457
|
1 |
|
$connection->query('SET FOREIGN_KEY_CHECKS=0'); |
458
|
1 |
|
} |
459
|
|
|
|
460
|
6 |
|
$this->loadFixtures(array()); |
461
|
|
|
|
462
|
6 |
|
if ($mysql) { |
463
|
1 |
|
$connection->query('SET FOREIGN_KEY_CHECKS=1'); |
464
|
1 |
|
} |
465
|
6 |
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @param array $paths Either symfony resource locators (@ BundleName/etc) or actual file paths |
469
|
|
|
* @param bool $append |
470
|
|
|
* @param null $omName |
471
|
|
|
* @param string $registryName |
472
|
|
|
* |
473
|
|
|
* @return array |
474
|
|
|
* |
475
|
|
|
* @throws \BadMethodCallException |
476
|
|
|
*/ |
477
|
6 |
|
public function loadFixtureFiles(array $paths = array(), $append = false, $omName = null, $registryName = 'doctrine') |
478
|
|
|
{ |
479
|
6 |
|
if (!class_exists('Nelmio\Alice\Fixtures')) { |
480
|
|
|
// This class is available during tests, no exception will be thrown. |
481
|
|
|
// @codeCoverageIgnoreStart |
482
|
|
|
throw new \BadMethodCallException('nelmio/alice should be installed to use this method.'); |
483
|
|
|
// @codeCoverageIgnoreEnd |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
/** @var ContainerInterface $container */ |
487
|
6 |
|
$container = $this->getContainer(); |
488
|
|
|
|
489
|
|
|
/** @var ManagerRegistry $registry */ |
490
|
6 |
|
$registry = $container->get($registryName); |
491
|
|
|
|
492
|
|
|
/** @var EntityManager $om */ |
493
|
6 |
|
$om = $registry->getManager($omName); |
494
|
|
|
|
495
|
6 |
|
if ($append === false) { |
496
|
6 |
|
$this->cleanDatabase($registry, $om); |
497
|
6 |
|
} |
498
|
|
|
|
499
|
6 |
|
$files = FixturesLoader::locateResources($paths, $container); |
500
|
|
|
|
501
|
|
|
// Check if the Hautelook AliceBundle is registered and if yes, use it instead of Nelmio Alice |
502
|
6 |
|
$hautelookLoaderServiceName = 'hautelook_alice.fixtures.loader'; |
503
|
6 |
|
if ($container->has($hautelookLoaderServiceName)) { |
504
|
3 |
|
$loaderService = $container->get($hautelookLoaderServiceName); |
505
|
3 |
|
$persisterClass = class_exists('Nelmio\Alice\ORM\Doctrine') ? |
506
|
3 |
|
'Nelmio\Alice\ORM\Doctrine' : |
507
|
3 |
|
'Nelmio\Alice\Persister\Doctrine'; |
508
|
|
|
|
509
|
3 |
|
return $loaderService->load(new $persisterClass($om), $files); |
510
|
|
|
} |
511
|
|
|
|
512
|
3 |
|
return Fixtures::load($files, $om); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Callback function to be executed after Schema creation. |
517
|
|
|
* Use this to execute acl:init or other things necessary. |
518
|
|
|
*/ |
519
|
21 |
|
protected function postFixtureSetup() |
520
|
|
|
{ |
521
|
21 |
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Callback function to be executed after Schema restore. |
525
|
|
|
* |
526
|
|
|
* @return WebTestCase |
527
|
|
|
*/ |
528
|
7 |
|
protected function postFixtureRestore() |
529
|
|
|
{ |
530
|
7 |
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Callback function to be executed before Schema restore. |
534
|
|
|
* |
535
|
|
|
* @param ObjectManager $manager The object manager |
536
|
|
|
* @param ProxyReferenceRepository $referenceRepository The reference repository |
537
|
|
|
* |
538
|
|
|
* @return WebTestCase |
539
|
|
|
*/ |
540
|
7 |
|
protected function preFixtureRestore(ObjectManager $manager, ProxyReferenceRepository $referenceRepository) |
|
|
|
|
541
|
|
|
{ |
542
|
7 |
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Callback function to be executed after save of references. |
546
|
|
|
* |
547
|
|
|
* @param ObjectManager $manager The object manager |
548
|
|
|
* @param AbstractExecutor $executor Executor of the data fixtures |
549
|
|
|
* @param string $backupFilePath Path of file used to backup the references of the data fixtures |
550
|
|
|
* |
551
|
|
|
* @return WebTestCase |
552
|
|
|
*/ |
553
|
3 |
|
protected function postReferenceSave(ObjectManager $manager, AbstractExecutor $executor, $backupFilePath) |
|
|
|
|
554
|
|
|
{ |
555
|
3 |
|
} |
556
|
|
|
|
557
|
|
|
/** |
558
|
|
|
* Callback function to be executed before save of references. |
559
|
|
|
* |
560
|
|
|
* @param ObjectManager $manager The object manager |
561
|
|
|
* @param AbstractExecutor $executor Executor of the data fixtures |
562
|
|
|
* @param string $backupFilePath Path of file used to backup the references of the data fixtures |
563
|
|
|
* |
564
|
|
|
* @return WebTestCase |
565
|
|
|
*/ |
566
|
3 |
|
protected function preReferenceSave(ObjectManager $manager, AbstractExecutor $executor, $backupFilePath) |
|
|
|
|
567
|
|
|
{ |
568
|
3 |
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Creates an instance of a lightweight Http client. |
572
|
|
|
* |
573
|
|
|
* If $authentication is set to 'true' it will use the content of |
574
|
|
|
* 'liip_functional_test.authentication' to log in. |
575
|
|
|
* |
576
|
|
|
* $params can be used to pass headers to the client, note that they have |
577
|
|
|
* to follow the naming format used in $_SERVER. |
578
|
|
|
* Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
579
|
|
|
* |
580
|
|
|
* @param bool|array $authentication |
581
|
|
|
* @param array $params |
582
|
|
|
* |
583
|
|
|
* @return Client |
584
|
|
|
*/ |
585
|
49 |
|
protected function makeClient($authentication = false, array $params = array()) |
586
|
|
|
{ |
587
|
49 |
|
if ($authentication) { |
588
|
2 |
|
if ($authentication === true) { |
589
|
|
|
$authentication = array( |
590
|
1 |
|
'username' => $this->getContainer() |
591
|
1 |
|
->getParameter('liip_functional_test.authentication.username'), |
592
|
1 |
|
'password' => $this->getContainer() |
593
|
1 |
|
->getParameter('liip_functional_test.authentication.password'), |
594
|
1 |
|
); |
595
|
1 |
|
} |
596
|
|
|
|
597
|
2 |
|
$params = array_merge($params, array( |
598
|
2 |
|
'PHP_AUTH_USER' => $authentication['username'], |
599
|
2 |
|
'PHP_AUTH_PW' => $authentication['password'], |
600
|
2 |
|
)); |
601
|
2 |
|
} |
602
|
|
|
|
603
|
49 |
|
$client = static::createClient(array('environment' => $this->environment), $params); |
604
|
|
|
|
605
|
49 |
|
if ($this->firewallLogins) { |
|
|
|
|
606
|
|
|
// has to be set otherwise "hasPreviousSession" in Request returns false. |
607
|
2 |
|
$options = $client->getContainer()->getParameter('session.storage.options'); |
608
|
|
|
|
609
|
2 |
|
if (!$options || !isset($options['name'])) { |
610
|
|
|
throw new \InvalidArgumentException('Missing session.storage.options#name'); |
611
|
|
|
} |
612
|
|
|
|
613
|
2 |
|
$session = $client->getContainer()->get('session'); |
614
|
|
|
// Since the namespace of the session changed in symfony 2.1, instanceof can be used to check the version. |
615
|
2 |
|
if ($session instanceof Session) { |
616
|
2 |
|
$session->setId(uniqid()); |
617
|
2 |
|
} |
618
|
|
|
|
619
|
2 |
|
$client->getCookieJar()->set(new Cookie($options['name'], $session->getId())); |
620
|
|
|
|
621
|
|
|
/** @var $user UserInterface */ |
622
|
2 |
|
foreach ($this->firewallLogins as $firewallName => $user) { |
623
|
2 |
|
$token = $this->createUserToken($user, $firewallName); |
624
|
|
|
|
625
|
|
|
// BC: security.token_storage is available on Symfony 2.6+ |
626
|
|
|
// see http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements |
627
|
2 |
|
if ($client->getContainer()->has('security.token_storage')) { |
628
|
2 |
|
$tokenStorage = $client->getContainer()->get('security.token_storage'); |
629
|
2 |
|
} else { |
630
|
|
|
// This block will never be reached with Symfony 2.6+ |
631
|
|
|
// @codeCoverageIgnoreStart |
632
|
|
|
$tokenStorage = $client->getContainer()->get('security.context'); |
633
|
|
|
// @codeCoverageIgnoreEnd |
634
|
|
|
} |
635
|
|
|
|
636
|
2 |
|
$tokenStorage->setToken($token); |
637
|
2 |
|
$session->set('_security_'.$firewallName, serialize($token)); |
638
|
2 |
|
} |
639
|
|
|
|
640
|
2 |
|
$session->save(); |
641
|
2 |
|
} |
642
|
|
|
|
643
|
49 |
|
return $client; |
644
|
|
|
} |
645
|
|
|
|
646
|
|
|
/** |
647
|
|
|
* Create User Token. |
648
|
|
|
* |
649
|
|
|
* Factory method for creating a User Token object for the firewall based on |
650
|
|
|
* the user object provided. By default it will be a Username/Password |
651
|
|
|
* Token based on the user's credentials, but may be overridden for custom |
652
|
|
|
* tokens in your applications. |
653
|
|
|
* |
654
|
|
|
* @param UserInterface $user The user object to base the token off of |
655
|
|
|
* @param string $firewallName name of the firewall provider to use |
656
|
|
|
* |
657
|
|
|
* @return TokenInterface The token to be used in the security context |
658
|
|
|
*/ |
659
|
2 |
|
protected function createUserToken(UserInterface $user, $firewallName) |
660
|
|
|
{ |
661
|
2 |
|
return new UsernamePasswordToken( |
662
|
2 |
|
$user, |
663
|
2 |
|
null, |
664
|
2 |
|
$firewallName, |
665
|
2 |
|
$user->getRoles() |
666
|
2 |
|
); |
667
|
|
|
} |
668
|
|
|
|
669
|
|
|
/** |
670
|
|
|
* Extracts the location from the given route. |
671
|
|
|
* |
672
|
|
|
* @param string $route The name of the route |
673
|
|
|
* @param array $params Set of parameters |
674
|
|
|
* @param int $absolute |
675
|
|
|
* |
676
|
|
|
* @return string |
677
|
|
|
*/ |
678
|
1 |
|
protected function getUrl($route, $params = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH) |
679
|
|
|
{ |
680
|
1 |
|
return $this->getContainer()->get('router')->generate($route, $params, $absolute); |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* Checks the success state of a response. |
685
|
|
|
* |
686
|
|
|
* @param Response $response Response object |
687
|
|
|
* @param bool $success to define whether the response is expected to be successful |
688
|
|
|
* @param string $type |
689
|
|
|
*/ |
690
|
6 |
|
public function isSuccessful(Response $response, $success = true, $type = 'text/html') |
691
|
|
|
{ |
692
|
6 |
|
HttpAssertions::isSuccessful($response, $success, $type); |
693
|
5 |
|
} |
694
|
|
|
|
695
|
|
|
/** |
696
|
|
|
* Executes a request on the given url and returns the response contents. |
697
|
|
|
* |
698
|
|
|
* This method also asserts the request was successful. |
699
|
|
|
* |
700
|
|
|
* @param string $path path of the requested page |
701
|
|
|
* @param string $method The HTTP method to use, defaults to GET |
702
|
|
|
* @param bool $authentication Whether to use authentication, defaults to false |
703
|
|
|
* @param bool $success to define whether the response is expected to be successful |
704
|
|
|
* |
705
|
|
|
* @return string |
706
|
|
|
*/ |
707
|
1 |
|
public function fetchContent($path, $method = 'GET', $authentication = false, $success = true) |
708
|
|
|
{ |
709
|
1 |
|
$client = $this->makeClient($authentication); |
710
|
1 |
|
$client->request($method, $path); |
711
|
|
|
|
712
|
1 |
|
$content = $client->getResponse()->getContent(); |
713
|
1 |
|
if (is_bool($success)) { |
714
|
1 |
|
$this->isSuccessful($client->getResponse(), $success); |
715
|
1 |
|
} |
716
|
|
|
|
717
|
1 |
|
return $content; |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
/** |
721
|
|
|
* Executes a request on the given url and returns a Crawler object. |
722
|
|
|
* |
723
|
|
|
* This method also asserts the request was successful. |
724
|
|
|
* |
725
|
|
|
* @param string $path path of the requested page |
726
|
|
|
* @param string $method The HTTP method to use, defaults to GET |
727
|
|
|
* @param bool $authentication Whether to use authentication, defaults to false |
728
|
|
|
* @param bool $success Whether the response is expected to be successful |
729
|
|
|
* |
730
|
|
|
* @return Crawler |
731
|
|
|
*/ |
732
|
1 |
|
public function fetchCrawler($path, $method = 'GET', $authentication = false, $success = true) |
733
|
|
|
{ |
734
|
1 |
|
$client = $this->makeClient($authentication); |
735
|
1 |
|
$crawler = $client->request($method, $path); |
736
|
|
|
|
737
|
1 |
|
$this->isSuccessful($client->getResponse(), $success); |
738
|
|
|
|
739
|
1 |
|
return $crawler; |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
743
|
|
|
* @param UserInterface $user |
744
|
|
|
* @param string $firewallName |
745
|
|
|
* |
746
|
|
|
* @return WebTestCase |
747
|
|
|
*/ |
748
|
2 |
|
public function loginAs(UserInterface $user, $firewallName) |
749
|
|
|
{ |
750
|
2 |
|
$this->firewallLogins[$firewallName] = $user; |
751
|
|
|
|
752
|
2 |
|
return $this; |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
/** |
756
|
|
|
* Asserts that the HTTP response code of the last request performed by |
757
|
|
|
* $client matches the expected code. If not, raises an error with more |
758
|
|
|
* information. |
759
|
|
|
* |
760
|
|
|
* @param $expectedStatusCode |
761
|
|
|
* @param Client $client |
762
|
|
|
*/ |
763
|
11 |
|
public function assertStatusCode($expectedStatusCode, Client $client) |
764
|
|
|
{ |
765
|
11 |
|
HttpAssertions::assertStatusCode($expectedStatusCode, $client); |
766
|
8 |
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* Assert that the last validation errors within $container match the |
770
|
|
|
* expected keys. |
771
|
|
|
* |
772
|
|
|
* @param array $expected A flat array of field names |
773
|
|
|
* @param ContainerInterface $container |
774
|
|
|
*/ |
775
|
2 |
|
public function assertValidationErrors(array $expected, ContainerInterface $container) |
776
|
|
|
{ |
777
|
2 |
|
HttpAssertions::assertValidationErrors($expected, $container); |
778
|
1 |
|
} |
779
|
|
|
} |
780
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: