Passed
Push — main ( e090ab...58e8d0 )
by Slawomir
04:24
created

createAuthenticatedClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace App\Tests\Modules\Security\Integration;
4
5
use App\Tests\TestUtils\IntegrationTest;
6
use DusanKasan\Knapsack\Collection;
7
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
8
9
abstract class SecurityIntegrationTest extends IntegrationTest
10
{
11
12
    protected ?string $token = null;
13
14
    /**
15
     * Create a client with a default Authorization header.
16
     *
17
     *
18
     * @return KernelBrowser
19
     */
20
    protected function createAuthenticatedClient(): KernelBrowser
21
    {
22
        $client = static::createClient();
23
        $client->setServerParameter('HTTP_Authorization', sprintf('Bearer %s',
24
                $this->createToken($client))
25
        );
26
        return $client;
27
    }
28
29
    private function createToken(KernelBrowser $client): string
30
    {
31
        $client->request(
32
            'POST',
33
            '/api/login_check',
34
            array(),
35
            array(),
36
            array('CONTENT_TYPE' => 'application/json'),
37
            json_encode(array(
38
                'username' => self::DEFAULT_USER_ID,
39
                'password' => self::DEFAULT_USER_PASSWORD,
40
            ))
41
        );
42
        $cookies = $client->getResponse()->headers->getCookies();
43
        $bearer = Collection::from($cookies)
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $bearer is correct as DusanKasan\Knapsack\Coll...ion(...) { /* ... */ }) targeting DusanKasan\Knapsack\Collection::find() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
            ->find(function ($c) {
45
                return $c->getName() == "BEARER";
46
            });
47
        if ($bearer == null) {
1 ignored issue
show
introduced by
The condition $bearer == null is always true.
Loading history...
48
            $this->fail("Unable to Authenticate in Test");
49
        }
50
        $this->token = $bearer->getValue();
51
        return $this->token;
52
    }
53
}