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

RuleManagerTest::testGetWithUndefinedRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
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 Psr\Container\ContainerInterface;
16
use Spiral\Security\Exception\RuleException;
17
use Spiral\Security\RuleInterface;
18
use Spiral\Security\RuleManager;
19
use Spiral\Security\Rule\CallableRule;
20
21
/**
22
 * Class RuleManagerTest
23
 *
24
 * @package Spiral\Tests\Security
25
 */
26
class RuleManagerTest extends TestCase
27
{
28
    public const RULE_NAME = 'test';
29
30
    /**
31
     * @var ContainerInterface
32
     */
33
    private $container;
34
35
    /**
36
     * @var RuleInterface
37
     */
38
    private $rule;
39
40
    public function setUp(): void
41
    {
42
        $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 Psr\Container\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...
43
        $this->rule = $this->createMock(RuleInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Spiral...y\RuleInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Spiral\Security\RuleInterface 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...
44
    }
45
46
    public function testFlow(): void
47
    {
48
        $ruleClass = get_class($this->rule);
49
50
        $this->container->expects($this->once())->method('get')
0 ignored issues
show
Bug introduced by
The method expects() 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

50
        $this->container->/** @scrutinizer ignore-call */ 
51
                          expects($this->once())->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...
51
            ->with($ruleClass)->willReturn($this->rule);
52
53
        $manager = new RuleManager($this->container);
54
55
        $this->assertEquals($manager, $manager->set(self::RULE_NAME, $ruleClass));
56
        $this->assertTrue($manager->has(self::RULE_NAME));
57
        $this->assertEquals($this->rule, $manager->get(self::RULE_NAME));
58
        $this->assertEquals($manager, $manager->remove(self::RULE_NAME));
59
60
        // other rule types
61
        $manager->set('RuleInterface', $this->rule);
62
        $this->assertEquals($this->rule, $manager->get('RuleInterface'));
63
        $manager->set('Closure', function () {
64
            return true;
65
        });
66
        $this->assertTrue($manager->get('Closure') instanceof CallableRule);
67
        $manager->set('Array', [$this, 'testFlow']);
68
        $this->assertTrue($manager->get('Array') instanceof CallableRule);
69
    }
70
71
    public function testHasWithNotRegisteredClass(): void
72
    {
73
        $ruleClass = get_class($this->rule);
74
        $manager = new RuleManager($this->container);
75
76
        $this->assertTrue($manager->has($ruleClass));
77
    }
78
79
    public function testSetRuleException(): void
80
    {
81
        $manager = new RuleManager($this->container);
82
83
        $this->expectException(RuleException::class);
84
        $manager->set(self::RULE_NAME);
85
    }
86
87
    public function testRemoveException(): void
88
    {
89
        $this->container->method('has')->with(self::RULE_NAME)->willReturn(false);
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

89
        $this->container->/** @scrutinizer ignore-call */ 
90
                          method('has')->with(self::RULE_NAME)->willReturn(false);

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...
90
91
        $manager = new RuleManager($this->container);
92
93
        $this->expectException(RuleException::class);
94
        $manager->remove(self::RULE_NAME);
95
    }
96
97
    public function testGetWithUndefinedRule(): void
98
    {
99
        $this->container->method('has')->with(self::RULE_NAME)->willReturn(false);
100
101
        $manager = new RuleManager($this->container);
102
103
        $this->expectException(RuleException::class);
104
        $manager->get(static::RULE_NAME);
105
    }
106
107
    public function testGetWithSomethingOtherThanRule(): void
108
    {
109
        $ruleClass = \stdClass::class;
110
        $this->container->method('has')->with(self::RULE_NAME)->willReturn(true);
111
112
        $manager = new RuleManager($this->container);
113
114
        $this->expectException(RuleException::class);
115
        $manager->get($ruleClass);
116
    }
117
}
118