Completed
Push — master ( c114e4...0fa665 )
by Eric
9s
created

createAuthorizationCheckerMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Bundle\ResourceBundle\Tests\Security;
13
14
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface;
15
use Lug\Bundle\ResourceBundle\Security\SecurityChecker;
16
use Lug\Bundle\ResourceBundle\Security\SecurityCheckerInterface;
17
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class SecurityCheckerTest extends \PHPUnit_Framework_TestCase
23
{
24
    /**
25
     * @var SecurityChecker
26
     */
27
    private $securityChecker;
28
29
    /**
30
     * @var \PHPUnit_Framework_MockObject_MockObject|AuthorizationCheckerInterface
31
     */
32
    private $authorizationChecker;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
36
     */
37
    private $parameterResolver;
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function setUp()
43
    {
44
        $this->authorizationChecker = $this->createAuthorizationCheckerMock();
45
        $this->parameterResolver = $this->createParameterResolverMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createParameterResolverMock() can also be of type object<Lug\Bundle\Resour...meterResolverInterface>. However, the property $parameterResolver is declared as type object<PHPUnit_Framework_MockObject_MockObject>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46
47
        $this->securityChecker = new SecurityChecker($this->authorizationChecker, $this->parameterResolver);
48
    }
49
50
    public function testInheritance()
51
    {
52
        $this->assertInstanceOf(SecurityCheckerInterface::class, $this->securityChecker);
53
    }
54
55
    public function testIsGrantedWithoutVoter()
56
    {
57
        $this->parameterResolver
58
            ->expects($this->once())
59
            ->method('resolveVoter')
60
            ->will($this->returnValue(false));
61
62
        $this->authorizationChecker
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Securi...izationCheckerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
63
            ->expects($this->never())
64
            ->method('isGranted');
65
66
        $this->assertTrue($this->securityChecker->isGranted('show', $this->createStdClassMock()));
67
    }
68
69 View Code Duplication
    public function testIsGrantedWithVoter()
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...
70
    {
71
        $this->parameterResolver
72
            ->expects($this->once())
73
            ->method('resolveVoter')
74
            ->will($this->returnValue(true));
75
76
        $this->authorizationChecker
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Symfony\Component\Securi...izationCheckerInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
77
            ->expects($this->once())
78
            ->method('isGranted')
79
            ->with(
80
                $this->identicalTo('lug.'.($action = 'show')),
81
                $this->identicalTo($object = $this->createStdClassMock())
82
            )
83
            ->will($this->returnValue(true));
84
85
        $this->assertTrue($this->securityChecker->isGranted($action, $object));
86
    }
87
88
    /**
89
     * @return \PHPUnit_Framework_MockObject_MockObject|AuthorizationCheckerInterface
90
     */
91
    private function createAuthorizationCheckerMock()
92
    {
93
        return $this->getMock(AuthorizationCheckerInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
    }
95
96
    /**
97
     * @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface
98
     */
99
    private function createParameterResolverMock()
100
    {
101
        return $this->getMock(ParameterResolverInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
102
    }
103
104
    /**
105
     * @return \PHPUnit_Framework_MockObject_MockObject|\stdClass
106
     */
107
    private function createStdClassMock()
108
    {
109
        return $this->getMock(\stdClass::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::getMock() has been deprecated with message: Method deprecated since Release 5.4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
110
    }
111
}
112