Passed
Push — fix_coverage_in_scrutinizer ( cd0379...a04ba4 )
by Herberto
13:22
created

UserFixtures::load()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 31
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 29
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\DataFixtures\ORM;
13
14
use App\Entity\User;
15
use Doctrine\Common\DataFixtures\AbstractFixture;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
19
20
/**
21
 * Defines the sample users to load in the database before running the unit and
22
 * functional tests. Execute this command to load the data.
23
 *
24
 *   $ php bin/console doctrine:fixtures:load
25
 *
26
 * See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html
27
 *
28
 * @author Ryan Weaver <[email protected]>
29
 * @author Javier Eguiluz <[email protected]>
30
 * @author Yonel Ceruto <[email protected]>
31
 */
32
class UserFixtures extends AbstractFixture implements ContainerAwareInterface
33
{
34
    use ContainerAwareTrait;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function load(ObjectManager $manager): void
40
    {
41
        $passwordEncoder = $this->container->get('security.password_encoder');
42
43
        $janeAdmin = new User();
44
        $janeAdmin->setFullName('Jane Doe');
45
        $janeAdmin->setUsername('jane_admin');
46
        $janeAdmin->setEmail('[email protected]');
47
        $janeAdmin->setRoles(['ROLE_ADMIN']);
48
        $encodedPassword = $passwordEncoder->encodePassword($janeAdmin, 'kitten');
49
        $janeAdmin->setPassword($encodedPassword);
50
        $manager->persist($janeAdmin);
51
        // In case if fixture objects have relations to other fixtures, adds a reference
52
        // to that object by name and later reference it to form a relation.
53
        // See https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
54
        $this->addReference('jane-admin', $janeAdmin);
55
56
        $tomAdmin = new User();
57
        $tomAdmin->setFullName('Tom Doe');
58
        $tomAdmin->setUsername('tom_admin');
59
        $tomAdmin->setEmail('[email protected]');
60
        $tomAdmin->setRoles(['ROLE_ADMIN']);
61
        $encodedPassword = $passwordEncoder->encodePassword($tomAdmin, 'kitten');
62
        $tomAdmin->setPassword($encodedPassword);
63
        $manager->persist($tomAdmin);
64
        $this->addReference('tom-admin', $tomAdmin);
65
66
        $johnUser = new User();
67
        $johnUser->setFullName('John Doe');
68
        $johnUser->setUsername('john_user');
69
        $johnUser->setEmail('[email protected]');
70
        $encodedPassword = $passwordEncoder->encodePassword($johnUser, 'kitten');
71
        $johnUser->setPassword($encodedPassword);
72
        $manager->persist($johnUser);
73
        $this->addReference('john-user', $johnUser);
74
75
        $manager->flush();
76
    }
77
}
78