Test Failed
Push — master ( 692977...048823 )
by Elijah
02:28
created

UserTest::testTrueHasRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 18
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Eloyekunle\PermissionsBundle\Tests\Model;
13
14
use Eloyekunle\PermissionsBundle\Model\RoleInterface;
15
use Eloyekunle\PermissionsBundle\Model\UserInterface;
16
use Eloyekunle\PermissionsBundle\Tests\TestRole;
17
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
class UserTest extends TestCase
20
{
21
    public function testTrueHasRole()
22
    {
23
        $user = $this->getUser();
24
        $newrole = $this->createMockRole('ROLE_X');
25
26
        $user->addRole($newrole);
27
        $this->assertTrue($user->hasRole($newrole));
28
29
        // Role already exists (added above), so addRole should return without doing anything
30
        $user->addRole($newrole);
31
        $this->assertTrue($user->hasRole($newrole));
32
33
        $user->removeRole($newrole);
34
        $this->assertFalse($user->hasRole($newrole));
35
36
        // Role already removed (removed above), so removeRole should return without doing anything
37
        $user->removeRole($newrole);
38
        $this->assertFalse($user->hasRole($newrole));
39
    }
40
41
    public function testFalseHasRole()
42
    {
43
        $user = $this->getUser();
44
        $newrole = $this->createMockRole('ROLE_X');
45
46
        $this->assertFalse($user->hasRole($newrole));
47
        $user->addRole($newrole);
48
        $this->assertTrue($user->hasRole($newrole));
49
    }
50
51
    public function testIsSuperAdmin()
52
    {
53
        $user = $this->getUser();
54
        $permission = 'do stuff';
55
56
        $this->assertFalse($user->isSuperAdmin());
57
        $this->assertFalse($user->hasPermission($permission));
58
59
        $superAdminRole = $this->createMockRole(RoleInterface::ROLE_SUPER_ADMIN);
60
        $user->addRole($superAdminRole);
61
62
        $this->assertTrue($user->isSuperAdmin());
63
        $this->assertTrue($user->hasPermission($permission));
64
    }
65
66
    public function testSetUserRoles()
67
    {
68
        $roleNames = ['Content Admin', 'Service Admin'];
69
        $roles = [];
70
        foreach ($roleNames as $roleName) {
71
            $roles[] = $this->createMockRole($roleName);
72
        }
73
        $user1 = $this->getUser();
74
        $user2 = $this->getUser();
75
        $user2->setUserRoles($roles);
76
77
        foreach ($roles as $role) {
78
            $this->assertFalse($user1->hasRole($role));
79
            $this->assertTrue($user2->hasRole($role));
80
        }
81
    }
82
83
    /**
84
     * @param $roleName
85
     *
86
     * @return RoleInterface
87
     */
88
    protected function createMockRole($roleName)
89
    {
90
        return new TestRole($roleName);
91
    }
92
93
    /**
94
     * @return UserInterface
95
     *
96
     * @throws \ReflectionException
97
     */
98
    protected function getUser()
99
    {
100
        return $this->getMockForAbstractClass('Eloyekunle\PermissionsBundle\Model\User');
101
    }
102
}
103