From: Raphaël Gertz Date: Thu, 31 Oct 2024 06:04:05 +0000 (+0100) Subject: Add bundle fixture X-Git-Tag: 0.5.1~4 X-Git-Url: https://git.rapsys.eu/userbundle/commitdiff_plain/408e3d492050ac34f994b6865bb3f8bac5dec044?ds=sidebyside Add bundle fixture --- 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(); + } +}