Completed
Push — master ( 419f51...37bf86 )
by Michał
08:39
created

appendToLogsUsernameIfUserHasToStringMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37

Duplication

Lines 37
Ratio 100 %

Importance

Changes 0
Metric Value
dl 37
loc 37
rs 9.328
c 0
b 0
f 0
cc 1
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A UserDataAppendingTest.php$0 ➔ __toString() 4 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dziki\MonologSentryBundle\Tests\Unit\SubscribedProcessor;
6
7
use Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\HttpKernel\KernelEvents;
11
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
12
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
15
class UserDataAppendingTest extends TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function isSubscribedToKernelRequest(): void
21
    {
22
        $subscribedEvents = UserDataAppending::getSubscribedEvents();
23
        $this->assertArrayHasKey(KernelEvents::REQUEST, $subscribedEvents);
24
        $this->assertSame(['onKernelRequest', 7], $subscribedEvents[KernelEvents::REQUEST]);
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function doNothingIfMissingToken(): void
31
    {
32
        /** @var TokenStorageInterface $tokenStorage */
33
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
34
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
35
36
        $userDataAppendingProcessor->onKernelRequest();
37
38
        $record = $userDataAppendingProcessor([]);
39
40
        $this->assertSame([], $record);
41
    }
42
43
    /**
44
     * @test
45
     */
46
    public function obtainUsernameIfUserImplementsUserInterface(): void
47
    {
48
        $user = $this->createMock(UserInterface::class);
49
        $user
50
            ->expects($this->once())
51
            ->method('getUsername')
52
            ->willReturn('user')
53
        ;
54
55
        $token = $this->createMock(TokenInterface::class);
56
        $token
57
            ->method('getUser')
58
            ->willReturn($user)
59
        ;
60
61
        /** @var TokenStorageInterface|MockObject $tokenStorage */
62
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
63
        $tokenStorage
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...\TokenStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
            ->method('getToken')
65
            ->willReturn($token)
66
        ;
67
68
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
69
        $userDataAppendingProcessor->onKernelRequest();
70
    }
71
72
    /**
73
     * @test
74
     */
75 View Code Duplication
    public function appendToLogsUsernameIfUserIsString(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        $user = 'user';
78
79
        $token = $this->createMock(TokenInterface::class);
80
        $token
81
            ->method('getUser')
82
            ->willReturn($user)
83
        ;
84
85
        /** @var TokenStorageInterface|MockObject $tokenStorage */
86
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
87
        $tokenStorage
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...\TokenStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            ->method('getToken')
89
            ->willReturn($token)
90
        ;
91
92
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
93
94
        $userDataAppendingProcessor->onKernelRequest();
95
96
        $record = $userDataAppendingProcessor([]);
97
98
        $this->assertSame(
99
            ['context' => [
100
                'user' => [
101
                    'username' => 'user',
102
                ],
103
            ],
104
            ],
105
            $record
106
        );
107
    }
108
109
    /**
110
     * @test
111
     */
112 View Code Duplication
    public function appendToLogsUsernameIfUserHasToStringMethod(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        $user = new class() {
115
            public function __toString()
116
            {
117
                return 'user';
118
            }
119
        };
120
121
        $token = $this->createMock(TokenInterface::class);
122
        $token
123
            ->method('getUser')
124
            ->willReturn($user)
125
        ;
126
127
        /** @var TokenStorageInterface|MockObject $tokenStorage */
128
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
129
        $tokenStorage
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...\TokenStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            ->method('getToken')
131
            ->willReturn($token)
132
        ;
133
134
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
135
        $userDataAppendingProcessor->onKernelRequest();
136
137
        $record = $userDataAppendingProcessor([]);
138
139
        $this->assertSame(
140
            ['context' => [
141
                'user' => [
142
                    'username' => 'user',
143
                ],
144
            ],
145
            ],
146
            $record
147
        );
148
    }
149
}
150