mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
test: add some KernelTestCase
This commit is contained in:
37
tests/AbstractTest.php
Normal file
37
tests/AbstractTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests;
|
||||
|
||||
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
|
||||
use ApiPlatform\Symfony\Bundle\Test\Client;
|
||||
use App\Entity\User;
|
||||
|
||||
abstract 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'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user