Completed
Pull Request — master (#9)
by Simon
01:50
created

AccessControlTest::testIsRouteSecure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Sil\RouteSecurityBundle\Tests\Security;
4
5
use PHPUnit\Framework\TestCase;
6
use Sil\RouteSecurityBundle\Exception\LogicException;
7
use Sil\RouteSecurityBundle\Interfaces\NamingStrategyInterface;
8
use Sil\RouteSecurityBundle\Security\AccessControl;
9
use Symfony\Component\Routing\RouteCollection;
10
use Symfony\Component\Routing\RouterInterface;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
13
class AccessControlTest extends TestCase
14
{
15
    public function testHasUserAccessToRoute()
16
    {
17
        $accessControl = $this->createFreshAccessControl();
18
        $user = $this->mockUser();
19
        $this->assertTrue($accessControl->hasUserAccessToRoute($user, 'home_page'));
20
        $this->assertTrue($accessControl->hasUserAccessToRoute($user, 'admin_dashboard'));
21
        $this->assertFalse($accessControl->hasUserAccessToRoute($user, 'admin_home'));
22
    }
23
24 View Code Duplication
    public function testHasUserAccessToRoutes()
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...
25
    {
26
        $accessControl = $this->createFreshAccessControl();
27
        $user = $this->mockUser();
28
        $this->assertTrue($accessControl->hasUserAccessToRoutes($user, ['home_page', 'admin_dashboard']));
29
        $this->assertFalse($accessControl->hasUserAccessToRoutes($user, ['home_page', 'admin_home']));
30
    }
31
32 View Code Duplication
    public function testHasUserAccessAtLeastOneRoute()
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...
33
    {
34
        $accessControl = $this->createFreshAccessControl();
35
        $user = $this->mockUser();
36
        $this->assertTrue($accessControl->hasUserAccessAtLeastOneRoute($user, ['home_page', 'admin_home']));
37
        $this->assertFalse($accessControl->hasUserAccessAtLeastOneRoute($user, ['admin_home', 'admin_profile']));
38
    }
39
40
    public function testIsRouteSecure()
41
    {
42
        $accessControl = $this->createFreshAccessControl();
43
        $this->assertTrue($accessControl->isRouteSecure('admin_home'));
44
        $this->assertFalse($accessControl->isRouteSecure('home_page'));
45
        $this->expectException(LogicException::class);
0 ignored issues
show
Bug introduced by
The method expectException() does not exist on Sil\RouteSecurityBundle\...urity\AccessControlTest. ( Ignorable by Annotation )

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

45
        $this->/** @scrutinizer ignore-call */ expectException(LogicException::class);

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...
46
        $this->assertFalse($accessControl->isRouteSecure('route_not_defined'));
47
    }
48
49
    public function testIsEnable()
50
    {
51
        $router = $this->createMock(RouterInterface::class);
0 ignored issues
show
Bug introduced by
The method createMock() does not exist on Sil\RouteSecurityBundle\...urity\AccessControlTest. ( Ignorable by Annotation )

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

51
        $router = $this->/** @scrutinizer ignore-call */ createMock(RouterInterface::class);

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...
52
        $routeToRoleConverter = $this->createMock(NamingStrategyInterface::class);
53
        $configuration = [
54
            'enable_access_control' => true,
55
            'secured_routes' => [],
56
            'secured_routes_format' => '',
57
            'ignored_routes' => [],
58
            'ignored_routes_format' => '',
59
        ];
60
        $accessControl = new AccessControl($router, $routeToRoleConverter, $configuration);
61
        $this->assertTrue($accessControl->isEnable());
62
        $configuration['enable_access_control'] = false;
63
        $accessControl = new AccessControl($router, $routeToRoleConverter, $configuration);
64
        $this->assertFalse($accessControl->isEnable());
65
    }
66
67
    protected function createFreshAccessControl()
68
    {
69
        $router = $this->createMock(RouterInterface::class);
70
        $routeCollection = $this->createMock(RouteCollection::class);
71
        $routeCollection
72
            ->method('all')
73
            ->willReturn([
74
                'admin_home' => null,
75
                'admin_dashboard' => null,
76
                'admin_profile' => null,
77
                'home_page' => null,
78
                'create_account' => null,
79
                'api_get_info' => null,
80
                'api_set_info' => null
81
            ]);
82
        $router
83
            ->method('getRouteCollection')
84
            ->willReturn($routeCollection);
85
        $routeToRoleConverter = $this->createMock(NamingStrategyInterface::class);
86
        $routeToRoleConverter
87
            ->method('generateRoleForRoute')
88
            ->will($this->returnCallback(function ($route) {
89
                return 'ROLE_'.strtoupper($route);
90
            }));
91
        $configuration = [
92
            'enable_access_control' => true,
93
            'secured_routes' => ['admin_home'],
94
            'secured_routes_format' => '/^admin_/',
95
            'ignored_routes' => ['home_page'],
96
            'ignored_routes_format' => '/^api_/',
97
        ];
98
99
        return new AccessControl($router, $routeToRoleConverter, $configuration);
100
    }
101
102
    protected function mockUser()
103
    {
104
        $user = $this->createMock(UserInterface::class);
105
        $user
106
            ->method('getRoles')
107
            ->willReturn(['ROLE_ADMIN_DASHBOARD']);
108
109
        return $user;
110
    }
111
}
112