test: add some KernelTestCase

This commit is contained in:
Maël Gangloff
2025-10-14 17:40:48 +02:00
parent 39262814ce
commit 97ea1ab33a
10 changed files with 255 additions and 10 deletions

View File

@@ -21,6 +21,7 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class RegistrationController extends AbstractController
{
@@ -33,6 +34,7 @@ class RegistrationController extends AbstractController
private readonly SerializerInterface $serializer,
private readonly LoggerInterface $logger,
private readonly KernelInterface $kernel,
private readonly ValidatorInterface $validator,
) {
}
@@ -64,14 +66,16 @@ class RegistrationController extends AbstractController
}
$user = $this->serializer->deserialize($request->getContent(), User::class, 'json', ['groups' => 'user:register']);
if (null === $user->getEmail() || null === $user->getPassword()) {
throw new BadRequestHttpException('Bad request');
$violations = $this->validator->validate($user);
if ($violations->count() > 0) {
throw new BadRequestHttpException($violations->get(0));
}
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$user->getPassword()
$user->getPlainPassword()
)
)->setCreatedAt(new \DateTimeImmutable());

View File

@@ -2,6 +2,7 @@
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Config\EventAction;
@@ -75,6 +76,11 @@ class Domain
*/
#[ORM\OneToMany(targetEntity: DomainEvent::class, mappedBy: 'domain', cascade: ['persist'], orphanRemoval: true)]
#[Groups(['domain:item', 'domain:list', 'watchlist:list'])]
#[ApiProperty(
openapiContext: [
'type' => 'array',
]
)]
private Collection $events;
/**

View File

@@ -2,6 +2,7 @@
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\EntityRepository;
use Doctrine\Common\Collections\ArrayCollection;
@@ -77,7 +78,13 @@ class Entity
#[Groups(['entity:item', 'domain:item'])]
private Collection $events;
#[ORM\Column]
#[ORM\Column(type: 'json')]
#[ApiProperty(
openapiContext: [
'type' => 'array',
'items' => ['type' => 'array'],
]
)]
#[Groups(['entity:item', 'domain:item'])]
private array $jCard = [];

View File

@@ -15,6 +15,8 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\SerializedName;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_EMAIL', fields: ['email'])]
@@ -46,6 +48,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(length: 180)]
#[Groups(['user:list', 'user:register'])]
#[Assert\Email]
#[Assert\NotBlank]
private ?string $email = null;
/**
@@ -59,7 +63,6 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
* @var string|null The hashed password
*/
#[ORM\Column(nullable: true)]
#[Groups(['user:register'])]
private ?string $password = null;
/**
@@ -80,6 +83,10 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $verifiedAt = null;
#[Assert\PasswordStrength]
#[Assert\NotBlank]
#[SerializedName('password')]
#[Groups(['user:register'])]
private ?string $plainPassword = null;
public function __construct()

View File

@@ -36,7 +36,7 @@ final class UserFactory extends PersistentObjectFactory
protected function defaults(): array|callable
{
$createdAt = \DateTimeImmutable::createFromMutable(self::faker()->dateTime());
$plainPassword = self::faker()->password(8, 20);
$plainPassword = self::faker()->password(16, 20);
return [
'createdAt' => $createdAt,