Completed
Push — master ( 9305d9...d34e55 )
by Tarmo
46:41
created

WebTestCase::getFastestHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Utils/Tests/WebTestCase.php
5
 *
6
 * @author  TLe, Tarmo Leppänen <[email protected]>
7
 */
8
namespace App\Utils\Tests;
9
10
use Symfony\Bundle\FrameworkBundle\Client;
11
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Class WebTestCase
16
 *
17
 * @package App\Tests
18
 * @author  TLe, Tarmo Leppänen <[email protected]>
19
 */
20
class WebTestCase extends BaseWebTestCase
21
{
22
    /**
23
     * @var ContainerInterface
24
     */
25
    private $container;
26
27
    /**
28
     * @var Auth
29
     */
30
    private $authService;
31
32
    /**
33
     * Getter method for container
34
     *
35
     * @return ContainerInterface
36
     */
37
    public function getContainer(): ContainerInterface
38
    {
39
        if (!($this->container instanceof ContainerInterface)) {
40
            self::bootKernel();
41
42
            $this->container = static::$kernel->getContainer();
43
        }
44
45
        return $this->container;
46
    }
47
48
    /**
49
     * Getter method for auth service
50
     *
51
     * @return Auth
52
     */
53
    public function getAuthService(): Auth
54
    {
55
        if (!($this->authService instanceof Auth)) {
56
            // We need to boot kernel up to get auth service
57
            self::bootKernel();
58
59
            $this->authService = $this->getContainer()->get(Auth::class);
60
        }
61
62
        return $this->authService;
63
    }
64
65
    /**
66
     * Helper method to get authorized client for specified username and password.
67
     *
68
     * @param string|null $username
69
     * @param string|null $password
70
     * @param array       $options
71
     * @param array       $server
72
     *
73
     * @return Client
74
     */
75
    public function getClient(
76
        string $username = null,
77
        string $password = null,
78
        array $options = null,
79
        array $server = null
80
    ): Client {
81
        $options = $options ?? [];
82
        $server = $server ?? [];
83
84
        // Merge authorization headers
85
        $server = \array_merge(
86
            $username === null ? [] : $this->getAuthService()->getAuthorizationHeadersForUser($username, $password),
87
            \array_merge($this->getJsonHeaders(), $this->getFastestHeaders()),
88
            $this->getAuthService()->getJwtHeaders(),
89
            $server
90
        );
91
92
        return static::createClient($options, $server);
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getJsonHeaders(): array
99
    {
100
        return [
101
            'CONTENT_TYPE'          => 'application/json',
102
            'HTTP_X-Requested-With' => 'XMLHttpRequest',
103
        ];
104
    }
105
106
    /**
107
     * @codeCoverageIgnore
108
     *
109
     * @return array
110
     */
111
    public function getFastestHeaders(): array
112
    {
113
        $output = [];
114
115
        if (\getenv('ENV_TEST_CHANNEL_READABLE')) {
116
            $output = [
117
                'X-FASTEST-ENV-TEST-CHANNEL-READABLE' => \getenv('ENV_TEST_CHANNEL_READABLE'),
118
            ];
119
        }
120
121
        return $output;
122
    }
123
}
124