OroContext   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 9
c 4
b 1
f 2
lcom 1
cbo 4
dl 0
loc 103
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A startTransaction() 0 4 1
A rollbackTransaction() 0 4 1
A reindex() 0 7 1
A setWsseHeader() 0 4 1
A getClient() 0 12 3
B generateWsseAuthHeader() 0 25 2
1
<?php
2
3
namespace Indigo\Oro\Behat\Context;
4
5
use Behat\Symfony2Extension\Context\KernelAwareContext;
6
use Behat\Symfony2Extension\Context\KernelDictionary;
7
use Oro\Bundle\SearchBundle\Engine\EngineInterface;
8
use Oro\Bundle\TestFrameworkBundle\Test\Client;
9
10
/**
11
 * Context containing Oro hooks.
12
 *
13
 * @author Márk Sági-Kazár <[email protected]>
14
 */
15
class OroContext implements KernelAwareContext
16
{
17
    use KernelDictionary;
18
19
    /** Default WSSE credentials */
20
    const USER_NAME = 'admin';
21
    const USER_PASSWORD = 'admin_api_key';
22
23
    /**
24
     * @var Client
25
     */
26
    private $client;
27
28
    /**
29
     * @BeforeScenario @dbIsolation
30
     */
31
    public function startTransaction()
32
    {
33
        $this->getClient()->startTransaction();
34
    }
35
36
    /**
37
     * @AfterScenario @dbIsolation
38
     */
39
    public function rollbackTransaction()
40
    {
41
        $this->getClient()->rollbackTransaction();
42
    }
43
44
    /**
45
     * @BeforeScenario @dbIsolation&&@dbReindex
46
     */
47
    public function reindex()
48
    {
49
        /** @var EngineInterface $searchEngine */
50
        $searchEngine = $this->getContainer()->get('oro_search.search.engine');
51
52
        $searchEngine->reindex();
53
    }
54
55
    /**
56
     * @BeforeScenario @wsse
57
     */
58
    public function setWsseHeader()
59
    {
60
        $this->getClient()->setServerParameters($this->generateWsseAuthHeader());
61
    }
62
63
    /**
64
     * Returns the test client.
65
     *
66
     * @return Client
67
     */
68
    public function getClient()
69
    {
70
        if (null === $this->client) {
71
            $this->client = $this->getContainer()->get('test.client');
72
73
            if (false === $this->client instanceof Client) {
74
                throw new \RuntimeException('The test client must be an instance of Oro\Bundle\TestFrameworkBundle\Test\Client');
75
            }
76
        }
77
78
        return $this->client;
79
    }
80
81
    /**
82
     * Generates WSSE Auth header.
83
     *
84
     * {@link \Oro\Bundle\TestFrameworkBundle\Test\WebTestCase}
85
     *
86
     * @param string      $userName
87
     * @param string      $userPassword
88
     * @param string|null $nonce
89
     *
90
     * @return array
91
     */
92
    public function generateWsseAuthHeader(
93
        $userName = self::USER_NAME,
94
        $userPassword = self::USER_PASSWORD,
95
        $nonce = null
96
    ) {
97
        if (null === $nonce) {
98
            $nonce = uniqid();
99
        }
100
101
        $created = date('c');
102
        $digest = base64_encode(sha1(base64_decode($nonce).$created.$userPassword, true));
103
        $wsseHeader = [
104
            'CONTENT_TYPE' => 'application/json',
105
            'HTTP_Authorization' => 'WSSE profile="UsernameToken"',
106
            'HTTP_X-WSSE' => sprintf(
107
                'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
108
                $userName,
109
                $digest,
110
                $nonce,
111
                $created
112
            ),
113
        ];
114
115
        return $wsseHeader;
116
    }
117
}
118