Failed Conditions
Push — master ( aacec5...b5a0b4 )
by Florent
04:50
created

AccessToken/AccessTokenRevocationTypeHintTest.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Core\Tests\AccessToken;
15
16
use OAuth2Framework\Component\Core\AccessToken\AccessToken;
17
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId;
18
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository;
19
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRevocationTypeHint;
20
use OAuth2Framework\Component\Core\Client\ClientId;
21
use OAuth2Framework\Component\Core\DataBag\DataBag;
22
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
23
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
24
use OAuth2Framework\Component\TokenRevocationEndpoint\TokenTypeHint;
25
use PHPUnit\Framework\TestCase;
26
use Prophecy\Argument;
27
28
/**
29
 * @group TypeHint
30
 * @group AccessTokenRevocationTypeHint
31
 */
32
final class AccessTokenRevocationTypeHintTest extends TestCase
33
{
34
    protected function setUp()
35
    {
36
        if (!\interface_exists(TokenTypeHint::class)) {
37
            static::markTestSkipped('The component "oauth2-framework/token-type" is not installed.');
38
        }
39
    }
40
41
    /**
42
     * @test
43
     */
44
    public function genericInformation()
45
    {
46
        static::assertEquals('access_token', $this->getAccessTokenRevocationTypeHint()->hint());
47
    }
48
49
    /**
50
     * @test
51
     */
52
    public function theTokenTypeHintCanFindATokenAndRevokeIt()
53
    {
54
        static::assertNull($this->getAccessTokenRevocationTypeHint()->find('UNKNOWN_TOKEN_ID'));
55
        $accessToken = $this->getAccessTokenRevocationTypeHint()->find('ACCESS_TOKEN_ID');
56
        static::assertInstanceOf(AccessToken::class, $accessToken);
57
        $this->getAccessTokenRevocationTypeHint()->revoke($accessToken);
0 ignored issues
show
It seems like $accessToken defined by $this->getAccessTokenRev...find('ACCESS_TOKEN_ID') on line 55 can be null; however, OAuth2Framework\Componen...ationTypeHint::revoke() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
        static::assertTrue(true);
59
    }
60
61
    /**
62
     * @var AccessTokenRevocationTypeHint|null
63
     */
64
    private $accessTokenTypeHint = null;
65
66
    public function getAccessTokenRevocationTypeHint(): AccessTokenRevocationTypeHint
67
    {
68
        if (null === $this->accessTokenTypeHint) {
69
            $accessToken = new AccessToken(
70
                new AccessTokenId('ACCESS_TOKEN_ID'),
71
                new ClientId('CLIENT_ID'),
72
                new UserAccountId('USER_ACCOUNT_ID'),
73
                new \DateTimeImmutable('now +1hour'),
74
                new DataBag([
75
                    'scope' => 'scope1 scope2',
76
                ]),
77
                new DataBag([]),
78
                new ResourceServerId('RESOURCE_SERVER_ID')
79
            );
80
            $accessTokenRepository = $this->prophesize(AccessTokenRepository::class);
81
            $accessTokenRepository->find(Argument::type(AccessTokenId::class))->will(function ($args) use ($accessToken) {
82
                if ('ACCESS_TOKEN_ID' === $args[0]->getValue()) {
83
                    return $accessToken;
84
                }
85
86
                return;
87
            });
88
            $accessTokenRepository->save(Argument::type(AccessToken::class))->will(function () {
89
            });
90
91
            $this->accessTokenTypeHint = new AccessTokenRevocationTypeHint(
92
                $accessTokenRepository->reveal()
93
            );
94
        }
95
96
        return $this->accessTokenTypeHint;
97
    }
98
}
99