Passed
Push — master ( fb1eb0...02cee8 )
by Guido
08:09
created

UserServiceTest::testUserAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
use Gvera\Helpers\validation\ValidationService;
4
use Gvera\Models\User;
5
use Gvera\Models\UserRole;
6
use Doctrine\ORM\EntityRepository;
7
use Gvera\Helpers\session\Session;
8
use Gvera\Helpers\entities\GvEntityManager;
9
use PHPUnit\Framework\TestCase;
10
11
class UserServiceTest extends TestCase
12
{
13
14
    private $userService;
15
    /**
16
     * @test
17
     */
18
    private $user;
19
    /**
20
     * @var EntityRepository|\PHPUnit\Framework\MockObject\MockObject
21
     */
22
    private $repo;
23
    /**
24
     * @var GvEntityManager|\PHPUnit\Framework\MockObject\MockObject
25
     */
26
    private $gvEntityManager;
27
28
    public function validateEmail()
29
    {
30
        $invalidEmail = 'test';
31
        $this->assertFalse($this->userService->validateEmail($invalidEmail));
32
        $validEmail = '[email protected]';
33
        $this->assertTrue($this->userService->validateEmail($validEmail));
34
    }
35
36
    public function setUp():void
37
    {
38
        $this->repo = $this->createMock(EntityRepository::class);
39
40
41
        $this->gvEntityManager = $this->createMock(GvEntityManager::class);
42
        $gvEntityManager = $this->gvEntityManager;
43
        $gvEntityManager->expects($this->any())
44
            ->method('getRepository')
45
            ->willReturn($this->repo);
46
47
        $session = $this->createMock(Session::class);
48
        $session->expects($this->any())
49
            ->method('set')
50
            ->with($this->isType('string'), $this->isType('array'))
51
            ->willReturn(true);
52
53
        $this->userService = new \Gvera\Services\UserService($gvEntityManager, $session, new ValidationService());
54
55
        $role = new UserRole();
56
        $role->setName("testRole");
57
        $role->setRolePriority(5);
58
59
        $user = new User();
60
        $user->setEmail("[email protected]");
61
        $user->setUsername("test");
62
        $user->setRole($role);
63
64
        $this->user = $user;
65
        $passHash = $this->userService->generatePassword('test');
66
        $user->setPassword($passHash);
67
68
69
70
        $session->expects($this->any())
71
            ->method('get')
72
            ->with('user')
73
            ->willReturn(['role' => $role->getRolePriority(), 'username' => $user->getUsername()]);
74
75
        parent::setUp(); // TODO: Change the autogenerated stub
76
    }
77
78
    /**
79
     * @test
80
     */
81
    public function generateAndValidatePassword()
82
    {
83
        $passHash = $this->userService->generatePassword('password');
84
        $this->assertNotNull($passHash);
85
86
        $this->assertTrue($this->userService->validatePassword('password', $passHash));
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function login()
93
    {
94
        $this->expectException(Exception::class);
95
96
97
        $user = new User();
98
        $user->setEmail("[email protected]");
99
        $user->setUsername("test");
100
        $passHash = $this->userService->generatePassword('test');
101
        $user->setPassword($passHash);
102
103
104
        $repo = $this->createMock(EntityRepository::class);
105
        $repo->expects($this->any())
106
            ->method('findOneBy')
107
            ->willReturn($user);
108
109
        $gvEntityManager = $this->createMock(GvEntityManager::class);
110
        $gvEntityManager->expects($this->any())
111
            ->method('getRepository')
112
            ->willReturn($repo);
113
114
        $role = new UserRole();
115
        $role->setName("testRole");
116
        $role->setRolePriority(5);
117
118
        $user->setRole($role);
119
120
        $session = $this->createMock(Session::class);
121
        $session->expects($this->any())
122
            ->method('set')
123
            ->with($this->isType('string'), $this->isType('array'))
124
            ->willReturn(true);
125
        
126
        $session->expects($this->any())
127
            ->method('get')
128
            ->with('user')
129
            ->willReturn(['role' => $role->getRolePriority(), 'username' => $user->getUsername()]);
130
            
131
132
        $this->userService->entityManager = $gvEntityManager;
133
        $this->userService->session = $session;
134
135
        $this->userService->login($user->getUsername(), $passHash);
136
        $this->assertTrue($session->get('user')['username'] === $user->getUsername());
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

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...
137
        $this->assertTrue($this->userService->getUserRole() === 5);
138
        $this->userService->login($user->getUsername(), 'failedpass');
139
    }
140
141
    /**
142
     * @test
143
     */
144
    public function logout()
145
    {
146
        $session = $this->createMock(Session::class);
147
        $session->expects($this->any())
148
            ->method('unsetByKey')
149
            ->with('user')
150
            ->willReturn(true);
151
        
152
        $session->expects($this->any())
153
            ->method('get')
154
            ->with('user')
155
            ->willReturn(null);
156
157
        $this->userService->session = $session;
158
159
        $this->userService->logout();
160
161
        $this->assertFalse($this->userService->isUserLoggedIn());
162
    }
163
164
    public function testUserAuthorization()
165
    {
166
167
        $roleAction = new \Gvera\Models\UserRoleAction();
168
        $roleAction->setActionName('test');
169
170
        $role = new UserRole();
171
        $role->setName("testRole");
172
        $role->setRolePriority(5);
173
        $role->addRoleAction($roleAction);
174
175
        $this->gvEntityManager->expects($this->any())->method('getRepository')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Gvera\Helpers\entities\GvEntityManager.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
176
            ->willReturn($this->repo);
177
178
        $this->repo->expects($this->any())->method('findOneBy')
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\ORM\EntityRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
179
            ->willReturn($roleAction);
180
181
        $user = new User();
182
        $user->setEmail("[email protected]");
183
        $user->setUsername("test");
184
        $user->setRole($role);
185
186
187
        $this->assertTrue($this->userService->userCan($user, 'test'));
188
        $this->assertFalse($this->userService->userCan(null, 'asd'));
189
190
    }
191
}