test: add some KernelTestCase

This commit is contained in:
Maël Gangloff
2025-10-14 00:09:01 +02:00
parent 398ca8df2f
commit 6154759507
15 changed files with 884 additions and 3 deletions

View File

@@ -0,0 +1,15 @@
<?php
namespace App\DataFixtures;
use App\Story\DefaultUsersStory;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
class AppFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
DefaultUsersStory::load();
}
}

View File

@@ -80,6 +80,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $verifiedAt = null;
private ?string $plainPassword = null;
public function __construct()
{
$this->watchLists = new ArrayCollection();
@@ -155,7 +157,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
$this->plainPassword = null;
}
/**
@@ -241,4 +243,16 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
return $this;
}
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
public function setPlainPassword(?string $plainPassword): self
{
$this->plainPassword = $plainPassword;
return $this;
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Factory;
use App\Entity\User;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
/**
* @extends PersistentObjectFactory<User>
*/
final class UserFactory extends PersistentObjectFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct(
private readonly UserPasswordHasherInterface $passwordHasher,
) {
}
#[\Override]
public static function class(): string
{
return User::class;
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
#[\Override]
protected function defaults(): array|callable
{
$createdAt = \DateTimeImmutable::createFromMutable(self::faker()->dateTime());
$plainPassword = self::faker()->password(8, 20);
return [
'createdAt' => $createdAt,
'verifiedAt' => $createdAt,
'email' => self::faker()->unique()->safeEmail(),
'plainPassword' => $plainPassword,
'roles' => ['ROLE_USER'],
];
}
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
#[\Override]
protected function initialize(): static
{
return $this->afterInstantiate(function (User $user): void {
if ($user->getPlainPassword()) {
$user->setPassword(
$this->passwordHasher->hashPassword($user, $user->getPlainPassword())
);
}
});
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace App\Story;
use App\Factory\UserFactory;
use Zenstruck\Foundry\Story;
final class DefaultUsersStory extends Story
{
public function build(): void
{
UserFactory::createMany(3);
}
}