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

CompositeRuleTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Rules;
13
14
use PHPUnit\Framework\MockObject\Stub\ConsecutiveCalls;
15
use PHPUnit\Framework\TestCase;
16
use Spiral\Security\ActorInterface;
17
use Spiral\Security\RuleInterface;
18
use Spiral\Security\RulesInterface;
19
use Spiral\Tests\Security\Rules\Fixtures\AllCompositeRule;
20
use Spiral\Tests\Security\Rules\Fixtures\OneCompositeRule;
21
22
class CompositeRuleTest extends TestCase
23
{
24
    public const OPERATION = 'test';
25
    public const CONTEXT = [];
26
27
    /**
28
     * @var \PHPUnit_Framework_MockObject_MockObject|ActorInterface $callable
29
     */
30
    private $actor;
31
32
    public function setUp(): void
33
    {
34
        $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...
35
    }
36
37
    /**
38
     * @param $expected
39
     * @param $compositeRuleClass
40
     * @param $rules
41
     *
42
     * @dataProvider allowsProvider
43
     */
44
    public function testAllow($expected, $compositeRuleClass, $rules): void
45
    {
46
        $repository = $this->createRepository($rules);
47
48
        /** @var RuleInterface $rule */
49
        $rule = new $compositeRuleClass($repository);
50
        $this->assertEquals(
51
            $expected,
52
            $rule->allows($this->actor, static::OPERATION, static::CONTEXT)
53
        );
54
    }
55
56
    public function allowsProvider()
57
    {
58
        $allowRule = $this->allowRule();
59
        $forbidRule = $this->forbidRule();
60
61
        return [
62
            [true, AllCompositeRule::class, [$allowRule, $allowRule, $allowRule]],
63
            [false, AllCompositeRule::class, [$allowRule, $allowRule, $forbidRule]],
64
            [true, OneCompositeRule::class, [$allowRule, $forbidRule, $forbidRule]],
65
            [true, OneCompositeRule::class, [$allowRule, $allowRule, $allowRule]],
66
            [false, OneCompositeRule::class, [$forbidRule, $forbidRule, $forbidRule]],
67
        ];
68
    }
69
70
    /**
71
     * @param array $rules
72
     *
73
     * @return RulesInterface
74
     */
75
    private function createRepository(array $rules): RulesInterface
76
    {
77
        /** @var \PHPUnit_Framework_MockObject_MockObject|RulesInterface $repository */
78
        $repository = $this->createMock(RulesInterface::class);
79
80
        $repository->method('get')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Spiral\Security\RulesInterface. ( Ignorable by Annotation )

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

80
        $repository->/** @scrutinizer ignore-call */ 
81
                     method('get')

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...
81
            ->will(new ConsecutiveCalls($rules));
82
83
        return $repository;
84
    }
85
86
    /**
87
     * @return RuleInterface
88
     */
89
    private function allowRule(): RuleInterface
90
    {
91
        /** @var \PHPUnit_Framework_MockObject_MockObject|RuleInterface $rule */
92
        $rule = $this->createMock(RuleInterface::class);
93
        $rule->method('allows')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Spiral\Security\RuleInterface. ( Ignorable by Annotation )

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

93
        $rule->/** @scrutinizer ignore-call */ 
94
               method('allows')->willReturn(true);

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...
94
95
        return $rule;
96
    }
97
98
    /**
99
     * @return RuleInterface
100
     */
101
    private function forbidRule(): RuleInterface
102
    {
103
        /** @var \PHPUnit_Framework_MockObject_MockObject|RuleInterface $rule */
104
        $rule = $this->createMock(RuleInterface::class);
105
        $rule->method('allows')->willReturn(false);
106
107
        return $rule;
108
    }
109
}
110