Issues (62)

Tests/Functional/WebTestCase.php (1 issue)

1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Functional;
12
13
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
14
use Symfony\Component\Filesystem\Filesystem;
15
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
16
use Symfony\Bundle\SecurityBundle\SecurityBundle;
17
use Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle;
18
use Symfony\Component\HttpKernel\Kernel;
19
use Yarhon\RouteGuardBundle\YarhonRouteGuardBundle;
20
21
/**
22
 * @author Yaroslav Honcharuk <[email protected]>
23
 */
24
abstract class WebTestCase extends BaseWebTestCase
25
{
26
    protected static $bundles = [];
27
28
    protected static $configs = [];
29
30
    protected static $users = [];
31
32
    protected static function getBundles()
33
    {
34
        $bundles = static::$bundles;
35
36
        // In Symfony 3.3 @Route annotations are loaded by SensioFrameworkExtraBundle
37
        if (Kernel::VERSION_ID < 30400 && !in_array(SensioFrameworkExtraBundle::class, $bundles, true)) {
38
            $bundles[] = SensioFrameworkExtraBundle::class;
39
        }
40
41
        return array_merge([
42
            FrameworkBundle::class,
43
            SecurityBundle::class,
44
            YarhonRouteGuardBundle::class,
45
        ], $bundles);
46
    }
47
48
    protected static function getConfigs()
49
    {
50
        $configs = static::$configs;
51
        $configs['security']['providers']['main']['memory']['users'] = static::$users;
52
        // $configs['framework']['router'] = $routerConfig;
53
54
        return $configs;
55
    }
56
57
    public static function setUpBeforeClass()
58
    {
59
        static::deleteTempDir();
60
    }
61
62
    public static function tearDownAfterClass()
63
    {
64
        static::deleteTempDir();
65
    }
66
67
    protected static function deleteTempDir()
68
    {
69
        if (!file_exists($dir = static::getTempDir())) {
70
            return;
71
        }
72
73
        $fs = new Filesystem();
74
        $fs->remove($dir);
75
    }
76
77
    protected static function createKernel(array $options = [])
78
    {
79
        return new app\Kernel(
80
            static::getTempDir(),
81
            static::getBundles(),
82
            static::getConfigs(),
83
            isset($options['environment']) ? $options['environment'] : 'test',
84
            isset($options['debug']) ? $options['debug'] : true
85
        );
86
    }
87
88
    protected static function getTempDir()
89
    {
90
        return sys_get_temp_dir().'/route-guard-'.substr(strrchr(static::class, '\\'), 1);
91
    }
92
93
    protected static function createClient(array $options = [], array $server = [])
94
    {
95
        $server = array_merge([
96
            'HTTP_HOST' => 'example.com',
97
        ], $server);
98
99
        return parent::createClient($options, $server);
100
    }
101
102
    protected function requestLink($user, $route)
103
    {
104
        if ($user) {
105
            $serverOptions = ['PHP_AUTH_USER' => $user, 'PHP_AUTH_PW' => static::$users[$user]['password']];
106
        } else {
107
            $serverOptions = [];
108
        }
109
110
        $client = static::createClient([], $serverOptions);
111
112
        $uri = '/link/'.$route[0];
113
114
        $query = array_filter([
115
            'parameters' => isset($route[1]) ? $route[1] : null,
116
            'method' => isset($route[2]) ? $route[2] : null,
117
        ]);
118
119
        if ($query) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query 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...
120
            $uri .= '?'.http_build_query($query);
121
        }
122
123
        $crawler = $client->request('GET', $uri);
124
125
        return $crawler->filterXPath('//*[@id="link"]');
126
    }
127
}
128