Completed
Push — pagerfanta-fix ( 187c46...923c07 )
by Kamil
25:06 queued 03:45
created

UserContext::thereIsUserWithShippingCountry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 6
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 Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
17
use Sylius\Behat\Service\SharedStorageInterface;
18
use Sylius\Component\User\Model\UserInterface;
19
use Sylius\Component\User\Repository\UserRepositoryInterface;
20
21
/**
22
 * @author Arkadiusz Krakowiak <[email protected]>
23
 * @author Magdalena Banasiak <[email protected]>
24
 */
25
final class UserContext implements Context
26
{
27
    /**
28
     * @var SharedStorageInterface
29
     */
30
    private $sharedStorage;
31
32
    /**
33
     * @var UserRepositoryInterface
34
     */
35
    private $userRepository;
36
37
    /**
38
     * @var ExampleFactoryInterface
39
     */
40
    private $userFactory;
41
42
    /**
43
     * @var ObjectManager
44
     */
45
    private $userManager;
46
47
    /**
48
     * @param SharedStorageInterface $sharedStorage
49
     * @param UserRepositoryInterface $userRepository
50
     * @param ExampleFactoryInterface $userFactory
51
     * @param ObjectManager $userManager
52
     */
53
    public function __construct(
54
        SharedStorageInterface $sharedStorage,
55
        UserRepositoryInterface $userRepository,
56
        ExampleFactoryInterface $userFactory,
57
        ObjectManager $userManager
58
    ) {
59
        $this->sharedStorage = $sharedStorage;
60
        $this->userRepository = $userRepository;
61
        $this->userFactory = $userFactory;
62
        $this->userManager = $userManager;
63
    }
64
65
    /**
66
     * @Given there is a user :email identified by :password
67
     * @Given there was account of :email with password :password
68
     * @Given there is a user :email
69
     * @Given there is a :email user
70
     */
71
    public function thereIsUserIdentifiedBy($email, $password = 'sylius')
72
    {
73
        $user = $this->userFactory->create(['email' => $email, 'password' => $password, 'enabled' => true]);
74
75
        $this->sharedStorage->set('user', $user);
76
77
        $this->userRepository->add($user);
78
    }
79
80
    /**
81
     * @Given the account of :email was deleted
82
     * @Given my account :email was deleted
83
     */
84
    public function accountWasDeleted($email)
85
    {
86
        $user = $this->userRepository->findOneByEmail($email);
87
88
        $this->sharedStorage->set('customer', $user->getCustomer());
89
90
        $this->userRepository->remove($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 86 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...
91
    }
92
93
    /**
94
     * @Given its account was deleted
95
     */
96
    public function hisAccountWasDeleted()
97
    {
98
        $user = $this->sharedStorage->get('user');
99
100
        $this->userRepository->remove($user);
101
    }
102
103
    /**
104
     * @Given /^(this user) is not verified$/
105
     * @Given /^(I) have not verified my account (?:yet)$/
106
     */
107
    public function accountIsNotVerified(UserInterface $user)
108
    {
109
        $user->setVerifiedAt(null);
110
111
        $this->userManager->flush();
112
    }
113
114
    /**
115
     * @Given /^(?:(I) have|(this user) has) already received a verification email$/
116
     */
117
    public function iHaveReceivedVerificationEmail(UserInterface $user)
118
    {
119
        $this->prepareUserVerification($user);
120
    }
121
122
    /**
123
     * @Given a verification email has already been sent to :email
124
     */
125
    public function aVerificationEmailHasBeenSentTo($email)
126
    {
127
        $user = $this->userRepository->findOneByEmail($email);
128
129
        $this->prepareUserVerification($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userRepository->findOneByEmail($email) on line 127 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...
130
    }
131
132
    /**
133
     * @Given /^(I) have already verified my account$/
134
     */
135
    public function iHaveAlreadyVerifiedMyAccount(UserInterface $user)
136
    {
137
        $user->setVerifiedAt(new \DateTime());
138
139
        $this->userManager->flush();
140
    }
141
142
    /**
143
     * @param UserInterface $user
144
     */
145
    private function prepareUserVerification(UserInterface $user)
146
    {
147
        $token = 'marryhadalittlelamb';
148
        $this->sharedStorage->set('verification_token', $token);
149
150
        $user->setEmailVerificationToken($token);
151
152
        $this->userManager->flush();
153
    }
154
}
155