UsersContext::thereAreUsersWithNames()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Donut Social Network - Yet another experimental social network.
5
 * Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Donut Social Network.
8
 *
9
 * Donut Social Network is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Donut Social Network is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Donut Social Network.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Donut Social Network
23
 * @copyright Copyright (C) 2016-2017, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/donut/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace AppBundle\FeatureContexts\Setup;
29
30
use Angelov\Donut\Behat\Service\Storage\StorageInterface;
31
use Angelov\Donut\Core\CommandBus\CommandBusInterface;
32
use Angelov\Donut\Core\UuidGenerator\UuidGeneratorInterface;
33
use Angelov\Donut\Places\City;
34
use Angelov\Donut\Users\Commands\StoreUserCommand;
35
use Angelov\Donut\Users\Repositories\UsersRepositoryInterface;
36
use Behat\Behat\Context\Context;
37
use Doctrine\ORM\EntityManagerInterface;
38
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
39
40
class UsersContext implements Context
41
{
42
    private $em;
43
    private $storage;
44
    private $passwordEncoder;
45
    private $uuidGenerator;
46
    private $users;
47
    private $commandBus;
48
49
    public function __construct(
50
        EntityManagerInterface $entityManager,
51
        UserPasswordEncoder $passwordEncoder,
52
        StorageInterface $storage,
53
        UuidGeneratorInterface $uuidGenerator,
54
        UsersRepositoryInterface $users,
55
        CommandBusInterface $commandBus
56
    ) {
57
        $this->em = $entityManager;
58
        $this->storage = $storage;
59
        $this->passwordEncoder = $passwordEncoder;
60
        $this->uuidGenerator = $uuidGenerator;
61
        $this->users = $users;
62
        $this->commandBus = $commandBus;
63
    }
64
65
    /**
66
     * @Given there is a user :name with email :email and password :password
67
     */
68
    public function thereIsAUserWithEmailAndPassword($name, $email, $password) : void
69
    {
70
        $id = $this->uuidGenerator->generate();
71
        $city = new City($id, 'Valandovo');
72
        $this->em->persist($city);
73
74
        $id = $this->uuidGenerator->generate();
75
        $this->commandBus->handle(new StoreUserCommand($id, $name, $email, $password, $city->getId()));
76
77
        $user = $this->users->find($id);
78
79
        $key = 'created_user_' . $name;
80
        $this->storage->set($key, $user);
81
82
        $this->storage->set('last_created_user', $user);
83
    }
84
85
    /**
86
     * @Given I am :name
87
     */
88
    public function iAm($name) : void
89
    {
90
        $this->storage->set('user_name', $name);
91
    }
92
93
    /**
94
     * @Given I am registered with email :email and password :password
95
     */
96
    public function iAmRegisteredWithEmailAndPassword($email, $password) : void
97
    {
98
        $name = $this->storage->get('user_name', 'John Smith');
99
        $this->thereIsAUserWithEmailAndPassword($name, $email, $password);
100
    }
101
102
    /**
103
     * @Given there are users :first, :second, :third, :fourth, :fifth and :sixth
104
     * @Given there are users :first, :second, :third and :fourth
105
     * @Given there are users :first, :second and :third
106
     * @Given there are users :first and :second
107
     * @Given there is a user :first
108
     */
109
    public function thereAreUsersWithNames(string ...$names) : void
110
    {
111
        foreach ($names as $name) {
112
            $email = str_replace(' ', '.', strtolower($name));
113
            $this->thereIsAUserWithEmailAndPassword($name, $email, '123456'); // @todo use a factory or something
114
        }
115
    }
116
117
    /**
118
     * @Given there is a user :name with email :email
119
     */
120
    public function thereIsAUserNameWithEmail(string $name, string $email) : void
121
    {
122
        $this->thereIsAUserWithEmailAndPassword($name, $email, '123456');
123
    }
124
}
125