Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#277)
by Jérémiah
20:41
created

GetUserTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 0
loc 71
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 4 1
A testGetUserNoTokenStorage() 0 6 1
A testGetUserNoToken() 0 9 1
A testGetUser() 0 18 1
A getUserProvider() 0 16 1
A getCompileCode() 0 4 1
1
<?php
2
3
namespace Overblog\GraphQLBundle\Tests\ExpressionLanguage\ExpressionFunction\Security;
4
5
use Overblog\GraphQLBundle\Definition\GlobalVariables;
6
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security\GetUser;
7
use Overblog\GraphQLBundle\Tests\ExpressionLanguage\TestCase;
8
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
12
class GetUserTest extends TestCase
13
{
14
    protected function getFunctions()
15
    {
16
        return [new GetUser()];
17
    }
18
19
    public function testGetUserNoTokenStorage()
20
    {
21
        $globalVariable = new GlobalVariables(['container' => $this->getDIContainerMock()]);
22
        $globalVariable->has('container');
0 ignored issues
show
Unused Code introduced by
The call to the method Overblog\GraphQLBundle\D...\GlobalVariables::has() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
23
        $this->assertNull(eval($this->getCompileCode()));
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
24
    }
25
26
    public function testGetUserNoToken()
27
    {
28
        $tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
29
        $globalVariable = new GlobalVariables(['container' => $this->getDIContainerMock(['security.token_storage' => $tokenStorage])]);
30
        $globalVariable->get('container');
31
32
        $this->getDIContainerMock(['security.token_storage' => $tokenStorage]);
33
        $this->assertNull(eval($this->getCompileCode()));
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
34
    }
35
36
    /**
37
     * @dataProvider getUserProvider
38
     *
39
     * @param $user
40
     * @param $expectedUser
41
     */
42
    public function testGetUser($user, $expectedUser)
43
    {
44
        $tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
45
        $token = $this->getMockBuilder(TokenInterface::class)->getMock();
46
        $globalVariable = new GlobalVariables(['container' => $this->getDIContainerMock(['security.token_storage' => $tokenStorage])]);
47
        $globalVariable->get('container');
48
49
        $token
50
            ->expects($this->once())
51
            ->method('getUser')
52
            ->will($this->returnValue($user));
53
        $tokenStorage
54
            ->expects($this->once())
55
            ->method('getToken')
56
            ->will($this->returnValue($token));
57
58
        $this->assertSame($expectedUser, eval($this->getCompileCode()));
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
59
    }
60
61
    public function getUserProvider()
62
    {
63
        $user = $this->getMockBuilder(UserInterface::class)->getMock();
64
        $std = new \stdClass();
65
        $token = $this->getMockBuilder(TokenInterface::class)->getMock();
66
67
        return [
68
            [$user, $user],
69
            [$std, $std],
70
            [$token, $token],
71
            ['Anon.', null],
72
            [null, null],
73
            [10, null],
74
            [true, null],
75
        ];
76
    }
77
78
    private function getCompileCode()
79
    {
80
        return 'return '.$this->expressionLanguage->compile('getUser()').';';
81
    }
82
}
83