|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\TestUtils; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use App\Infrastructure\Security\LoggedInUser; |
|
7
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
11
|
|
|
use Symfony\Component\Uid\Ulid; |
|
12
|
|
|
|
|
13
|
|
|
abstract class IntegrationTest extends WebTestCase |
|
14
|
|
|
{ |
|
15
|
|
|
const DEFAULT_USER_ID = '[email protected]'; |
|
16
|
|
|
const DEFAULT_USER_PASSWORD = 'user'; |
|
17
|
|
|
|
|
18
|
|
|
private ?KernelBrowser $client = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @before |
|
22
|
|
|
*/ |
|
23
|
|
|
public function setupClient() |
|
24
|
|
|
{ |
|
25
|
|
|
if ($this->client == null) { |
|
26
|
|
|
$this->client = $this->createAuthenticatedClient(); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a client with a default Authorization header. |
|
32
|
|
|
* |
|
33
|
|
|
* |
|
34
|
|
|
* @return KernelBrowser |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function createAuthenticatedClient(): KernelBrowser |
|
37
|
|
|
{ |
|
38
|
|
|
$client = static::createClient(); |
|
39
|
|
|
$client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s', |
|
40
|
|
|
$this->createToken()) |
|
41
|
|
|
); |
|
42
|
|
|
return $client; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getClient(): KernelBrowser |
|
46
|
|
|
{ |
|
47
|
|
|
if (!isset($this->client) || $this->client == null) { |
|
48
|
|
|
throw new \RuntimeException("invalid Client"); |
|
49
|
|
|
} |
|
50
|
|
|
return $this->client; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function createToken(): string |
|
54
|
|
|
{ |
|
55
|
|
|
$tokenStorage = $this->getContainer()->get('security.token_storage'); |
|
56
|
|
|
$user = new LoggedInUser(new Ulid(), self::DEFAULT_USER_ID, ['ROLE_USER']); |
|
57
|
|
|
$token = new TestBrowserToken(['ROLE_USER'], $user); |
|
58
|
|
|
$tokenStorage->setToken($token); |
|
59
|
|
|
$jwtManager = $this->getContainer()->get(JWTTokenManagerInterface::class); |
|
60
|
|
|
return $jwtManager->create($user); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
} |