-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathUserServiceTest.php
More file actions
142 lines (119 loc) · 4.56 KB
/
Copy pathUserServiceTest.php
File metadata and controls
142 lines (119 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Tests\PHPCensor\Service;
use PHPCensor\Common\Application\ConfigurationInterface;
use PHPCensor\DatabaseManager;
use PHPCensor\Model\User;
use PHPCensor\Service\UserService;
use PHPCensor\Store\UserStore;
use PHPCensor\StoreRegistry;
use PHPUnit\Framework\TestCase;
/**
* Unit tests for the ProjectService class.
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class UserServiceTest extends TestCase
{
private UserService $testedService;
private UserStore $userStore;
private ConfigurationInterface $configuration;
private DatabaseManager $databaseManager;
private StoreRegistry $storeRegistry;
protected function setUp(): void
{
$this->configuration = $this->getMockBuilder(ConfigurationInterface::class)->getMock();
$this->databaseManager = $this
->getMockBuilder(DatabaseManager::class)
->setConstructorArgs([$this->configuration])
->getMock();
$this->storeRegistry = $this
->getMockBuilder(StoreRegistry::class)
->setConstructorArgs([$this->databaseManager])
->getMock();
$this->userStore = $this
->getMockBuilder(UserStore::class)
->setConstructorArgs([$this->databaseManager, $this->storeRegistry])
->getMock();
$this->userStore
->expects($this->any())
->method('save')
->will($this->returnArgument(0));
$this->testedService = new UserService($this->storeRegistry, $this->userStore);
}
public function testExecute_CreateNonAdminUser(): void
{
$user = $this->testedService->createUser(
'Test',
'test@example.com',
'internal',
['type' => 'internal'],
'testing',
false
);
self::assertEquals('Test', $user->getName());
self::assertEquals('test@example.com', $user->getEmail());
self::assertEquals(false, $user->getIsAdmin());
self::assertTrue(\password_verify('testing', $user->getHash()));
}
public function testExecute_CreateAdminUser(): void
{
$user = $this->testedService->createUser(
'Test',
'test@example.com',
'internal',
['type' => 'internal'],
'testing',
true
);
self::assertEquals(true, $user->getIsAdmin());
}
public function testExecute_RevokeAdminStatus(): void
{
$user = new User($this->storeRegistry);
$user->setEmail('test@example.com');
$user->setName('Test');
$user->setIsAdmin(true);
$user = $this->testedService->updateUser($user, 'Test', 'test@example.com', 'testing', false);
self::assertEquals(false, $user->getIsAdmin());
}
public function testExecute_GrantAdminStatus(): void
{
$user = new User($this->storeRegistry);
$user->setEmail('test@example.com');
$user->setName('Test');
$user->setIsAdmin(false);
$user = $this->testedService->updateUser($user, 'Test', 'test@example.com', 'testing', true);
self::assertEquals(true, $user->getIsAdmin());
}
public function testExecute_ChangesPasswordIfNotEmpty(): void
{
$user = new User($this->storeRegistry);
$user->setHash(\password_hash('testing', PASSWORD_DEFAULT));
$user = $this->testedService->updateUser($user, 'Test', 'test@example.com', 'newpassword', false);
self::assertFalse(\password_verify('testing', $user->getHash()));
self::assertTrue(\password_verify('newpassword', $user->getHash()));
}
public function testExecute_DoesNotChangePasswordIfEmpty(): void
{
$user = new User($this->storeRegistry);
$user->setHash(\password_hash('testing', PASSWORD_DEFAULT));
$user = $this->testedService->updateUser($user, 'Test', 'test@example.com', '', false);
self::assertTrue(\password_verify('testing', $user->getHash()));
}
public function testExecuteDeleteUser(): void
{
$store = $this
->getMockBuilder(UserStore::class)
->setConstructorArgs([$this->databaseManager, $this->storeRegistry])
->getMock();
$store->expects($this->once())
->method('delete')
->will($this->returnValue(true));
$service = new UserService($this->storeRegistry, $store);
$user = new User($this->storeRegistry);
self::assertEquals(false, $service->deleteUser($user));
$user->setId(11);
self::assertEquals(true, $service->deleteUser($user));
}
}