Passed
Push — master ( 7f699f...e24ab0 )
by Kirill
03:06
created

RuleTest::testAllowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Security;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Core\ResolverInterface;
16
use Spiral\Security\ActorInterface;
17
use Spiral\Security\Exception\RuleException;
18
use Spiral\Security\Rule;
19
20
/**
21
 * Class RuleTest
22
 *
23
 * @package Spiral\Tests\Security
24
 */
25
class RuleTest extends TestCase
26
{
27
    public const OPERATION = 'test';
28
    public const CONTEXT = [];
29
30
    /**
31
     * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface
32
     */
33
    private $actor;
34
35
    /**
36
     * @var \PHPUnit_Framework_MockObject_MockObject|ResolverInterface
37
     */
38
    private $resolver;
39
40
    /**
41
     * @var \PHPUnit_Framework_MockObject_MockObject|Rule
42
     */
43
    private $rule;
44
45
    protected function setUp(): void
46
    {
47
        $this->actor = $this->createMock(ActorInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Spiral...\ActorInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...Security\ActorInterface of property $actor.

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...
48
        $this->resolver = $this->createMock(ResolverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Spiral...solverInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...\Core\ResolverInterface of property $resolver.

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...
49
        $this->rule = $this->getMockBuilder(Rule::class)
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Sp...ay('check'))->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...ct&Spiral\Security\Rule of property $rule.

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...
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

49
        $this->rule = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(Rule::class)

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

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

Loading history...
50
            ->setConstructorArgs([$this->resolver])
51
            ->setMethods(['check'])->getMock();
52
    }
53
54
    /**
55
     * @param $permission
56
     * @param $context
57
     * @param $allowed
58
     *
59
     * @dataProvider allowsProvider
60
     */
61
    public function testAllows($permission, $context, $allowed): void
62
    {
63
        $parameters = [
64
                'actor'      => $this->actor,
65
                'user'       => $this->actor,
66
                'permission' => $permission,
67
                'context'    => $context,
68
            ] + $context;
69
70
        $method = new \ReflectionMethod($this->rule, 'check');
71
        $this->resolver
72
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Spiral\Core\ResolverInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            ->/** @scrutinizer ignore-call */ 
73
              expects($this->once())

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...
73
            ->method('resolveArguments')
74
            ->with($method, $parameters)
75
            ->willReturn([$parameters]);
76
77
        $this->rule
78
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Spiral\Security\Rule. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
            ->/** @scrutinizer ignore-call */ 
79
              expects($this->once())

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...
79
            ->method('check')
80
            ->with($parameters)
81
            ->willReturn($allowed);
82
83
        $this->assertEquals($allowed, $this->rule->allows($this->actor, $permission, $context));
84
    }
85
86
    public function testAllowsException(): void
87
    {
88
        $this->expectException(RuleException::class);
89
        $this->rule->allows($this->actor, static::OPERATION, static::CONTEXT);
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function allowsProvider()
96
    {
97
        return [
98
            ['test.create', [], false],
99
            ['test.create', [], true],
100
            ['test.create', ['a' => 'b'], false],
101
            ['test.create', ['a' => 'b'], true],
102
        ];
103
    }
104
}
105