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:
15
src/DataFixtures/AppFixtures.php
Normal file
15
src/DataFixtures/AppFixtures.php
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
64
src/Factory/UserFactory.php
Normal file
64
src/Factory/UserFactory.php
Normal 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())
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
14
src/Story/DefaultUsersStory.php
Normal file
14
src/Story/DefaultUsersStory.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user