Passed
Push — master ( 35eebd...7b8a1a )
by Roman
16:45
created

testRaiseExceptionWhenTokenIsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Tests\Game\Features\Answers\Authentication;
6
7
use App\Game\Features\Answers\Authentication\PlayerFromTokenExtractor;
8
use App\Game\Features\Answers\Authentication\PlayerNotFoundInTokenStorageException;
9
use App\Game\Features\Answers\Player\PlayerDto;
10
use App\Game\Features\Player\Player\PlayerId;
11
use App\Tests\Game\GameTestCase;
12
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...on\Token\TokenInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
/**
16
 * @coversDefaultClass \App\Game\Features\Answers\Authentication\PlayerFromTokenExtractor
17
 */
18
final class PlayerFromTokenExtractorTest extends GameTestCase
19
{
20
    /**
21
     * @covers ::player
22
     */
23
    public function testExtractPlayerFromTokenStorage(): void
24
    {
25
        $player = $this->createPlayer(new PlayerId());
26
27
        $playerArray['id'] = (string) $player->playerId();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$playerArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $playerArray = array(); before regardless.
Loading history...
28
        $playerArray['nickname'] = $player->nickname();
29
        $playerArray['level'] = $player->level();
30
        $playerArray['role'] = $player->role()->getValue();
31
32
        $tokenMock = $this->createMock(TokenInterface::class);
33
        $tokenMock->method('getUser')->willReturn(json_encode($playerArray, JSON_THROW_ON_ERROR));
34
35
        $tokenStorageMock = $this->createMock(TokenStorageInterface::class);
36
        $tokenStorageMock->method('getToken')->willReturn($tokenMock);
37
38
        $playerFromTokenExtractor = new PlayerFromTokenExtractor($tokenStorageMock);
39
40
        self::assertInstanceOf(PlayerDto::class, $playerFromTokenExtractor->player());
41
    }
42
43
    /**
44
     * @covers ::player
45
     */
46
    public function testRaiseExceptionWhenTokenIsNull(): void
47
    {
48
        $this->expectException(PlayerNotFoundInTokenStorageException::class);
49
50
        $tokenStorageMock = $this->createMock(TokenStorageInterface::class);
51
        $tokenStorageMock->method('getToken')->willReturn(null);
52
53
        $playerFromTokenExtractor = new PlayerFromTokenExtractor($tokenStorageMock);
54
        $playerFromTokenExtractor->player();
55
    }
56
57
    /**
58
     * @covers ::player
59
     */
60
    public function testRaiseExceptionWhenPlayerIsNotDecoded(): void
61
    {
62
        $this->expectException(PlayerNotFoundInTokenStorageException::class);
63
64
        $tokenMock = $this->createMock(TokenInterface::class);
65
        $tokenMock->method('getUser')->willReturn('', JSON_THROW_ON_ERROR);
66
67
        $tokenStorageMock = $this->createMock(TokenStorageInterface::class);
68
        $tokenStorageMock->method('getToken')->willReturn($tokenMock);
69
70
        $playerFromTokenExtractor = new PlayerFromTokenExtractor($tokenStorageMock);
71
        $playerFromTokenExtractor->player();
72
    }
73
}
74