mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-18 10:15:41 +00:00
38 lines
943 B
PHP
38 lines
943 B
PHP
<?php
|
|
|
|
namespace App\Tests;
|
|
|
|
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
|
use ApiPlatform\Symfony\Bundle\Test\Client;
|
|
use App\Entity\User;
|
|
|
|
class AbstractTest extends ApiTestCase
|
|
{
|
|
private ?string $token = null;
|
|
|
|
public function setUp(): void
|
|
{
|
|
self::bootKernel();
|
|
}
|
|
|
|
protected function createClientWithCredentials($token = null): Client
|
|
{
|
|
return static::createClient([], ['headers' => ['authorization' => 'Bearer '.$token]]);
|
|
}
|
|
|
|
protected function getToken(User $testUser): string
|
|
{
|
|
if ($this->token) {
|
|
return $this->token;
|
|
}
|
|
|
|
$response = static::createClient()->request('POST', '/api/login', ['json' => ['email' => $testUser->getEmail(), 'password' => $testUser->getPlainPassword()]]);
|
|
|
|
$this->assertResponseIsSuccessful();
|
|
$data = $response->toArray();
|
|
$this->token = $data['token'];
|
|
|
|
return $data['token'];
|
|
}
|
|
}
|