Completed
Push — master ( 7fc704...cfa808 )
by Paweł
33:04 queued 17:47
created

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

Labels
Severity

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\Component\Addressing\Converter\CountryNameConverterInterface;
17
use Sylius\Component\Addressing\Model\AddressInterface;
18
use Sylius\Component\Core\Test\Factory\TestUserFactoryInterface;
19
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Sylius\Component\User\Repository\UserRepositoryInterface;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 * @author Magdalena Banasiak <[email protected]>
26
 */
27
final class UserContext implements Context
28
{
29
    /**
30
     * @var SharedStorageInterface
31
     */
32
    private $sharedStorage;
33
34
    /**
35
     * @var UserRepositoryInterface
36
     */
37
    private $userRepository;
38
39
    /**
40
     * @var TestUserFactoryInterface
41
     */
42
    private $userFactory;
43
44
    /**
45
     * @var FactoryInterface
46
     */
47
    private $addressFactory;
48
49
    /**
50
     * @var ObjectManager
51
     */
52
    private $userManager;
53
54
    /**
55
     * @var CountryNameConverterInterface
56
     */
57
    private $countryCodeConverter;
58
59
    /**
60
     * @param SharedStorageInterface $sharedStorage
61
     * @param UserRepositoryInterface $userRepository
62
     * @param TestUserFactoryInterface $userFactory
63
     * @param FactoryInterface $addressFactory
64
     * @param ObjectManager $userManager
65
     * @param CountryNameConverterInterface $countryCodeConverter
66
     */
67
    public function __construct(
68
        SharedStorageInterface $sharedStorage,
69
        UserRepositoryInterface $userRepository,
70
        TestUserFactoryInterface $userFactory,
71
        FactoryInterface $addressFactory,
72
        ObjectManager $userManager,
73
        CountryNameConverterInterface $countryCodeConverter
74
    ) {
75
        $this->sharedStorage = $sharedStorage;
76
        $this->userRepository = $userRepository;
77
        $this->userFactory = $userFactory;
78
        $this->addressFactory = $addressFactory;
79
        $this->userManager = $userManager;
80
        $this->countryCodeConverter = $countryCodeConverter;
81
    }
82
83
    /**
84
     * @Given there is user :email identified by :password
85
     * @Given there was account of :email with password :password
86
     */
87
    public function thereIsUserIdentifiedBy($email, $password)
88
    {
89
        $user = $this->userFactory->create($email, $password);
0 ignored issues
show
The call to create() misses some required arguments starting with $firstName.
Loading history...
90
91
        $this->sharedStorage->set('user', $user);
92
93
        $this->userRepository->add($user);
94
    }
95
96
    /**
97
     * @Given there is user :email identified by :password, with :country as shipping country
98
     */
99
    public function thereIsUserWithShippingCountry($email, $password, $country)
100
    {
101
        $user = $this->userFactory->create($email, $password);
0 ignored issues
show
The call to create() misses some required arguments starting with $firstName.
Loading history...
102
103
        $customer = $user->getCustomer();
104
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
105
106
        $this->sharedStorage->set('user', $user);
107
        $this->userRepository->add($user);
108
    }
109
110
    /**
111
     * @Given my default shipping address is :country
112
     */
113
    public function myDefaultShippingAddressIs($country)
114
    {
115
        $user = $this->sharedStorage->get('user');
116
        $customer = $user->getCustomer();
117
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
118
119
        $this->userManager->flush();
120
    }
121
122
    /**
123
     * @Given the account of :email was deleted
124
     */
125
    public function accountWasDeleted($email)
126
    {
127
        $user = $this->userRepository->findOneByEmail($email);
128
129
        $this->sharedStorage->set('customer', $user->getCustomer());
130
131
        $this->userRepository->remove($user);
132
    }
133
134
    /**
135
     * @param string $firstName
136
     * @param string $lastName
137
     * @param string $country
138
     * @param string $street
139
     * @param string $city
140
     * @param string $postcode
141
     *
142
     * @return AddressInterface
143
     */
144
    private function createAddress(
145
        $firstName,
146
        $lastName,
147
        $country = 'United States',
148
        $street = 'Jones St. 114',
149
        $city = 'Paradise City',
150
        $postcode = '99999'
151
    ) {
152
        $address = $this->addressFactory->createNew();
153
        $address->setFirstName($firstName);
154
        $address->setLastName($lastName);
155
        $address->setStreet($street);
156
        $address->setCity($city);
157
        $address->setPostcode($postcode);
158
        $address->setCountryCode($this->countryCodeConverter->convertToCode($country));
159
160
        return $address;
161
    }
162
163
    /**
164
     * @Given his account was deleted
165
     */
166
    public function hisAccountWasDeleted()
167
    {
168
        $user = $this->sharedStorage->get('user');
169
170
        $this->userRepository->remove($user);
171
    }
172
}
173