AbstractWebTestCase   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 18
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getToken() 0 18 2
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Tests\Controller;
13
14
use Faker\Factory;
15
use Faker\Generator;
16
use Lexik\Bundle\JWTAuthenticationBundle\Exception\JWTEncodeFailureException;
17
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
18
use Symfony\Component\BrowserKit\Client;
19
20
abstract class AbstractWebTestCase extends WebTestCase
21
{
22
    /**
23
     * @var Client
24
     */
25
    protected $client;
26
27
    /**
28
     * @var string|null
29
     */
30
    protected $token;
31
32
    /**
33
     * @var Generator
34
     */
35
    protected $faker;
36
37
    /**
38
     * AbstractWebTestCase constructor.
39
     *
40
     * @param string|null $name
41
     * @param array $data
42
     * @param string $dataName
43
     */
44
    public function __construct(?string $name = null, array $data = [], string $dataName = '')
45
    {
46
        parent::__construct($name, $data, $dataName);
47
48
        $this->client = static::createClient();
49
        $this->token = self::getToken();
50
        $this->faker = Factory::create();
51
    }
52
53
    /**
54
     * @return string|null
55
     */
56
    private static function getToken(): ?string
57
    {
58
        self::bootKernel();
59
60
        // returns the real and unchanged service container
61
        $container = self::$kernel->getContainer();
62
63
        $data = ['username' => '[email protected]', 'roles' => ['ROLE_ADMIN']];
64
65
        try {
66
            $token = $container
67
                ->get('lexik_jwt_authentication.encoder')
68
                ->encode($data);
69
        } catch (JWTEncodeFailureException $e) {
70
            echo $e->getMessage().PHP_EOL;
71
        }
72
73
        return $token;
74
    }
75
}
76