|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jedisjeux. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Loïc Frémont |
|
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 App\Behat\Context\Setup; |
|
13
|
|
|
|
|
14
|
|
|
use App\Behat\Service\SecurityServiceInterface; |
|
15
|
|
|
use App\Behat\Service\SharedStorageInterface; |
|
16
|
|
|
use App\Entity\AppUser; |
|
17
|
|
|
use App\Fixture\Factory\ExampleFactoryInterface; |
|
18
|
|
|
use Behat\Behat\Context\Context; |
|
19
|
|
|
use Sylius\Component\User\Repository\UserRepositoryInterface; |
|
20
|
|
|
use Webmozart\Assert\Assert; |
|
21
|
|
|
|
|
22
|
|
|
final class AppSecurityContext implements Context |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var SharedStorageInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $sharedStorage; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var SecurityServiceInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $securityService; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ExampleFactoryInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $userFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var UserRepositoryInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $userRepository; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param SharedStorageInterface $sharedStorage |
|
46
|
|
|
* @param SecurityServiceInterface $securityService |
|
47
|
|
|
* @param ExampleFactoryInterface $userFactory |
|
48
|
|
|
* @param UserRepositoryInterface $userRepository |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
SharedStorageInterface $sharedStorage, |
|
52
|
|
|
SecurityServiceInterface $securityService, |
|
53
|
|
|
ExampleFactoryInterface $userFactory, |
|
54
|
|
|
UserRepositoryInterface $userRepository |
|
55
|
|
|
) { |
|
56
|
|
|
$this->sharedStorage = $sharedStorage; |
|
57
|
|
|
$this->securityService = $securityService; |
|
58
|
|
|
$this->userFactory = $userFactory; |
|
59
|
|
|
$this->userRepository = $userRepository; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @Given I am logged in as :email |
|
64
|
|
|
*/ |
|
65
|
|
|
public function iAmLoggedInAs($email) |
|
66
|
|
|
{ |
|
67
|
|
|
$user = $this->userRepository->findOneByEmail($email); |
|
68
|
|
|
Assert::notNull($user); |
|
69
|
|
|
|
|
70
|
|
|
$this->securityService->logIn($user); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @Given I am a logged in customer |
|
75
|
|
|
*/ |
|
76
|
|
|
public function iAmLoggedInAsACustomer() |
|
77
|
|
|
{ |
|
78
|
|
|
/** @var AppUser $user */ |
|
79
|
|
|
$user = $this->userFactory->create(['email' => '[email protected]', 'password' => 'password', 'roles' => ['ROLE_USER']]); |
|
80
|
|
|
$this->userRepository->add($user); |
|
81
|
|
|
|
|
82
|
|
|
$this->securityService->logIn($user); |
|
83
|
|
|
|
|
84
|
|
|
$this->sharedStorage->set('customer', $user->getCustomer()); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
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: