marcins-code
2/3/2020 - 2:19 PM

User fixtures #users

User fixtures #users

<?php

namespace App\DataFixtures;

use App\Entity\Users;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class User extends Fixture
{
    private $passwordEncoder;

    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->passwordEncoder = $passwordEncoder;
    }

    public function load(ObjectManager $manager)
    {
        $user = new Users();
        $user->setUsername('someuser');
        $user->setPassword($this->passwordEncoder->encodePassword(
            $user, 'password'
        ));
        $user->setRoles(['ROLE_ADMIN', 'ROLE_SUPER_USER']);
        $user->setIsEnabled(true);

        $manager->persist($user);
        $manager->flush();

    }
}