From 408e3d492050ac34f994b6865bb3f8bac5dec044 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Thu, 31 Oct 2024 07:04:05 +0100 Subject: [PATCH 1/1] Add bundle fixture --- Fixture/UserFixture.php | 107 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Fixture/UserFixture.php diff --git a/Fixture/UserFixture.php b/Fixture/UserFixture.php new file mode 100644 index 0000000..01a7e8a --- /dev/null +++ b/Fixture/UserFixture.php @@ -0,0 +1,107 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Rapsys\UserBundle\Fixture; + +use Doctrine\Bundle\FixturesBundle\Fixture; +use Doctrine\Persistence\ObjectManager; + +use Rapsys\UserBundle\RapsysUserBundle; + +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; + +/** + * {@inheritdoc} + */ +class UserFixture extends Fixture { + /** + * Config array + */ + protected array $config; + + /** + * Air fixtures constructor + */ + public function __construct(protected ContainerInterface $container, protected UserPasswordHasherInterface $hasher) { + //Retrieve config + $this->config = $container->getParameter(RapsysUserBundle::getAlias()); + } + + /** + * {@inheritDoc} + */ + public function load(ObjectManager $manager) { + //Civility tree + $civilityTree = [ + 'Mister', + 'Madam', + 'Miss' + ]; + + //Create titles + $civilitys = []; + foreach($civilityTree as $civilityData) { + $civility = new $this->config['class']['civility']($civilityData); + $manager->persist($civility); + $civilitys[$civilityData] = $civility; + unset($civility); + } + + //Group tree + //XXX: ROLE_XXX is required by + $groupTree = [ + 'Guest', + 'User', + 'Admin' + ]; + + //Create groups + $groups = []; + foreach($groupTree as $groupData) { + $group = new $this->config['class']['group']($groupData); + $manager->persist($group); + $groups[$groupData] = $group; + unset($group); + } + + //Flush to get the ids + $manager->flush(); + + //User tree + $userTree = [ + [ + 'civility' => 'Mister', + 'group' => 'Admin', + 'mail' => 'admin@example.com', + 'forename' => 'Forename', + 'surname' => 'Surname', + 'password' => 'test', + 'active' => true + ] + ]; + + //Create users + $users = []; + foreach($userTree as $userData) { + $user = new $this->config['class']['user']($userData['mail'], $userData['password'], $civilitys[$userData['civility']], $userData['forename'], $userData['surname'], $userData['active']); + #TODO: check that password is hashed correctly !!! + $user->setPassword($this->hasher->hashPassword($user, $userData['password'])); + $user->addGroup($groups[$userData['group']]); + $manager->persist($user); + $users[] = $user; + unset($user); + } + + //Flush to get the ids + $manager->flush(); + } +} -- 2.41.1