1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Utils/Tests/WebTestCase.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace App\Utils\Tests; |
9
|
|
|
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class WebTestCase |
16
|
|
|
* |
17
|
|
|
* @package App\Tests |
18
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class WebTestCase extends BaseWebTestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ContainerInterface |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Auth |
29
|
|
|
*/ |
30
|
|
|
private $authService; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Getter method for container |
34
|
|
|
* |
35
|
|
|
* @return ContainerInterface |
36
|
|
|
*/ |
37
|
|
|
public function getContainer(): ContainerInterface |
38
|
|
|
{ |
39
|
|
|
if (!($this->container instanceof ContainerInterface)) { |
40
|
|
|
self::bootKernel(); |
41
|
|
|
|
42
|
|
|
$this->container = static::$kernel->getContainer(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->container; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Getter method for auth service |
50
|
|
|
* |
51
|
|
|
* @return Auth |
52
|
|
|
*/ |
53
|
|
|
public function getAuthService(): Auth |
54
|
|
|
{ |
55
|
|
|
if (!($this->authService instanceof Auth)) { |
56
|
|
|
// We need to boot kernel up to get auth service |
57
|
|
|
self::bootKernel(); |
58
|
|
|
|
59
|
|
|
$this->authService = $this->getContainer()->get(Auth::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->authService; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Helper method to get authorized client for specified username and password. |
67
|
|
|
* |
68
|
|
|
* @param string|null $username |
69
|
|
|
* @param string|null $password |
70
|
|
|
* @param array $options |
71
|
|
|
* @param array $server |
72
|
|
|
* |
73
|
|
|
* @return Client |
74
|
|
|
*/ |
75
|
|
|
public function getClient( |
76
|
|
|
string $username = null, |
77
|
|
|
string $password = null, |
78
|
|
|
array $options = null, |
79
|
|
|
array $server = null |
80
|
|
|
): Client { |
81
|
|
|
$options = $options ?? []; |
82
|
|
|
$server = $server ?? []; |
83
|
|
|
|
84
|
|
|
// Merge authorization headers |
85
|
|
|
$server = \array_merge( |
86
|
|
|
$username === null ? [] : $this->getAuthService()->getAuthorizationHeadersForUser($username, $password), |
87
|
|
|
\array_merge($this->getJsonHeaders(), $this->getFastestHeaders()), |
88
|
|
|
$this->getAuthService()->getJwtHeaders(), |
89
|
|
|
$server |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return static::createClient($options, $server); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function getJsonHeaders(): array |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
'CONTENT_TYPE' => 'application/json', |
102
|
|
|
'HTTP_X-Requested-With' => 'XMLHttpRequest', |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @codeCoverageIgnore |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getFastestHeaders(): array |
112
|
|
|
{ |
113
|
|
|
$output = []; |
114
|
|
|
|
115
|
|
|
if (\getenv('ENV_TEST_CHANNEL_READABLE')) { |
116
|
|
|
$output = [ |
117
|
|
|
'X-FASTEST-ENV-TEST-CHANNEL-READABLE' => \getenv('ENV_TEST_CHANNEL_READABLE'), |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $output; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|