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

GuardedTraitTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 81
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGuardScopeException2() 0 5 1
A testResolvePermission() 0 8 1
A testGuardScopeException() 0 8 1
A setUp() 0 5 1
A testGetGuardFromContainer() 0 7 1
A testAllows() 0 15 1
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\Traits;
13
14
use PHPUnit\Framework\TestCase;
15
use Psr\Container\ContainerInterface;
16
use Spiral\Core\Container;
17
use Spiral\Core\ContainerScope;
18
use Spiral\Core\Exception\ScopeException;
19
use Spiral\Security\GuardInterface;
20
use Spiral\Tests\Security\Traits\Fixtures\Guarded;
21
use Spiral\Tests\Security\Traits\Fixtures\GuardedWithNamespace;
22
use Spiral\Security\Traits\GuardedTrait;
23
24
class GuardedTraitTest extends TestCase
25
{
26
    public const OPERATION = 'test';
27
    public const CONTEXT   = [];
28
29
    /**
30
     * @var \PHPUnit_Framework_MockObject_MockObject|GuardedTrait
31
     */
32
    private $trait;
33
34
    /**
35
     * @var \PHPUnit_Framework_MockObject_MockObject|GuardInterface
36
     */
37
    private $guard;
38
39
    /**
40
     * @var \PHPUnit_Framework_MockObject_MockObject|ContainerInterface
41
     */
42
    private $container;
43
44
    public function setUp(): void
45
    {
46
        $this->trait = $this->getMockForTrait(GuardedTrait::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMockForTrait(S...ts\GuardedTrait::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...ity\Traits\GuardedTrait of property $trait.

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

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->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Co...tainerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type PHPUnit_Framework_MockOb...iner\ContainerInterface of property $container.

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
    }
50
51
    public function testGetGuardFromContainer(): void
52
    {
53
        $this->container->method('has')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Psr\Container\ContainerInterface. ( Ignorable by Annotation )

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

53
        $this->container->/** @scrutinizer ignore-call */ 
54
                          method('has')->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...
54
        $this->container->method('get')->will($this->returnValue($this->guard));
55
56
        ContainerScope::runScope($this->container, function (): void {
57
            $this->assertEquals($this->guard, $this->trait->getGuard());
58
        });
59
    }
60
61
    public function testGuardScopeException(): void
62
    {
63
        $this->expectException(ScopeException::class);
64
65
        $this->container->method('has')->willReturn(false);
66
67
        ContainerScope::runScope($this->container, function (): void {
68
            $this->assertEquals($this->guard, $this->trait->getGuard());
69
        });
70
    }
71
72
    public function testGuardScopeException2(): void
73
    {
74
        $this->expectException(ScopeException::class);
75
76
        $this->assertEquals($this->guard, $this->trait->getGuard());
77
    }
78
79
    public function testAllows(): void
80
    {
81
        $this->guard->method('allows')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Spiral\Security\GuardInterface. ( Ignorable by Annotation )

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

81
        $this->guard->/** @scrutinizer ignore-call */ 
82
                      method('allows')

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...
82
            ->with(static::OPERATION, static::CONTEXT)
83
            ->will($this->returnValue(true))
84
        ;
85
86
        $guarded = new Guarded();
87
88
        $container = new Container();
89
        $container->bind(GuardInterface::class, $this->guard);
90
91
        ContainerScope::runScope($container, function () use ($guarded): void {
92
            $this->assertTrue($guarded->allows(static::OPERATION, static::CONTEXT));
93
            $this->assertFalse($guarded->denies(static::OPERATION, static::CONTEXT));
94
        });
95
    }
96
97
    public function testResolvePermission(): void
98
    {
99
        $guarded = new Guarded();
100
        $this->assertEquals(static::OPERATION, $guarded->resolvePermission(static::OPERATION));
101
102
        $guarded = new GuardedWithNamespace();
103
        $resolvedPermission = GuardedWithNamespace::GUARD_NAMESPACE . '.' . static::OPERATION;
104
        $this->assertEquals($resolvedPermission, $guarded->resolvePermission(static::OPERATION));
105
    }
106
}
107