0   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 100 %

Coupling/Cohesion

Dependencies 0

Importance

Changes 0
Metric Value
wmc 1
cbo 0
dl 6
loc 6
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
/**
16
 * @covers \Dziki\MonologSentryBundle\SubscribedProcessor\UserDataAppending
17
 */
18
class UserDataAppendingTest extends TestCase
19
{
20
    /**
21
     * @test
22
     */
23
    public function isSubscribedToKernelRequest(): void
24
    {
25
        $subscribedEvents = UserDataAppending::getSubscribedEvents();
26
        $this->assertArrayHasKey(KernelEvents::REQUEST, $subscribedEvents);
27
        $this->assertSame(['onKernelRequest', 7], $subscribedEvents[KernelEvents::REQUEST]);
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function doNothingIfMissingToken(): void
34
    {
35
        /** @var TokenStorageInterface $tokenStorage */
36
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
37
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
38
39
        $userDataAppendingProcessor->onKernelRequest();
40
41
        $record = $userDataAppendingProcessor([]);
42
43
        $this->assertSame([], $record);
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function obtainUsernameIfUserImplementsUserInterface(): void
50
    {
51
        $user = $this->createMock(UserInterface::class);
52
        $user
53
            ->expects($this->once())
54
            ->method('getUsername')
55
            ->willReturn('user')
56
        ;
57
58
        $token = $this->createMock(TokenInterface::class);
59
        $token
60
            ->method('getUser')
61
            ->willReturn($user)
62
        ;
63
64
        /** @var TokenStorageInterface|MockObject $tokenStorage */
65
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
66
        $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...
67
            ->method('getToken')
68
            ->willReturn($token)
69
        ;
70
71
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
72
        $userDataAppendingProcessor->onKernelRequest();
73
    }
74
75
    /**
76
     * @test
77
     */
78 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...
79
    {
80
        $user = 'user';
81
82
        $token = $this->createMock(TokenInterface::class);
83
        $token
84
            ->method('getUser')
85
            ->willReturn($user)
86
        ;
87
88
        /** @var TokenStorageInterface|MockObject $tokenStorage */
89
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
90
        $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...
91
            ->method('getToken')
92
            ->willReturn($token)
93
        ;
94
95
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
96
97
        $userDataAppendingProcessor->onKernelRequest();
98
99
        $record = $userDataAppendingProcessor([]);
100
101
        $this->assertSame(
102
            ['context' => [
103
                'user' => [
104
                    'username' => 'user',
105
                ],
106
            ],
107
            ],
108
            $record
109
        );
110
    }
111
112
    /**
113
     * @test
114
     */
115 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...
116
    {
117
        $user = new class() {
118
            public function __toString()
119
            {
120
                return 'user';
121
            }
122
        };
123
124
        $token = $this->createMock(TokenInterface::class);
125
        $token
126
            ->method('getUser')
127
            ->willReturn($user)
128
        ;
129
130
        /** @var TokenStorageInterface|MockObject $tokenStorage */
131
        $tokenStorage = $this->createMock(TokenStorageInterface::class);
132
        $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...
133
            ->method('getToken')
134
            ->willReturn($token)
135
        ;
136
137
        $userDataAppendingProcessor = new UserDataAppending($tokenStorage);
138
        $userDataAppendingProcessor->onKernelRequest();
139
140
        $record = $userDataAppendingProcessor([]);
141
142
        $this->assertSame(
143
            ['context' => [
144
                'user' => [
145
                    'username' => 'user',
146
                ],
147
            ],
148
            ],
149
            $record
150
        );
151
    }
152
}
153