| 1 | <?php |
| 2 | |
| 3 | namespace Liuch\DmarcSrg; |
| 4 | |
| 5 | use Liuch\DmarcSrg\Users\User; |
| 6 | use Liuch\DmarcSrg\Users\DbUser; |
| 7 | use Liuch\DmarcSrg\Exception\SoftException; |
| 8 | use Liuch\DmarcSrg\Exception\LogicException; |
| 9 | use Liuch\DmarcSrg\Exception\DatabaseNotFoundException; |
| 10 | use Liuch\DmarcSrg\Database\DatabaseController; |
| 11 | use Liuch\DmarcSrg\Database\UserMapperInterface; |
| 12 | |
| 13 | class DbUserTest extends \PHPUnit\Framework\TestCase |
| 14 | { |
| 15 | public function testIfExist(): void |
| 16 | { |
| 17 | $user = new DbUser('username', $this->getDatabaseMapperOnce('exists', '', true)); |
| 18 | $this->assertTrue($user->exists()); |
| 19 | $this->assertTrue($user->exists()); |
| 20 | $user = new DbUser('username', $this->getDatabaseMapperOnce('exists', '', false)); |
| 21 | $this->assertFalse($user->exists()); |
| 22 | $this->assertFalse($user->exists()); |
| 23 | } |
| 24 | |
| 25 | public function testZeroId(): void |
| 26 | { |
| 27 | $this->expectException(LogicException::class); |
| 28 | new DbUser([ 'id' => 0 ], $this->getDatabaseNever()); |
| 29 | } |
| 30 | |
| 31 | public function testNegativeId(): void |
| 32 | { |
| 33 | $this->expectException(LogicException::class); |
| 34 | new DbUser([ 'id' => -1 ], $this->getDatabaseNever()); |
| 35 | } |
| 36 | |
| 37 | public function testIdFromData(): void |
| 38 | { |
| 39 | $this->assertSame(1, (new DbUser([ 'id' => 1 ], $this->getDatabaseNever()))->id()); |
| 40 | |
| 41 | $this->expectException(SoftException::class); |
| 42 | $this->expectExceptionMessage('User not found'); |
| 43 | $user = new DbUser('user1', $this->getDatabaseMapperNotFound('fetch')); |
| 44 | $user->id(); |
| 45 | } |
| 46 | |
| 47 | public function testIdFromDatabase(): void |
| 48 | { |
| 49 | $this->assertSame(2, (new DbUser('some', $this->getDatabaseMapperOnce('fetch', 'id', 2)))->id()); |
| 50 | |
| 51 | $user = new DbUser([ 'name' => 'some' ], $this->getDatabaseMapperOnce('fetch', 'id', 3)); |
| 52 | $this->assertSame(3, $user->id()); |
| 53 | $this->assertSame(3, $user->id()); |
| 54 | } |
| 55 | |
| 56 | public function testEmptyName1(): void |
| 57 | { |
| 58 | $this->expectException(SoftException::class); |
| 59 | new DbUser('', $this->getDatabaseNever()); |
| 60 | } |
| 61 | |
| 62 | public function testEmptyName2(): void |
| 63 | { |
| 64 | $this->expectException(SoftException::class); |
| 65 | new DbUser([ 'name' => '', 'id' => 33 ], $this->getDatabaseNever()); |
| 66 | } |
| 67 | |
| 68 | public function testReservedName(): void |
| 69 | { |
| 70 | $this->expectException(SoftException::class); |
| 71 | new DbUser('admin', $this->getDatabaseNever()); |
| 72 | } |
| 73 | |
| 74 | public function testName(): void |
| 75 | { |
| 76 | $this->assertSame('user1', (new DbUser('user1', $this->getDatabaseNever()))->name()); |
| 77 | $this->assertSame('user2', (new DbUser([ 'name' => 'user2' ], $this->getDatabaseNever()))->name()); |
| 78 | $user = new DbUser([ 'id' => 88 ], $this->getDatabaseMapperOnce('fetch', 'name', 'user3')); |
| 79 | $this->assertSame('user3', $user->name()); |
| 80 | $this->assertSame('user3', $user->name()); |
| 81 | |
| 82 | $this->expectException(SoftException::class); |
| 83 | $this->expectExceptionMessage('User not found'); |
| 84 | $user = new DbUser([ 'id' => 7 ], $this->getDatabaseMapperNotFound('fetch')); |
| 85 | $user->name(); |
| 86 | } |
| 87 | |
| 88 | public function testLevel(): void |
| 89 | { |
| 90 | $this->assertSame( |
| 91 | User::LEVEL_USER, |
| 92 | (new DbUser([ 'name' => 'user1', 'level' => User::LEVEL_USER ], $this->getDatabaseNever()))->level() |
| 93 | ); |
| 94 | $user = new DbUser('user2', $this->getDatabaseMapperOnce('fetch', 'level', User::LEVEL_MANAGER)); |
| 95 | $this->assertSame(User::LEVEL_MANAGER, $user->level()); |
| 96 | $this->assertSame(User::LEVEL_MANAGER, $user->level()); |
| 97 | |
| 98 | $this->expectException(SoftException::class); |
| 99 | $this->expectExceptionMessage('User not found'); |
| 100 | $user = new DbUser('user3', $this->getDatabaseMapperNotFound('fetch')); |
| 101 | $user->level(); |
| 102 | } |
| 103 | |
| 104 | public function testIsEnabled(): void |
| 105 | { |
| 106 | $this->assertTrue( |
| 107 | (new DbUser([ 'name' => 'user1', 'enabled' => true ], $this->getDatabaseNever()))->isEnabled() |
| 108 | ); |
| 109 | $this->assertFalse( |
| 110 | (new DbUser([ 'name' => 'user2', 'enabled' => false ], $this->getDatabaseNever()))->isEnabled() |
| 111 | ); |
| 112 | $user = new DbUser('user3', $this->getDatabaseMapperOnce('fetch', 'enabled', false)); |
| 113 | $this->assertFalse($user->isEnabled()); |
| 114 | $this->assertFalse($user->isEnabled()); |
| 115 | |
| 116 | $this->expectException(SoftException::class); |
| 117 | $this->expectExceptionMessage('User not found'); |
| 118 | $user = new DbUser('user4', $this->getDatabaseMapperNotFound('fetch')); |
| 119 | $user->isEnabled(); |
| 120 | } |
| 121 | |
| 122 | public function testToArray(): void |
| 123 | { |
| 124 | $data = [ |
| 125 | 'id' => 33, |
| 126 | 'name' => 'user1', |
| 127 | 'enabled' => true, |
| 128 | 'password' => true, |
| 129 | 'level' => User::LEVEL_USER, |
| 130 | 'email' => '[email protected]', |
| 131 | 'key' => 'zxd', |
| 132 | 'domains' => null, |
| 133 | 'created_time' => new \DateTime(), |
| 134 | 'updated_time' => (new \DateTime())->modify('+1 day') |
| 135 | ]; |
| 136 | $user = new DbUser($data, $this->getDatabaseNever()); |
| 137 | unset($data['id']); |
| 138 | $data['password'] = null; |
| 139 | $data['level'] = $data['level']; |
| 140 | $this->assertSame($data, $user->toArray()); |
| 141 | |
| 142 | $this->expectException(SoftException::class); |
| 143 | $this->expectExceptionMessage('User not found'); |
| 144 | $user = new DbUser('user2', $this->getDatabaseMapperNotFound('fetch')); |
| 145 | $user->toArray(); |
| 146 | } |
| 147 | |
| 148 | public function testSave(): void |
| 149 | { |
| 150 | $user = new DbUser([ 'name' => 'user1', 'id' => 1 ], $this->getDatabaseMapperOnce('save', '', true)); |
| 151 | $user->save(); |
| 152 | $this->assertTrue($user->exists()); |
| 153 | } |
| 154 | |
| 155 | public function testDelete(): void |
| 156 | { |
| 157 | $mp = $this->getMockBuilder(UserMapperInterface::class) |
| 158 | ->disableOriginalConstructor() |
| 159 | ->getMock(); |
| 160 | $mp->expects($this->once())->method('fetch'); |
| 161 | $mp->expects($this->once())->method('delete'); |
| 162 | $db = $this->getDatabase(); |
| 163 | $db->expects($this->any()) |
| 164 | ->method('getMapper') |
| 165 | ->with('user') |
| 166 | ->willReturn($mp); |
| 167 | $user = new DbUser('user1', $db); |
| 168 | $user->delete(); |
| 169 | $this->assertFalse($user->exists()); |
| 170 | |
| 171 | $mp = $this->getMockBuilder(UserMapperInterface::class) |
| 172 | ->disableOriginalConstructor() |
| 173 | ->getMock(); |
| 174 | $mp->expects($this->never())->method('fetch'); |
| 175 | $mp->expects($this->once())->method('delete'); |
| 176 | $db = $this->getDatabase(); |
| 177 | $db->expects($this->once()) |
| 178 | ->method('getMapper') |
| 179 | ->with('user') |
| 180 | ->willReturn($mp); |
| 181 | $user = new DbUser([ 'name' => 'user2', 'id' => 2 ], $db); |
| 182 | $user->delete(); |
| 183 | $this->assertFalse($user->exists()); |
| 184 | |
| 185 | $this->expectException(SoftException::class); |
| 186 | $this->expectExceptionMessage('User not found'); |
| 187 | $user = new DbUser('user2', $this->getDatabaseMapperNotFound('fetch')); |
| 188 | $user->delete(); |
| 189 | } |
| 190 | |
| 191 | public function testEmptyPassword(): void |
| 192 | { |
| 193 | $this->assertFalse((new DbUser('user1', $this->getDatabaseNever()))->verifyPassword('')); |
| 194 | } |
| 195 | |
| 196 | public function testIncorrectPassword(): void |
| 197 | { |
| 198 | $user = new DbUser( |
| 199 | 'user1', |
| 200 | $this->getDatabaseMapperOnce('getPasswordHash', '', password_hash('password1', PASSWORD_DEFAULT)) |
| 201 | ); |
| 202 | $this->assertFalse($user->verifyPassword('fake')); |
| 203 | |
| 204 | $user = new DbUser( |
| 205 | 'user1', |
| 206 | $this->getDatabaseMapperOnce('getPasswordHash', '', password_hash('', PASSWORD_DEFAULT)) |
| 207 | ); |
| 208 | $this->assertFalse($user->verifyPassword('fake')); |
| 209 | } |
| 210 | |
| 211 | public function testCorrectPassword(): void |
| 212 | { |
| 213 | $user = new DbUser( |
| 214 | 'user1', |
| 215 | $this->getDatabaseMapperOnce('getPasswordHash', '', password_hash('password1', PASSWORD_DEFAULT)) |
| 216 | ); |
| 217 | $this->assertTrue($user->verifyPassword('password1')); |
| 218 | } |
| 219 | |
| 220 | public function testPasswordForNonexistentUser(): void |
| 221 | { |
| 222 | $this->assertFalse( |
| 223 | (new DbUser('user1', $this->getDatabaseMapperNotFound('getPasswordHash')))->verifyPassword('some') |
| 224 | ); |
| 225 | } |
| 226 | |
| 227 | public function testSetPassword(): void |
| 228 | { |
| 229 | $mp = $this->getMockBuilder(UserMapperInterface::class) |
| 230 | ->disableOriginalConstructor() |
| 231 | ->getMock(); |
| 232 | $mp->expects($this->once()) |
| 233 | ->method('savePasswordHash') |
| 234 | ->willReturnCallback(function (&$data, $hash) { |
| 235 | $this->assertTrue(password_verify('password1', $hash)); |
| 236 | }); |
| 237 | $db = $this->getDatabase(); |
| 238 | $db->expects($this->once()) |
| 239 | ->method('getMapper') |
| 240 | ->with('user') |
| 241 | ->willReturn($mp); |
| 242 | (new DbUser('user1', $db))->setPassword('password1'); |
| 243 | } |
| 244 | |
| 245 | private function getDatabase(): object |
| 246 | { |
| 247 | return $this->getMockBuilder(DatabaseController::class) |
| 248 | ->disableOriginalConstructor() |
| 249 | ->onlyMethods([ 'getMapper' ]) |
| 250 | ->getMock(); |
| 251 | } |
| 252 | |
| 253 | private function getDatabaseNever(): object |
| 254 | { |
| 255 | $db = $this->getDatabase(); |
| 256 | $db->expects($this->never())->method('getMapper'); |
| 257 | return $db; |
| 258 | } |
| 259 | |
| 260 | private function getDatabaseMapperOnce(string $method, string $key, $value): object |
| 261 | { |
| 262 | $mapper = $this->getMockBuilder(UserMapperInterface::class) |
| 263 | ->disableOriginalConstructor() |
| 264 | ->getMock(); |
| 265 | $mapper->expects($this->once()) |
| 266 | ->method($method) |
| 267 | ->willReturnCallback(function (&$data) use ($key, $value) { |
| 268 | if (!empty($key)) { |
| 269 | $data[$key] = $value; |
| 270 | } |
| 271 | return $value; |
| 272 | }); |
| 273 | |
| 274 | $db = $this->getDatabase(); |
| 275 | $db->expects($this->once()) |
| 276 | ->method('getMapper') |
| 277 | ->with('user') |
| 278 | ->willReturn($mapper); |
| 279 | return $db; |
| 280 | } |
| 281 | |
| 282 | private function getDatabaseMapperNotFound($method): object |
| 283 | { |
| 284 | $mapper = $this->getMockBuilder(UserMapperInterface::class) |
| 285 | ->disableOriginalConstructor() |
| 286 | ->getMock(); |
| 287 | $mapper->expects($this->once()) |
| 288 | ->method($method) |
| 289 | ->willThrowException(new DatabaseNotFoundException('Not found')); |
| 290 | |
| 291 | $db = $this->getDatabase(); |
| 292 | $db->expects($this->any()) |
| 293 | ->method('getMapper') |
| 294 | ->with('user') |
| 295 | ->willReturn($mapper); |
| 296 | return $db; |
| 297 | } |
| 298 | } |