domain-watchdog/tests/AbstractTest.php
2025-10-14 11:15:56 +02:00

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'];
}
}