Test Setup Failed
Push — master ( a987f1...e79f61 )
by krzysztof
02:41
created

UserProcessorTest::testUserAsObject()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
rs 8.8571
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace Skowronline\UserProcessorBundle\Tests\Processor;
4
5
use Skowronline\UserProcessorBundle\Processor\UserProcessor;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\User\UserInterface;
9
10
/**
11
 * @author Krzysztof Skaradziński <[email protected]>
12
 */
13
class UserProcessorTest extends \PHPUnit_Framework_TestCase
14
{
15
    public function testProcessorWithoutToken()
16
    {
17
        $processor = new UserProcessor($this->mockTokenStorage());
0 ignored issues
show
Bug introduced by
It seems like $this->mockTokenStorage() targeting Skowronline\UserProcesso...est::mockTokenStorage() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Skowronline\UserProcesso...rocessor::__construct() does only seem to accept object<Symfony\Component...\TokenStorageInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
18
        $this->assertSame([], $processor([]));
19
20
        $record = ['data' => '2015-06-01 12:00:00'];
21
        $this->assertSame($record, $processor($record));
22
    }
23
24
    public function testProcessorWithoutUser()
25
    {
26
        $token = $this->mockTokenInterface();
27
        $storage = $this->mockTokenStorage($token);
28
29
        $processor = new UserProcessor($storage);
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->mockTokenStorage($token) on line 27 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Skowronline\UserProcesso...rocessor::__construct() does only seem to accept object<Symfony\Component...\TokenStorageInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30
        $this->assertSame([], $processor([]));
31
32
        $record = ['date' => '2015-06-01 12:00:00'];
33
        $this->assertSame($record, $processor($record));
34
    }
35
36
    public function testUserAsString()
37
    {
38
        $token = $this->mockTokenInterface('skowron-line');
39
        $storage = $this->mockTokenStorage($token);
40
41
        $processor = new UserProcessor($storage);
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->mockTokenStorage($token) on line 39 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Skowronline\UserProcesso...rocessor::__construct() does only seem to accept object<Symfony\Component...\TokenStorageInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
        $this->assertSame(
43
            [
44
                'extra' => ['user' => 'skowron-line']
45
            ],
46
            $processor([])
47
        );
48
49
        $record = [
50
            'date' => '2015-06-01 12:00:00',
51
            'extra' => [
52
                'branch' => 'master'
53
            ]
54
        ];
55
56
        $this->assertSame(
57
            [
58
                'date' => '2015-06-01 12:00:00',
59
                'extra' => [
60
                    'branch' => 'master',
61
                    'user'   => 'skowron-line'
62
                ]
63
            ],
64
            $processor($record)
65
        );
66
    }
67
68
    public function testUserAsObject()
69
    {
70
        /** @var UserInterface||PHPUnit_Framework_MockObject_MockObject $user */
0 ignored issues
show
Documentation introduced by
The doc-type UserInterface||PHPUnit_F...k_MockObject_MockObject could not be parsed: Unknown type name "|" at position 14. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
71
        $user = $this
72
            ->getMockBuilder(UserInterface::class)
73
            ->disableOriginalConstructor()
74
            ->getMock();
75
76
        $user
77
            ->method('getUserName')
78
            ->willReturn('skowron-line');
79
80
        $token = $this->mockTokenInterface($user);
81
        $storage = $this->mockTokenStorage($token);
82
83
        $processor = new UserProcessor($storage);
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->mockTokenStorage($token) on line 81 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Skowronline\UserProcesso...rocessor::__construct() does only seem to accept object<Symfony\Component...\TokenStorageInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
        $this->assertSame(
85
            [
86
                'extra' => ['user' => 'skowron-line']
87
            ],
88
            $processor([])
89
        );
90
91
        $record = [
92
            'date' => '2015-06-01 12:00:00',
93
            'extra' => [
94
                'branch' => 'master'
95
            ]
96
        ];
97
98
        $this->assertSame(
99
            [
100
                'date' => '2015-06-01 12:00:00',
101
                'extra' => [
102
                    'branch' => 'master',
103
                    'user'   => 'skowron-line'
104
                ]
105
            ],
106
            $processor($record)
107
        );
108
    }
109
110
    /***
111
     * @param null $returnValue
112
     *
113
     * @return \PHPUnit_Framework_MockObject_MockObject|TokenStorageInterface
114
     */
115
    private function mockTokenStorage($returnValue = null)
116
    {
117
        /** @var TokenStorageInterface|\PHPUnit_Framework_MockObject_MockObject $tokenStorage */
118
        $tokenStorage = $this
119
            ->getMockBuilder(TokenStorageInterface::class)
120
            ->disableOriginalConstructor()
121
            ->getMock();
122
123
        $tokenStorage
124
            ->method('getToken')
125
            ->willReturn($returnValue);
126
127
        return $tokenStorage;
128
    }
129
130
    /**
131
     * @param null $returnValue
132
     *
133
     * @return \PHPUnit_Framework_MockObject_MockObject|TokenInterface
134
     */
135
    private function mockTokenInterface($returnValue = null)
136
    {
137
        /** @var TokenInterface|\PHPUnit_Framework_MockObject_MockObject $token */
138
        $token = $this
139
            ->getMockBuilder(TokenInterface::class)
140
            ->disableOriginalConstructor()
141
            ->getMock();
142
143
        $token
144
            ->method('getUser')
145
            ->willReturn($returnValue);
146
147
        return $token;
148
    }
149
}
150
151