Completed
Push — master ( 35528d...2fa5ae )
by Paweł
34:34 queued 34:19
created

theStoreHasCustomerAccountWithEmailAndPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
16
use Sylius\Component\Resource\Factory\FactoryInterface;
17
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
18
use Sylius\Component\User\Repository\CustomerRepositoryInterface;
19
use Sylius\Component\User\Repository\UserRepositoryInterface;
20
use Sylius\Component\User\Security\PasswordUpdaterInterface;
21
22
/**
23
 * @author Anna Walasek <[email protected]>
24
 */
25
final class CustomerContext implements Context
26
{
27
    const DEFAULT_CUSTOMER_FIRST_NAME = 'John';
28
    const DEFAULT_CUSTOMER_LAST_NAME = 'Doe';
29
30
    /**
31
     * @var SharedStorageInterface
32
     */
33
    private $sharedStorage;
34
35
    /**
36
     * @var CustomerRepositoryInterface
37
     */
38
    private $customerRepository;
39
40
    /**
41
     * @var FactoryInterface
42
     */
43
    private $customerFactory;
44
45
    /**
46
     * @var FactoryInterface
47
     */
48
    private $userFactory;
49
50
    /**
51
     * @param SharedStorageInterface $sharedStorage
52
     * @param CustomerRepositoryInterface $customerRepository
53
     * @param FactoryInterface $customerFactory
54
     */
55
    public function __construct(
56
        SharedStorageInterface $sharedStorage,
57
        CustomerRepositoryInterface $customerRepository,
58
        FactoryInterface $customerFactory,
59
        FactoryInterface $userFactory
60
    ) {
61
        $this->sharedStorage = $sharedStorage;
62
        $this->customerRepository = $customerRepository;
63
        $this->customerFactory = $customerFactory;
64
        $this->userFactory = $userFactory;
65
    }
66
67
    /**
68
     * @Given the store has customer :name with email :email
69
     */
70
    public function theStoreHasCustomerWithNameAndEmail($name, $email)
71
    {
72
        $partsOfName = explode(' ', $name);
73
        $this->createCustomer($email, $partsOfName[0], $partsOfName[1]);
74
    }
75
76
    /**
77
     * @Given the store has customer :email
78
     */
79
    public function theStoreHasCustomer($email)
80
    {
81
        $this->createCustomer($email, self::DEFAULT_CUSTOMER_FIRST_NAME, self::DEFAULT_CUSTOMER_LAST_NAME);
82
    }
83
84
    /**
85
     * @Given the store has customer :email with first name :firstName
86
     */
87
    public function theStoreHasCustomerWithFirstName($email, $firstName)
88
    {
89
        $this->createCustomer($email, $firstName, self::DEFAULT_CUSTOMER_LAST_NAME);
90
    }
91
92
    /**
93
     * @Given the store has customer :email with last name :lastName
94
     */
95
    public function theStoreHasCustomerWithLastName($email, $lastName)
96
    {
97
        $this->createCustomer($email, self::DEFAULT_CUSTOMER_FIRST_NAME, $lastName);
98
    }
99
100
    /**
101
     * @Given there is disabled customer account :email with password :password
102
     */
103
    public function thereIsDisabledCustomerAccountWithPassword($email, $password)
104
    {
105
        $this->createCustomerWithUserAccount($email, $password, false);
106
    }
107
108
    /**
109
     * @Given there is enabled customer account :email with password :password
110
     */
111
    public function theStoreHasEnabledCustomerAccountWithPassword($email, $password)
112
    {
113
        $this->createCustomerWithUserAccount($email, $password, true);
114
    }
115
116
    /**
117
     * @Given there is a customer :name identified by an email :email and a password :password
118
     */
119
    public function theStoreHasCustomerAccountWithEmailAndPassword($name, $email, $password)
120
    {
121
        $names = explode(' ', $name);
122
        $firstName = $names[0];
123
        $lastName = count($names) > 1 ? $names[1] : self::DEFAULT_CUSTOMER_LAST_NAME;
124
125
        $this->createCustomerWithUserAccount($email, $password, true, $firstName, $lastName);
126
    }
127
128
    /**
129
     * @param string $email
130
     * @param string $firstName
131
     * @param string $lastName
132
     */
133
    private function createCustomer($email, $firstName, $lastName)
134
    {
135
        $customer = $this->customerFactory->createNew();
136
137
        $customer->setFirstName($firstName);
138
        $customer->setLastName($lastName);
139
        $customer->setEmail($email);
140
141
        $this->sharedStorage->set('customer', $customer);
142
        $this->customerRepository->add($customer);
143
    }
144
145
    /**
146
     * @param string $email
147
     * @param string $password
148
     * @param bool $enabled
149
     * @param string $firstName
150
     * @param string $lastName
151
     */
152
    private function createCustomerWithUserAccount(
153
        $email,
154
        $password,
155
        $enabled = true,
156
        $firstName = self::DEFAULT_CUSTOMER_FIRST_NAME,
157
        $lastName = self::DEFAULT_CUSTOMER_LAST_NAME
158
    ) {
159
        $user = $this->userFactory->createNew();
160
        $customer = $this->customerFactory->createNew();
161
162
        $customer->setFirstname($firstName);
163
        $customer->setLastname($lastName);
164
        $customer->setEmail($email);
165
166
        $user->setUsername($email);
167
        $user->setPlainPassword($password);
168
        $user->setEnabled($enabled);
169
170
        $customer->setUser($user);
171
172
        $this->sharedStorage->set('customer', $customer);
173
        $this->customerRepository->add($customer);
174
    }
175
}
176