Completed
Push — master ( 429264...faaec3 )
by Paweł
25s
created

theStoreHasCustomerServiceAccountIdentifiedBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
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
     * @Given there is a user :email
87
     */
88
    public function thereIsUserIdentifiedBy($email, $password = 'sylius')
89
    {
90
        $user = $this->userFactory->create($email, $password);
0 ignored issues
show
Bug introduced by
The call to create() misses some required arguments starting with $firstName.
Loading history...
91
92
        $this->sharedStorage->set('user', $user);
93
94
        $this->userRepository->add($user);
95
    }
96
97
    /**
98
     * @Given there is user :email identified by :password, with :country as shipping country
99
     */
100
    public function thereIsUserWithShippingCountry($email, $password, $country)
101
    {
102
        $user = $this->userFactory->create($email, $password);
0 ignored issues
show
Bug introduced by
The call to create() misses some required arguments starting with $firstName.
Loading history...
103
104
        $customer = $user->getCustomer();
105
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
106
107
        $this->sharedStorage->set('user', $user);
108
        $this->userRepository->add($user);
109
    }
110
111
    /**
112
     * @Given my default shipping address is :country
113
     */
114
    public function myDefaultShippingAddressIs($country)
115
    {
116
        $user = $this->sharedStorage->get('user');
117
        $customer = $user->getCustomer();
118
        $customer->setShippingAddress($this->createAddress($customer->getFirstName(), $customer->getLastName(), $country));
119
120
        $this->userManager->flush();
121
    }
122
123
    /**
124
     * @Given the account of :email was deleted
125
     */
126
    public function accountWasDeleted($email)
127
    {
128
        $user = $this->userRepository->findOneByEmail($email);
129
130
        $this->sharedStorage->set('customer', $user->getCustomer());
131
132
        $this->userRepository->remove($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 128 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...
133
    }
134
135
    /**
136
     * @Given his account was deleted
137
     */
138
    public function hisAccountWasDeleted()
139
    {
140
        $user = $this->sharedStorage->get('user');
141
142
        $this->userRepository->remove($user);
143
    }
144
145
    /**
146
     * @Given there is an administrator identified by :email
147
     */
148
    public function theStoreHasCustomerServiceAccountIdentifiedBy($email)
149
    {
150
        $administrator = $this->userFactory->createDefaultAdmin();
151
        $administrator->setEmail($email);
152
153
        $this->sharedStorage->set('customer_service', $administrator);
154
        $this->userRepository->add($administrator);
155
    }
156
157
    /**
158
     * @param string $firstName
159
     * @param string $lastName
160
     * @param string $country
161
     * @param string $street
162
     * @param string $city
163
     * @param string $postcode
164
     *
165
     * @return AddressInterface
166
     */
167
    private function createAddress(
168
        $firstName,
169
        $lastName,
170
        $country = 'United States',
171
        $street = 'Jones St. 114',
172
        $city = 'Paradise City',
173
        $postcode = '99999'
174
    ) {
175
        $address = $this->addressFactory->createNew();
176
        $address->setFirstName($firstName);
177
        $address->setLastName($lastName);
178
        $address->setStreet($street);
179
        $address->setCity($city);
180
        $address->setPostcode($postcode);
181
        $address->setCountryCode($this->countryCodeConverter->convertToCode($country));
182
183
        return $address;
184
    }
185
}
186