Completed
Push — master ( 860366...55daec )
by Kamil
19:37
created

SecurityContext::iAmLoggedInCustomer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
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 Behat\Mink\Driver\Selenium2Driver;
16
use Behat\Mink\Session;
17
use Sylius\Behat\Page\Shop\HomePage;
18
use Sylius\Bundle\CoreBundle\Test\Services\SecurityServiceInterface;
19
use Sylius\Component\Core\Test\Factory\TestUserFactoryInterface;
20
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
23
/**
24
 * @author Arkadiusz Krakowiak <[email protected]>
25
 */
26
final class SecurityContext implements Context
27
{
28
    /**
29
     * @var SecurityServiceInterface
30
     */
31
    private $securityService;
32
33
    /**
34
     * @var Session
35
     */
36
    private $minkSession;
37
38
    /**
39
     * @var array
40
     */
41
    private $minkParameters;
42
43
    /**
44
     * @var HomePage
45
     */
46
    private $homePage;
47
48
    /**
49
     * @var TestUserFactoryInterface
50
     */
51
    private $testUserFactory;
52
53
    /**
54
     * @var RepositoryInterface
55
     */
56
    private $userRepository;
57
58
    /**
59
     * @var SharedStorageInterface
60
     */
61
    private $sharedStorage;
62
63
    /**
64
     * @param SecurityServiceInterface $securityService
65
     * @param Session $minkSession
66
     * @param array $minkParameters
67
     * @param HomePage $homePage
68
     * @param TestUserFactoryInterface $testUserFactory
69
     * @param RepositoryInterface $userRepository
70
     * @param SharedStorageInterface $sharedStorage
71
     */
72
    public function __construct(
73
        SecurityServiceInterface $securityService,
74
        Session $minkSession,
75
        array $minkParameters,
76
        HomePage $homePage,
77
        TestUserFactoryInterface $testUserFactory,
78
        RepositoryInterface $userRepository,
79
        SharedStorageInterface $sharedStorage
80
    ) {
81
        $this->securityService = $securityService;
82
        $this->minkSession = $minkSession;
83
        $this->minkParameters = $minkParameters;
84
        $this->homePage = $homePage;
85
        $this->testUserFactory = $testUserFactory;
86
        $this->userRepository = $userRepository;
87
        $this->sharedStorage = $sharedStorage;
88
    }
89
90
    /**
91
     * @Given I am logged in as :email
92
     */
93
    public function iAmLoggedInAs($email)
94
    {
95
        $this->prepareSessionIfNeeded();
96
        $this->securityService->logIn($email, $this->minkSession);
97
    }
98
99
    /**
100
     * @Given /^I am logged in customer$/
101
     */
102
    public function iAmLoggedInCustomer()
103
    {
104
        $user = $this->testUserFactory->createDefault();
105
        $this->userRepository->add($user);
106
107
        $this->securityService->logIn($user->getEmail(), $this->minkSession);
108
109
        $this->sharedStorage->setCurrentResource('user', $user);
110
    }
111
112
    private function prepareSessionIfNeeded()
113
    {
114
        if (!$this->minkSession->getDriver() instanceof Selenium2Driver) {
115
            return;
116
        }
117
118
        if (false !== strpos($this->minkSession->getCurrentUrl(), $this->minkParameters['base_url'])) {
119
            return;
120
        }
121
122
        $this->homePage->open();
123
    }
124
}
125