Completed
Pull Request — master (#230)
by Adamo
06:30
created

WebTestCase::isDecorated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Security\Core\Authentication\Token\TokenInterface;
23
use Symfony\Component\Security\Core\User\UserInterface;
24
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
use Symfony\Component\HttpFoundation\Session\Session;
27
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
28
use Symfony\Bridge\Doctrine\ManagerRegistry;
29
use Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader;
30
use Doctrine\Common\Persistence\ObjectManager;
31
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
32
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor;
33
use Doctrine\Common\DataFixtures\ProxyReferenceRepository;
34
use Doctrine\DBAL\Driver\PDOSqlite\Driver as SqliteDriver;
35
use Doctrine\DBAL\Platforms\MySqlPlatform;
36
use Doctrine\ORM\Tools\SchemaTool;
37
use Nelmio\Alice\Fixtures;
38
39
/**
40
 * @author Lea Haensenberger
41
 * @author Lukas Kahwe Smith <[email protected]>
42
 * @author Benjamin Eberlei <[email protected]>
43
 */
44
abstract class WebTestCase extends BaseWebTestCase
45
{
46
    protected $environment = 'test';
47
    protected $containers;
48
    protected $kernelDir;
49
    // 5 * 1024 * 1024 KB
50
    protected $maxMemory = 5242880;
51
52
    // RUN COMMAND
53
    protected $verbosityLevel;
54
    protected $decorated;
55
56
    /**
57
     * @var array
58
     */
59
    private $firewallLogins = array();
60
61
    /**
62
     * @var array
63 1
     */
64
    private static $cachedMetadatas = array();
65 1
66
    protected static function getKernelClass()
0 ignored issues
show
Coding Style introduced by
getKernelClass uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
67 1
    {
68
        $dir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : self::getPhpUnitXmlDir();
69 1
70 1
        list($appname) = explode('\\', get_called_class());
71 1
72 1
        $class = $appname.'Kernel';
73
        $file = $dir.'/'.strtolower($appname).'/'.$class.'.php';
74
        if (!file_exists($file)) {
75
            return parent::getKernelClass();
76
        }
77
        require_once $file;
78
79
        return $class;
80
    }
81
82
    /**
83
     * Creates a mock object of a service identified by its id.
84
     *
85
     * @param string $id
86
     *
87
     * @return PHPUnit_Framework_MockObject_MockBuilder
88
     */
89
    protected function getServiceMockBuilder($id)
90
    {
91
        $service = $this->getContainer()->get($id);
92
        $class = get_class($service);
93
94
        return $this->getMockBuilder($class)->disableOriginalConstructor();
95
    }
96
97
    /**
98
     * Builds up the environment to run the given command.
99
     *
100
     * @param string $name
101
     * @param array  $params
102
     * @param bool   $reuseKernel
103 1
     *
104
     * @return string
105 1
     */
106
    protected function runCommand($name, array $params = array(), $reuseKernel = false)
107 1
    {
108 1
        array_unshift($params, $name);
109 1
110 1
        if (!$reuseKernel) {
111
            if (null !== static::$kernel) {
112 1
                static::$kernel->shutdown();
113 1
            }
114 1
115 1
            $kernel = static::$kernel = $this->createKernel(array('environment' => $this->environment));
116
            $kernel->boot();
117
        } else {
118 1
            $kernel = $this->getContainer()->get('kernel');
119 1
        }
120
121 1
        $application = new Application($kernel);
122 1
        $application->setAutoExit(false);
123
124 1
        if (\Symfony\Component\HttpKernel\Kernel::VERSION_ID === 20301) {
125 1
            $params = $this->configureVerbosityForSymfony20301($params);
126
127 1
            var_dump("\nParams");
0 ignored issues
show
Security Debugging Code introduced by
var_dump(' Params'); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
128
            var_dump($params);
129 1
        }
130
131 1
        $input = new ArrayInput($params);
132
        $input->setInteractive(false);
133
134
        $fp = fopen('php://temp/maxmemory:'.$this->maxMemory, 'r+');
135
        var_dump(printf("\n\n---Start debugging.---\n\n"));
136
        var_dump(sprintf("\nDEBUG: [runCommand getVerbosityLevel] to set in StreamOutput = %s;", $this->getVerbosityLevel()));
137
        $output = new StreamOutput($fp, $this->getVerbosityLevel(), $this->getDecorated());
138
        var_dump(sprintf("\nDEBUG: [runCommand getVerbosityLevel] set in StreamOutput (pre RUN) = %s;", $output->getVerbosity()));
139
140
        $application->run($input, $output);
141 1
142
        var_dump(sprintf("\nDEBUG: [runCommand getVerbosityLevel] set in StreamOutput (post RUN) = %s;", $output->getVerbosity()));
143
        var_dump(printf("\n\n---End debugging.---\n\n"));
144 1
145 1
        rewind($fp);
146 1
147
        return stream_get_contents($fp);
148
    }
149 1
150
    /**
151
     * Retrieves the output verbosity level.
152 1
     *
153 1
     * @see Symfony\Component\Console\Output\OutputInterface for available levels
154 1
     *
155
     * @return integer
156
     * @throws \OutOfBoundsException If the set value isn't accepted
157 1
     */
158
    protected function getVerbosityLevel()
159
    {
160 1
        // If `null`, is not yet set
161
        if (null === $this->verbosityLevel) {
162
            var_dump(sprintf("\nDEBUG: [getVerbosityLevel] this->verbosityLevel = %s;", $this->verbosityLevel));
0 ignored issues
show
Security Debugging Code introduced by
var_dump(sprintf(' DEBUG...this->verbosityLevel)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
163
            // Set the global verbosity level that is set as NORMAL by the TreeBuilder in Configuration
164
            $level = strtoupper($this->getContainer()->getParameter('liip_functional_test.command_verbosity'));
165
            $verbosity = '\Symfony\Component\Console\Output\StreamOutput::VERBOSITY_'.$level;
166
167
            $this->verbosityLevel = constant($verbosity);
168 1
169
            var_dump(sprintf("\nDEBUG: [getVerbosityLevel] this->verbosityLevel from container = %s;", $this->verbosityLevel));
170
        }
171 1
172 1
        // If string, it is set by the developer, so check that the value is an accepted one
173
        if (is_string($this->verbosityLevel)) {
174
            var_dump(sprintf("\nDEBUG: [getVerbosityLevel] this->verbosityLevel = %s;", $this->verbosityLevel));
175
            $level = strtoupper($this->verbosityLevel);
176 1
            $verbosity = '\Symfony\Component\Console\Output\StreamOutput::VERBOSITY_'.$level;
177 1
178
            if (null === constant($verbosity)) {
179
                throw new \OutOfBoundsException(
180
                    'The set value "%s" for verbosityLevel is not valid. Accepted are: "quiet", "normal", "verbose", "very_verbose" and "debug".
181
                    ');
182
            }
183
184
            $this->verbosityLevel = constant($verbosity);
185
            var_dump(sprintf("\nDEBUG: [getVerbosityLevel] Now this->verbosityLevel = %s;", $this->verbosityLevel));
186
        }
187
188
        var_dump(sprintf("\nDEBUG: [getVerbosityLevel] Returned this->verbosityLevel = %s;", $this->verbosityLevel));
189
        return $this->verbosityLevel;
190 25
    }
191
192 25
    private function configureVerbosityForSymfony20301(array $params) {
193
        switch($this->getVerbosityLevel()) {
194
            case OutputInterface::VERBOSITY_QUIET:
195
                $params['-q'] = '-q';
196
                break;
197 25
198 25
            case OutputInterface::VERBOSITY_NORMAL:
199
                $params['-v'] = '';
200 25
                break;
201 25
202 25
            case OutputInterface::VERBOSITY_VERBOSE:
203 25
                $params['-vv'] = '';
204
                break;
205 25
206 25
            case OutputInterface::VERBOSITY_VERY_VERBOSE:
207
                $params['-vvv'] = '';
208 25
                break;
209
        }
210
211
        return $params;
212 25
    }
213
214
    public function setVerbosityLevel($level)
215
    {
216
        $this->verbosityLevel = $level;
217
    }
218
219
    /**
220
     * Retrieves the flag indicating if the output should be decorated or not.
221
     *
222
     * @return bool
223
     */
224
    protected function getDecorated()
225 5
    {
226
        if (null === $this->decorated) {
227 5
            // Set the global decoration flag that is set to `true` by the TreeBuilder in Configuration
228
            $this->decorated = $this->getContainer()->getParameter('liip_functional_test.command_decoration');
229 5
        }
230 5
231
        // Check the local decorated flag
232 5
        if (false === is_bool($this->decorated)) {
233 5
            throw new \OutOfBoundsException(
234 5
                sprintf('`WebTestCase::decorated` has to be `bool`. "%s" given.', gettype($this->decorated))
235 5
            );
236
        }
237 5
238
        return $this->decorated;
239
    }
240
241
    public function isDecorated($decorated)
242
    {
243
        $this->decorated = $decorated;
244
    }
245
246
    /**
247
     * Get an instance of the dependency injection container.
248
     * (this creates a kernel *without* parameters).
249
     *
250 23
     * @return ContainerInterface
251
     */
252 23
    protected function getContainer()
0 ignored issues
show
Coding Style introduced by
getContainer uses the super-global variable $_SERVER which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
253 23
    {
254
        if (!empty($this->kernelDir)) {
255 23
            $tmpKernelDir = isset($_SERVER['KERNEL_DIR']) ? $_SERVER['KERNEL_DIR'] : null;
256 5
            $_SERVER['KERNEL_DIR'] = getcwd().$this->kernelDir;
257 5
        }
258
259
        $cacheKey = $this->kernelDir.'|'.$this->environment;
260 23
        if (empty($this->containers[$cacheKey])) {
261
            $options = array(
262 23
                'environment' => $this->environment,
263
            );
264
            $kernel = $this->createKernel($options);
265
            $kernel->boot();
266
267
            $this->containers[$cacheKey] = $kernel->getContainer();
268
        }
269
270
        if (isset($tmpKernelDir)) {
271
            $_SERVER['KERNEL_DIR'] = $tmpKernelDir;
272
        }
273
274
        return $this->containers[$cacheKey];
275
    }
276
277
    /**
278
     * This function finds the time when the data blocks of a class definition
279
     * file were being written to, that is, the time when the content of the
280
     * file was changed.
281
     *
282
     * @param string $class The fully qualified class name of the fixture class to
283
     *                      check modification date on.
284
     *
285
     * @return \DateTime|null
286
     */
287
    protected function getFixtureLastModified($class)
288 25
    {
289
        $lastModifiedDateTime = null;
290 25
291
        $reflClass = new \ReflectionClass($class);
292 25
        $classFileName = $reflClass->getFileName();
293 25
294 25
        if (file_exists($classFileName)) {
295
            $lastModifiedDateTime = new \DateTime();
296 25
            $lastModifiedDateTime->setTimestamp(filemtime($classFileName));
297 25
        }
298 25
299 25
        return $lastModifiedDateTime;
300 25
    }
301
302 25
    /**
303 25
     * Determine if the Fixtures that define a database backup have been
304 25
     * modified since the backup was made.
305
     *
306 25
     * @param array  $classNames The fixture classnames to check
307 25
     * @param string $backup     The fixture backup SQLite database file path
308 25
     *
309 25
     * @return bool TRUE if the backup was made since the modifications to the
310 25
     *              fixtures; FALSE otherwise
311
     */
312
    protected function isBackupUpToDate(array $classNames, $backup)
313
    {
314 25
        $backupLastModifiedDateTime = new \DateTime();
315 25
        $backupLastModifiedDateTime->setTimestamp(filemtime($backup));
316
317
        foreach ($classNames as &$className) {
318
            $fixtureLastModifiedDateTime = $this->getFixtureLastModified($className);
319 25
            if ($backupLastModifiedDateTime < $fixtureLastModifiedDateTime) {
320 1
                return false;
321
            }
322 1
        }
323 25
324
        return true;
325 25
    }
326 25
327 25
    /**
328 23
     * Set the database to the provided fixtures.
329 23
     *
330
     * Drops the current database and then loads fixtures using the specified
331 23
     * classes. The parameter is a list of fully qualified class names of
332
     * classes that implement Doctrine\Common\DataFixtures\FixtureInterface
333 23
     * so that they can be loaded by the DataFixtures Loader::addFixture
334
     *
335 23
     * When using SQLite this method will automatically make a copy of the
336 23
     * loaded schema and fixtures which will be restored automatically in
337 23
     * case the same fixture classes are to be loaded again. Caveat: changes
338
     * to references and/or identities may go undetected.
339 23
     *
340
     * Depends on the doctrine data-fixtures library being available in the
341 23
     * class path.
342
     *
343 2
     * @param array  $classNames   List of fully qualified class names of fixtures to load
344
     * @param string $omName       The name of object manager to use
345
     * @param string $registryName The service id of manager registry to use
346 2
     * @param int    $purgeMode    Sets the ORM purge mode
347 2
     *
348 2
     * @return null|AbstractExecutor
349 2
     */
350 2
    protected function loadFixtures(array $classNames, $omName = null, $registryName = 'doctrine', $purgeMode = null)
351 2
    {
352
        $container = $this->getContainer();
353 2
        /** @var ManagerRegistry $registry */
354 2
        $registry = $container->get($registryName);
355 2
        $om = $registry->getManager($omName);
356 2
        $type = $registry->getName();
357
358 2
        $executorClass = 'PHPCR' === $type && class_exists('Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor')
359
            ? 'Doctrine\Bundle\PHPCRBundle\DataFixtures\PHPCRExecutor'
360
            : 'Doctrine\\Common\\DataFixtures\\Executor\\'.$type.'Executor';
361
        $referenceRepository = new ProxyReferenceRepository($om);
362
        $cacheDriver = $om->getMetadataFactory()->getCacheDriver();
363
364
        if ($cacheDriver) {
365
            $cacheDriver->deleteAll();
366
        }
367
368
        if ('ORM' === $type) {
369
            $connection = $om->getConnection();
370
            if ($connection->getDriver() instanceof SqliteDriver) {
371
                $params = $connection->getParams();
372
                if (isset($params['master'])) {
373
                    $params = $params['master'];
374
                }
375
376
                $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
377
                if (!$name) {
378
                    throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
379
                }
380 2
381
                if (!isset(self::$cachedMetadatas[$omName])) {
382 2
                    self::$cachedMetadatas[$omName] = $om->getMetadataFactory()->getAllMetadata();
383
                    usort(self::$cachedMetadatas[$omName], function ($a, $b) { return strcmp($a->name, $b->name); });
384 2
                }
385 2
                $metadatas = self::$cachedMetadatas[$omName];
386
387 2
                if ($container->getParameter('liip_functional_test.cache_sqlite_db')) {
388 2
                    $backup = $container->getParameter('kernel.cache_dir').'/test_'.md5(serialize($metadatas).serialize($classNames)).'.db';
389
                    if (file_exists($backup) && file_exists($backup.'.ser') && $this->isBackupUpToDate($classNames, $backup)) {
390 2
                        $om->flush();
391 2
                        $om->clear();
392
393 2
                        $this->preFixtureRestore($om, $referenceRepository);
394
395
                        copy($backup, $name);
396
397
                        $executor = new $executorClass($om);
398
                        $executor->setReferenceRepository($referenceRepository);
399
                        $executor->getReferenceRepository()->load($backup);
400
401
                        $this->postFixtureRestore();
402
403
                        return $executor;
404
                    }
405
                }
406 2
407
                // TODO: handle case when using persistent connections. Fail loudly?
408 2
                $schemaTool = new SchemaTool($om);
0 ignored issues
show
Compatibility introduced by
$om of type object<Doctrine\Common\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManagerInterface>. It seems like you assume a child interface of the interface Doctrine\Common\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
409
                $schemaTool->dropDatabase($name);
0 ignored issues
show
Unused Code introduced by
The call to SchemaTool::dropDatabase() has too many arguments starting with $name.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
410
                if (!empty($metadatas)) {
411
                    $schemaTool->createSchema($metadatas);
412
                }
413 2
                $this->postFixtureSetup();
414 2
415
                $executor = new $executorClass($om);
416 2
                $executor->setReferenceRepository($referenceRepository);
417
            }
418 2
        }
419 2
420
        if (empty($executor)) {
421
            $purgerClass = 'Doctrine\\Common\\DataFixtures\\Purger\\'.$type.'Purger';
422
            if ('PHPCR' === $type) {
423 2
                $purger = new $purgerClass($om);
424
                $initManager = $container->has('doctrine_phpcr.initializer_manager')
425 2
                    ? $container->get('doctrine_phpcr.initializer_manager')
426
                    : null;
427
428 2
                $executor = new $executorClass($om, $purger, $initManager);
429
            } else {
430 2
                $purger = new $purgerClass();
431 2
                if (null !== $purgeMode) {
432 2
                    $purger->setPurgeMode($purgeMode);
433 2
                }
434 1
435 1
                $executor = new $executorClass($om, $purger);
436
            }
437
438 1
            $executor->setReferenceRepository($referenceRepository);
439 2
            $executor->purge();
440
        }
441 2
442
        $loader = $this->getFixtureLoader($container, $classNames);
443
444
        $executor->execute($loader->getFixtures(), true);
445
446
        if (isset($name) && isset($backup)) {
447
            $this->preReferenceSave($om, $executor, $backup);
448 2
449
            $executor->getReferenceRepository()->save($backup);
450 2
            copy($name, $backup);
451
452
            $this->postReferenceSave($om, $executor, $backup);
453
        }
454
455
        return $executor;
456
    }
457 23
458
    /**
459 23
     * @param array  $paths        Either symfony resource locators (@ BundleName/etc) or actual file paths
460
     * @param bool   $append
461
     * @param null   $omName
462
     * @param string $registryName
463
     *
464
     * @return array
465
     *
466
     * @throws \BadMethodCallException
467
     */
468
    public function loadFixtureFiles(array $paths = array(), $append = false, $omName = null, $registryName = 'doctrine')
469 23
    {
470
        if (!class_exists('Nelmio\Alice\Fixtures')) {
471 23
            throw new \BadMethodCallException('nelmio/alice should be installed to use this method.');
472
        }
473
474
        /** @var ManagerRegistry $registry */
475
        $registry = $this->getContainer()->get($registryName);
476
        $om = $registry->getManager($omName);
477
478
        if ($append === false) {
479
            //Clean database
480
            $connection = $om->getConnection();
481
            if ($registry->getName() === 'ORM' && $connection->getDatabasePlatform() instanceof MySqlPlatform) {
482 2
                $connection->query('SET FOREIGN_KEY_CHECKS=0');
483
            }
484 2
485
            $this->loadFixtures(array());
486
487
            if ($registry->getName() === 'ORM' && $connection->getDatabasePlatform() instanceof MySqlPlatform) {
488
                $connection->query('SET FOREIGN_KEY_CHECKS=1');
489
            }
490
        }
491
492
        $files = array();
493
        $kernel = $this->getContainer()->get('kernel');
494
        foreach ($paths as $path) {
495 2
            if ($path[0] !== '@' && file_exists($path) === true) {
496
                $files[] = $path;
497 2
                continue;
498
            }
499
500
            $files[] = $kernel->locateResource($path);
501
        }
502
503
        return Fixtures::load($files, $om);
504
    }
505
506
    /**
507 2
     * Callback function to be executed after Schema creation.
508
     * Use this to execute acl:init or other things necessary.
509 2
     */
510 2
    protected function postFixtureSetup()
511 2
    {
512
    }
513 2
514
    /**
515 2
     * Callback function to be executed after Schema restore.
516
     *
517 2
     * @return WebTestCase
518 1
     */
519 2
    protected function postFixtureRestore()
520
    {
521 2
    }
522
523
    /**
524
     * Callback function to be executed before Schema restore.
525
     *
526
     * @param ObjectManager            $manager             The object manager
527
     * @param ProxyReferenceRepository $referenceRepository The reference repository
528
     *
529
     * @return WebTestCase
530 1
     */
531
    protected function preFixtureRestore(ObjectManager $manager, ProxyReferenceRepository $referenceRepository)
0 ignored issues
show
Unused Code introduced by
The parameter $manager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $referenceRepository is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
532 1
    {
533
    }
534 1
535
    /**
536
     * Callback function to be executed after save of references.
537
     *
538
     * @param ObjectManager    $manager        The object manager
539
     * @param AbstractExecutor $executor       Executor of the data fixtures
540 1
     * @param string           $backupFilePath Path of file used to backup the references of the data fixtures
541
     *
542 1
     * @return WebTestCase
543
     */
544
    protected function postReferenceSave(ObjectManager $manager, AbstractExecutor $executor, $backupFilePath)
0 ignored issues
show
Unused Code introduced by
The parameter $manager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $executor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $backupFilePath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
545
    {
546
    }
547 1
548
    /**
549
     * Callback function to be executed before save of references.
550
     *
551
     * @param ObjectManager    $manager        The object manager
552
     * @param AbstractExecutor $executor       Executor of the data fixtures
553
     * @param string           $backupFilePath Path of file used to backup the references of the data fixtures
554
     *
555
     * @return WebTestCase
556
     */
557
    protected function preReferenceSave(ObjectManager $manager, AbstractExecutor $executor, $backupFilePath)
0 ignored issues
show
Unused Code introduced by
The parameter $manager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $executor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $backupFilePath is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
558
    {
559
    }
560
561
    /**
562
     * Retrieve Doctrine DataFixtures loader.
563
     *
564 24
     * @param ContainerInterface $container
565
     * @param array              $classNames
566 24
     *
567 5
     * @return Loader
568 3
     */
569 3
    protected function getFixtureLoader(ContainerInterface $container, array $classNames)
570
    {
571 5
        $loaderClass = class_exists('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader')
572 5
            ? 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader'
573 5
            : (class_exists('Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader')
574 5
                ? 'Doctrine\Bundle\FixturesBundle\Common\DataFixtures\Loader'
575 5
                : 'Symfony\Bundle\DoctrineFixturesBundle\Common\DataFixtures\Loader');
576
577 24
        $loader = new $loaderClass($container);
578
579 24
        foreach ($classNames as $className) {
580
            $this->loadFixtureClass($loader, $className);
581 1
        }
582
583 1
        return $loader;
584
    }
585
586
    /**
587 1
     * Load a data fixture class.
588
     *
589 1
     * @param Loader $loader
590 1
     * @param string $className
591 1
     */
592
    protected function loadFixtureClass($loader, $className)
593 1
    {
594
        $fixture = new $className();
595
596 1
        if ($loader->hasFixture($fixture)) {
597 1
            unset($fixture);
598
599
            return;
600
        }
601 1
602 1
        $loader->addFixture($fixture);
603 1
604
        if ($fixture instanceof DependentFixtureInterface) {
605
            foreach ($fixture->getDependencies() as $dependency) {
606
                $this->loadFixtureClass($loader, $dependency);
607
            }
608
        }
609
    }
610 1
611 1
    /**
612 1
     * Creates an instance of a lightweight Http client.
613
     *
614 1
     * If $authentication is set to 'true' it will use the content of
615 1
     * 'liip_functional_test.authentication' to log in.
616
     *
617 24
     * $params can be used to pass headers to the client, note that they have
618
     * to follow the naming format used in $_SERVER.
619
     * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With'
620
     *
621
     * @param bool|array $authentication
622
     * @param array      $params
623
     *
624
     * @return Client
625
     */
626
    protected function makeClient($authentication = false, array $params = array())
627
    {
628
        if ($authentication) {
629
            if ($authentication === true) {
630
                $authentication = $this->getContainer()->getParameter('liip_functional_test.authentication');
631
            }
632
633 1
            $params = array_merge($params, array(
634
                'PHP_AUTH_USER' => $authentication['username'],
635 1
                'PHP_AUTH_PW' => $authentication['password'],
636 1
            ));
637 1
        }
638 1
639 1
        $client = static::createClient(array('environment' => $this->environment), $params);
640 1
641
        if ($this->firewallLogins) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->firewallLogins of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
642
            // has to be set otherwise "hasPreviousSession" in Request returns false.
643
            $options = $client->getContainer()->getParameter('session.storage.options');
644
645
            if (!$options || !isset($options['name'])) {
646
                throw new \InvalidArgumentException('Missing session.storage.options#name');
647
            }
648
649
            $session = $client->getContainer()->get('session');
650
            // Since the namespace of the session changed in symfony 2.1, instanceof can be used to check the version.
651
            if ($session instanceof Session) {
652 1
                $session->setId(uniqid());
653
            }
654 1
655
            $client->getCookieJar()->set(new Cookie($options['name'], $session->getId()));
656
657
            /** @var $user UserInterface */
658
            foreach ($this->firewallLogins as $firewallName => $user) {
659
                $token = $this->createUserToken($user, $firewallName);
660
661
                // BC: security.token_storage is available on Symfony 2.6+
662
                // see http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
663
                if ($client->getContainer()->has('security.token_storage')) {
664 7
                    $tokenStorage = $client->getContainer()->get('security.token_storage');
665
                } else {
666
                    // This block will never be reached with Symfony 2.5+
667 7
                    // @codeCoverageIgnoreStart
668 7
                    $tokenStorage = $client->getContainer()->get('security.context');
669 7
                    // @codeCoverageIgnoreEnd
670 2
                }
671 2
672 5
                $tokenStorage->setToken($token);
673
                $session->set('_security_'.$firewallName, serialize($token));
674 7
            }
675
676
            $session->save();
677
        }
678 7
679 5
        return $client;
680 5
    }
681 2
682
    /**
683 7
     * Create User Token.
684
     *
685
     * Factory method for creating a User Token object for the firewall based on
686
     * the user object provided. By default it will be a Username/Password
687
     * Token based on the user's credentials, but may be overridden for custom
688
     * tokens in your applications.
689
     *
690
     * @param UserInterface $user         The user object to base the token off of
691
     * @param string        $firewallName name of the firewall provider to use
692
     *
693
     * @return TokenInterface The token to be used in the security context
694
     */
695
    protected function createUserToken(UserInterface $user, $firewallName)
696
    {
697 1
        return new UsernamePasswordToken(
698
            $user,
699 1
            null,
700 1
            $firewallName,
701
            $user->getRoles()
702 1
        );
703 1
    }
704 1
705 1
    /**
706
     * Extracts the location from the given route.
707 1
     *
708
     * @param string $route    The name of the route
709
     * @param array  $params   Set of parameters
710
     * @param bool   $absolute
711
     *
712
     * @return string
713
     */
714
    protected function getUrl($route, $params = array(), $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
715
    {
716
        return $this->getContainer()->get('router')->generate($route, $params, $absolute);
717
    }
718
719
    /**
720
     * Checks the success state of a response.
721
     *
722 1
     * @param Response $response Response object
723
     * @param bool     $success  to define whether the response is expected to be successful
724 1
     * @param string   $type
725 1
     */
726
    public function isSuccessful($response, $success = true, $type = 'text/html')
727 1
    {
728
        try {
729 1
            $crawler = new Crawler();
730
            $crawler->addContent($response->getContent(), $type);
731
            if (!count($crawler->filter('title'))) {
732
                $title = '['.$response->getStatusCode().'] - '.$response->getContent();
733
            } else {
734
                $title = $crawler->filter('title')->text();
735
            }
736
        } catch (\Exception $e) {
737 1
            $title = $e->getMessage();
738
        }
739 1
740
        if ($success) {
741 1
            $this->assertTrue($response->isSuccessful(), 'The Response was not successful: '.$title);
742
        } else {
743
            $this->assertFalse($response->isSuccessful(), 'The Response was successful: '.$title);
744
        }
745
    }
746
747
    /**
748
     * Executes a request on the given url and returns the response contents.
749
     *
750
     * This method also asserts the request was successful.
751
     *
752 13
     * @param string $path           path of the requested page
753
     * @param string $method         The HTTP method to use, defaults to GET
754 13
     * @param bool   $authentication Whether to use authentication, defaults to false
755
     * @param bool   $success        to define whether the response is expected to be successful
756 13
     *
757
     * @return string
758
     */
759
    public function fetchContent($path, $method = 'GET', $authentication = false, $success = true)
760
    {
761
        $client = $this->makeClient($authentication);
762
        $client->request($method, $path);
763
764
        $content = $client->getResponse()->getContent();
765
        if (is_bool($success)) {
766
            $this->isSuccessful($client->getResponse(), $success);
767
        }
768
769
        return $content;
770
    }
771 13
772 13
    /**
773
     * Executes a request on the given url and returns a Crawler object.
774
     *
775
     * This method also asserts the request was successful.
776
     *
777
     * @param string $path           path of the requested page
778
     * @param string $method         The HTTP method to use, defaults to GET
779
     * @param bool   $authentication Whether to use authentication, defaults to false
780
     * @param bool   $success        Whether the response is expected to be successful
781 2
     *
782
     * @return Crawler
783 2
     */
784 2
    public function fetchCrawler($path, $method = 'GET', $authentication = false, $success = true)
785 2
    {
786
        $client = $this->makeClient($authentication);
787 2
        $crawler = $client->request($method, $path);
788 1
789
        $this->isSuccessful($client->getResponse(), $success);
790
791
        return $crawler;
792
    }
793
794
    /**
795
     * @param UserInterface $user
796
     *
797
     * @return WebTestCase
798
     */
799
    public function loginAs(UserInterface $user, $firewallName)
800
    {
801
        $this->firewallLogins[$firewallName] = $user;
802
803
        return $this;
804
    }
805
806
    /**
807
     * Asserts that the HTTP response code of the last request performed by
808
     * $client matches the expected code. If not, raises an error with more
809
     * information.
810
     *
811
     * @param $expectedStatusCode
812
     * @param Client $client
813
     */
814
    public function assertStatusCode($expectedStatusCode, Client $client)
815
    {
816
        $helpfulErrorMessage = null;
817
818
        if ($expectedStatusCode !== $client->getResponse()->getStatusCode()) {
819
            // Get a more useful error message, if available
820
            if ($exception = $client->getContainer()->get('liip_functional_test.exception_listener')->getLastException()) {
821
                $helpfulErrorMessage = $exception->getMessage();
822
            } elseif (count($validationErrors = $client->getContainer()->get('liip_functional_test.validator')->getLastErrors())) {
823
                $helpfulErrorMessage = "Unexpected validation errors:\n";
824
825
                foreach ($validationErrors as $error) {
826
                    $helpfulErrorMessage .= sprintf("+ %s: %s\n", $error->getPropertyPath(), $error->getMessage());
827
                }
828
            } else {
829
                $helpfulErrorMessage = substr($client->getResponse(), 0, 200);
830
            }
831
        }
832
833
        self::assertEquals($expectedStatusCode, $client->getResponse()->getStatusCode(), $helpfulErrorMessage);
834
    }
835
836
    /**
837
     * Assert that the last validation errors within $container match the
838
     * expected keys.
839
     *
840
     * @param array              $expected  A flat array of field names
841
     * @param ContainerInterface $container
842
     */
843
    public function assertValidationErrors(array $expected, ContainerInterface $container)
844
    {
845
        self::assertThat(
846
            $container->get('liip_functional_test.validator')->getLastErrors(),
847
            new ValidationErrorsConstraint($expected),
848
            'Validation errors should match.'
849
        );
850
    }
851
}
852