- .env.test points at db_test (doctrine when@test appends _test suffix) - ApiTestCase: WebTestCase base with createUser/jwtFor/authJson helpers - InfraSmokeTest: proves boot + DB + JWT auth pipeline works (/health 200, unauth /oauth/userinfo 401, JWT-auth userinfo 200) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
75 lines
2.3 KiB
PHP
75 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Tests;
|
|
|
|
use App\Auth\Entity\User;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
|
|
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
/**
|
|
* Base class for functional API tests.
|
|
*
|
|
* Provides a booted KernelBrowser, the EntityManager, and helpers to create
|
|
* users and issue JWTs so tests can hit authenticated /api and /oauth endpoints.
|
|
* Runs against the dedicated db_test database (see .env.test / doctrine when@test).
|
|
*/
|
|
abstract class ApiTestCase extends WebTestCase
|
|
{
|
|
protected KernelBrowser $client;
|
|
protected EntityManagerInterface $em;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->client = static::createClient();
|
|
$this->em = static::getContainer()->get(EntityManagerInterface::class);
|
|
}
|
|
|
|
/**
|
|
* Persist a user with the given roles. Mobile is randomised per test to
|
|
* avoid unique-constraint clashes across cases without a full DB reset.
|
|
*/
|
|
protected function createUser(array $roles = ['ROLE_USER'], ?string $mobile = null): User
|
|
{
|
|
$mobile ??= '0912' . str_pad((string) random_int(0, 9_999_999), 7, '0', STR_PAD_LEFT);
|
|
$user = new User($mobile);
|
|
$user->setRoles($roles);
|
|
$user->setStatus(1);
|
|
$this->em->persist($user);
|
|
$this->em->flush();
|
|
|
|
return $user;
|
|
}
|
|
|
|
protected function jwtFor(User $user): string
|
|
{
|
|
return static::getContainer()
|
|
->get(JWTTokenManagerInterface::class)
|
|
->create($user);
|
|
}
|
|
|
|
/**
|
|
* Issue an authenticated JSON request and return the decoded response body.
|
|
*/
|
|
protected function authJson(string $method, string $uri, User $user, array $body = []): array
|
|
{
|
|
$this->client->request(
|
|
$method,
|
|
$uri,
|
|
server: [
|
|
'HTTP_AUTHORIZATION' => 'Bearer ' . $this->jwtFor($user),
|
|
'CONTENT_TYPE' => 'application/json',
|
|
],
|
|
content: $body ? json_encode($body) : null,
|
|
);
|
|
|
|
return json_decode($this->client->getResponse()->getContent(), true) ?? [];
|
|
}
|
|
|
|
protected function responseCode(): int
|
|
{
|
|
return $this->client->getResponse()->getStatusCode();
|
|
}
|
|
}
|