Completed
Push — master ( 37aba0...533498 )
by Paweł
09:21
created

src/Sylius/Behat/Context/Setup/UserContext.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
17
use Sylius\Component\Addressing\Converter\CountryNameConverterInterface;
18
use Sylius\Component\Addressing\Model\AddressInterface;
19
use Sylius\Behat\Service\SharedStorageInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\User\Model\UserInterface;
22
use Sylius\Component\User\Repository\UserRepositoryInterface;
23
24
/**
25
 * @author Arkadiusz Krakowiak <[email protected]>
26
 * @author Magdalena Banasiak <[email protected]>
27
 */
28
final class UserContext implements Context
29
{
30
    /**
31
     * @var SharedStorageInterface
32
     */
33
    private $sharedStorage;
34
35
    /**
36
     * @var UserRepositoryInterface
37
     */
38
    private $userRepository;
39
40
    /**
41
     * @var ExampleFactoryInterface
42
     */
43
    private $userFactory;
44
45
    /**
46
     * @var FactoryInterface
47
     */
48
    private $addressFactory;
49
50
    /**
51
     * @var ObjectManager
52
     */
53
    private $userManager;
54
55
    /**
56
     * @var CountryNameConverterInterface
57
     */
58
    private $countryCodeConverter;
59
60
    /**
61
     * @param SharedStorageInterface $sharedStorage
62
     * @param UserRepositoryInterface $userRepository
63
     * @param ExampleFactoryInterface $userFactory
64
     * @param FactoryInterface $addressFactory
65
     * @param ObjectManager $userManager
66
     * @param CountryNameConverterInterface $countryCodeConverter
67
     */
68
    public function __construct(
69
        SharedStorageInterface $sharedStorage,
70
        UserRepositoryInterface $userRepository,
71
        ExampleFactoryInterface $userFactory,
72
        FactoryInterface $addressFactory,
73
        ObjectManager $userManager,
74
        CountryNameConverterInterface $countryCodeConverter
75
    ) {
76
        $this->sharedStorage = $sharedStorage;
77
        $this->userRepository = $userRepository;
78
        $this->userFactory = $userFactory;
79
        $this->addressFactory = $addressFactory;
80
        $this->userManager = $userManager;
81
        $this->countryCodeConverter = $countryCodeConverter;
82
    }
83
84
    /**
85
     * @Given there is a user :email identified by :password
86
     * @Given there was account of :email with password :password
87
     * @Given there is a user :email
88
     * @Given there is a :email user
89
     */
90
    public function thereIsUserIdentifiedBy($email, $password = 'sylius')
91
    {
92
        $user = $this->userFactory->create(['email' => $email, 'password' => $password, 'enabled' => true]);
93
94
        $this->sharedStorage->set('user', $user);
95
96
        $this->userRepository->add($user);
97
    }
98
99
    /**
100
     * @Given there is user :email identified by :password, with :country as shipping country
101
     */
102
    public function thereIsUserWithShippingCountry($email, $password, $country)
103
    {
104
        $user = $this->userFactory->create(['email' => $email, 'password' => $password, 'enabled' => true]);
105
106
        $customer = $user->getCustomer();
107
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
108
109
        $this->sharedStorage->set('user', $user);
110
        $this->userRepository->add($user);
111
    }
112
113
    /**
114
     * @Given my default shipping address is :country
115
     */
116
    public function myDefaultShippingAddressIs($country)
117
    {
118
        $user = $this->sharedStorage->get('user');
119
        $customer = $user->getCustomer();
120
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
121
122
        $this->userManager->flush();
123
    }
124
125
    /**
126
     * @Given the account of :email was deleted
127
     * @Given my account :email was deleted
128
     */
129
    public function accountWasDeleted($email)
130
    {
131
        $user = $this->userRepository->findOneByEmail($email);
132
133
        $this->sharedStorage->set('customer', $user->getCustomer());
134
135
        $this->userRepository->remove($user);
0 ignored issues
show
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 131 can be null; however, Sylius\Component\Resourc...toryInterface::remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
136
    }
137
138
    /**
139
     * @Given its account was deleted
140
     */
141
    public function hisAccountWasDeleted()
142
    {
143
        $user = $this->sharedStorage->get('user');
144
145
        $this->userRepository->remove($user);
146
    }
147
148
    /**
149
     * @Given /^(this user) is not verified$/
150
     * @Given /^(I) have not verified my account (?:yet)$/
151
     */
152
    public function accountIsNotVerified(UserInterface $user)
153
    {
154
        $user->setVerifiedAt(null);
155
156
        $this->userManager->flush();
157
    }
158
159
    /**
160
     * @Given /^(?:(I) have|(this user) has) already received a verification email$/
161
     */
162
    public function iHaveReceivedVerificationEmail(UserInterface $user)
163
    {
164
        $this->prepareUserVerification($user);
165
    }
166
167
    /**
168
     * @Given a verification email has already been sent to :email
169
     */
170
    public function aVerificationEmailHasBeenSentTo($email)
171
    {
172
        $user = $this->userRepository->findOneByEmail($email);
173
174
        $this->prepareUserVerification($user);
0 ignored issues
show
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 172 can be null; however, Sylius\Behat\Context\Set...epareUserVerification() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
175
    }
176
177
    /**
178
     * @Given /^(I) have already verified my account$/
179
     */
180
    public function iHaveAlreadyVerifiedMyAccount(UserInterface $user)
181
    {
182
        $user->setVerifiedAt(new \DateTime());
183
184
        $this->userManager->flush();
185
    }
186
187
    /**
188
     * @param string $firstName
189
     * @param string $lastName
190
     * @param string $country
191
     * @param string $street
192
     * @param string $city
193
     * @param string $postcode
194
     *
195
     * @return AddressInterface
196
     */
197
    private function createAddress(
198
        $firstName,
199
        $lastName,
200
        $country = 'United States',
201
        $street = 'Jones St. 114',
202
        $city = 'Paradise City',
203
        $postcode = '99999'
204
    ) {
205
        $address = $this->addressFactory->createNew();
206
        $address->setFirstName($firstName);
207
        $address->setLastName($lastName);
208
        $address->setStreet($street);
209
        $address->setCity($city);
210
        $address->setPostcode($postcode);
211
        $address->setCountryCode($this->countryCodeConverter->convertToCode($country));
212
213
        return $address;
214
    }
215
216
    /**
217
     * @param UserInterface $user
218
     */
219
    private function prepareUserVerification(UserInterface $user)
220
    {
221
        $token = 'marryhadalittlelamb';
222
        $this->sharedStorage->set('verification_token', $token);
223
224
        $user->setEmailVerificationToken($token);
225
226
        $this->userManager->flush();
227
    }
228
}
229