Completed
Push — master ( fd313c...979590 )
by John
11:35
created

willPassTokenToAccessDecisionManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Tests\Security;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Description;
12
use KleijnWeb\PhpApi\Descriptions\Description\Operation;
13
use KleijnWeb\PhpApi\Descriptions\Description\Repository;
14
use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta;
15
use KleijnWeb\SwaggerBundle\Security\RbacRequestVoter;
16
use KleijnWeb\SwaggerBundle\Security\RequestAuthorizationListener;
17
use PHPUnit\Framework\TestCase;
18
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
21
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
22
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
23
24
/**
25
 * @author John Kleijn <[email protected]>
26
 */
27
class RbacRequestVoterTestTest extends TestCase
28
{
29
    /**
30
     * @var AccessDecisionManagerInterface
31
     */
32
    private $accessDecisionManager;
33
34
    /**
35
     * @var  \PHPUnit_Framework_MockObject_MockObject
36
     */
37
    private $repositoryMock;
38
39
    /**
40
     * @var RbacRequestVoter
41
     */
42
    private $voter;
43
44
    protected function setUp()
45
    {
46
        $this->accessDecisionManager = $this->getMockForAbstractClass(AccessDecisionManagerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForAbstrac...anagerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...cisionManagerInterface> of property $accessDecisionManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
48
        /** @var Repository $repository */
49
        $this->repositoryMock = $repository = $this
0 ignored issues
show
Unused Code introduced by
$repository is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
            ->getMockBuilder(Repository::class)
51
            ->disableOriginalConstructor()
52
            ->getMock();
53
54
        $this->voter = new RbacRequestVoter($this->repositoryMock, $this->accessDecisionManager);
0 ignored issues
show
Documentation introduced by
$this->repositoryMock is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<KleijnWeb\PhpApi\...Description\Repository>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$this->accessDecisionManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...cisionManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
    }
56
57
    /**
58
     * @test
59
     */
60
    public function legacySupportsClassMethodReturnsFalse()
61
    {
62
        $this->assertFalse($this->voter->supportsClass('Foo'));
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function willAbstainWhenNotPassedRequest()
69
    {
70
        /** @var TokenInterface $token */
71
        $token = $this->getMockForAbstractClass(TokenInterface::class);
72
73
        $this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($token, new \stdClass(), []));
74
    }
75
76
    /**
77
     * @test
78
     */
79 View Code Duplication
    public function willAbstainWhenRequestHasNoSwaggerPath()
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...
80
    {
81
        /** @var TokenInterface $token */
82
        $token = $this->getMockForAbstractClass(TokenInterface::class);
83
84
        $this->assertEquals(
85
            VoterInterface::ACCESS_ABSTAIN,
86
            $this->voter->vote($token, $this->createRequest([]), [RequestAuthorizationListener::ATTRIBUTE])
87
        );
88
    }
89
90
    /**
91
     * @test
92
     */
93 View Code Duplication
    public function willAbstainWhenAttributesNotFromListener()
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...
94
    {
95
        /** @var TokenInterface $token */
96
        $token = $this->getMockForAbstractClass(TokenInterface::class);
97
98
        $this->assertEquals(
99
            VoterInterface::ACCESS_ABSTAIN,
100
            $this->voter->vote($token, $this->createRequest([]), ['something else'])
101
        );
102
    }
103
104
    /**
105
     * @test
106
     */
107
    public function willNotAbstainOneAttributesFromListener()
108
    {
109
        /** @var TokenInterface $token */
110
        $token = $this->getMockForAbstractClass(TokenInterface::class);
111
112
        $this->repositoryMock->expects($this->once())->method('get');
113
114
        $this->voter->vote(
115
            $token,
116
            $this->createRequest([
117
                RequestMeta::ATTRIBUTE_URI  => '/',
118
                RequestMeta::ATTRIBUTE_PATH => '/',
119
            ]),
120
            ['something else', RequestAuthorizationListener::ATTRIBUTE]
121
        );
122
    }
123
124
    /**
125
     * @test
126
     */
127
    public function willRequireIsAuthenticatedFullyWhenOperationSecured()
128
    {
129
        /** @var TokenInterface $token */
130
        $token = $this->getMockForAbstractClass(TokenInterface::class);
131
132
        /** @var Operation $operation */
133
        $operationMock = $operation = $this->getMockBuilder(Operation::class)->disableOriginalConstructor()->getMock();
134
135
        /** @var Description $description */
136
        $description = $this->getMockBuilder(Description::class)->disableOriginalConstructor()->getMock();
137
138
        $request = $this->createRequest([RequestMeta::ATTRIBUTE => new RequestMeta($description, $operation)]);
0 ignored issues
show
Documentation introduced by
$operation is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<KleijnWeb\PhpApi\...\Description\Operation>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
140
        $this->assertEquals(
141
            VoterInterface::ACCESS_DENIED,
142
            $this->voter->vote($token, $request, [RequestAuthorizationListener::ATTRIBUTE])
143
        );
144
145
        $operationMock->expects($this->once())->method('isSecured')->willReturn(true);
146
147
        /** @var \PHPUnit_Framework_MockObject_MockObject $mock */
148
        $mock = $this->accessDecisionManager;
149
        $mock
150
            ->expects($this->once())
151
            ->method('decide')
152
            ->with($token, ['IS_AUTHENTICATED_FULLY'])
153
            ->willReturn(true);
154
155
        $this->assertEquals(
156
            VoterInterface::ACCESS_GRANTED,
157
            $this->voter->vote($token, $request, [RequestAuthorizationListener::ATTRIBUTE])
158
        );
159
    }
160
161
    /**
162
     * @param array  $attributes
163
     *
164
     * @param string $content
0 ignored issues
show
Bug introduced by
There is no parameter named $content. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
165
     *
166
     * @return Request
167
     */
168
    private function createRequest(array $attributes): Request
169
    {
170
        return new class($attributes) extends Request
171
        {
172
            /**
173
             * @param array $attributes
174
             * @param array $content
0 ignored issues
show
Bug introduced by
There is no parameter named $content. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
175
             */
176
            public function __construct(array $attributes)
177
            {
178
                parent::__construct();
179
                $this->attributes = new ParameterBag($attributes);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Symfony\Component\D...rameterBag($attributes) of type object<Symfony\Component...ameterBag\ParameterBag> is incompatible with the declared type object<Symfony\Component...oundation\ParameterBag> of property $attributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
180
            }
181
        };
182
    }
183
}
184