|
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 Yarhon\RouteGuardBundle\YarhonRouteGuardBundle; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class WebTestCase extends BaseWebTestCase |
|
23
|
|
|
{ |
|
24
|
|
|
protected static $bundles = []; |
|
25
|
|
|
|
|
26
|
|
|
protected static $configs = []; |
|
27
|
|
|
|
|
28
|
|
|
protected static $routeResources = []; |
|
29
|
|
|
|
|
30
|
|
|
protected static $users = []; |
|
31
|
|
|
|
|
32
|
|
|
protected static function getBundles() |
|
33
|
|
|
{ |
|
34
|
|
|
return array_merge([ |
|
35
|
|
|
FrameworkBundle::class, |
|
36
|
|
|
SecurityBundle::class, |
|
37
|
|
|
YarhonRouteGuardBundle::class, |
|
38
|
|
|
], static::$bundles); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected static function getConfigs() |
|
42
|
|
|
{ |
|
43
|
|
|
$configs = static::$configs; |
|
44
|
|
|
$configs['security']['providers']['main']['memory']['users'] = static::$users; |
|
45
|
|
|
// $configs['framework']['router'] = $routerConfig; |
|
46
|
|
|
|
|
47
|
|
|
return $configs; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected static function getRouteResources() |
|
51
|
|
|
{ |
|
52
|
|
|
return static::$routeResources; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function setUpBeforeClass() |
|
56
|
|
|
{ |
|
57
|
|
|
static::deleteTempDir(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public static function tearDownAfterClass() |
|
61
|
|
|
{ |
|
62
|
|
|
static::deleteTempDir(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected static function deleteTempDir() |
|
66
|
|
|
{ |
|
67
|
|
|
if (!file_exists($dir = static::getTempDir())) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$fs = new Filesystem(); |
|
72
|
|
|
$fs->remove($dir); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected static function createKernel(array $options = []) |
|
76
|
|
|
{ |
|
77
|
|
|
return new app\Kernel( |
|
78
|
|
|
static::getTempDir(), |
|
79
|
|
|
static::getBundles(), |
|
80
|
|
|
static::getConfigs(), |
|
81
|
|
|
static::getRouteResources(), |
|
82
|
|
|
isset($options['environment']) ? $options['environment'] : 'test', |
|
83
|
|
|
isset($options['debug']) ? $options['debug'] : true |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected static function getTempDir() |
|
88
|
|
|
{ |
|
89
|
|
|
return sys_get_temp_dir().'/route-guard-'.substr(strrchr(static::class, '\\'), 1); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected static function createClient(array $options = [], array $server = []) |
|
93
|
|
|
{ |
|
94
|
|
|
$server = array_merge([ |
|
95
|
|
|
'HTTP_HOST' => 'example.com', |
|
96
|
|
|
], $server); |
|
97
|
|
|
|
|
98
|
|
|
return parent::createClient($options, $server); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function requestLink($user, $route) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($user) { |
|
104
|
|
|
$serverOptions = ['PHP_AUTH_USER' => $user, 'PHP_AUTH_PW' => static::$users[$user]['password']]; |
|
105
|
|
|
} else { |
|
106
|
|
|
$serverOptions = []; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$client = static::createClient([], $serverOptions); |
|
110
|
|
|
|
|
111
|
|
|
$uri = '/link/'.$route[0]; |
|
112
|
|
|
|
|
113
|
|
|
$query = array_filter([ |
|
114
|
|
|
'parameters' => isset($route[1]) ? $route[1] : null, |
|
115
|
|
|
'method' => isset($route[2]) ? $route[2] : null, |
|
116
|
|
|
]); |
|
117
|
|
|
|
|
118
|
|
|
if ($query) { |
|
|
|
|
|
|
119
|
|
|
$uri .= '?'.http_build_query($query); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$crawler = $client->request('GET', $uri); |
|
123
|
|
|
|
|
124
|
|
|
return $crawler->filterXPath('//*[@id="link"]'); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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.