Test Failed
Push — master ( 4e470d...d26f6d )
by Elijah
02:23
created

UserTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testFalseHasRole() 0 8 1
A testTrueHasRole() 0 18 1
A testIsSuperAdmin() 0 13 1
A createMockRole() 0 3 1
A getUser() 0 3 1
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
    /**
67
     * @param $roleName
68
     *
69
     * @return RoleInterface
70
     */
71
    protected function createMockRole($roleName)
72
    {
73
        return new TestRole($roleName);
74
    }
75
76
    /**
77
     * @return UserInterface
78
     *
79
     * @throws \ReflectionException
80
     */
81
    protected function getUser()
82
    {
83
        return $this->getMockForAbstractClass('Eloyekunle\PermissionsBundle\Model\User');
84
    }
85
}
86