-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb434a6
commit 102a2ba
Showing
14 changed files
with
537 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atoolo\Security\Test\Entity; | ||
|
||
use Atoolo\Security\Entity\User; | ||
use Atoolo\Security\Exception\SecurityException; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(User::class)] | ||
class UserTest extends TestCase | ||
{ | ||
public function testGetUserIdentifier(): void | ||
{ | ||
$user = new User('test', []); | ||
$this->assertEquals( | ||
'test', | ||
$user->getUserIdentifier(), | ||
'User::getUserIdentifier should return the username' | ||
); | ||
} | ||
|
||
public function testGetRoles(): void | ||
{ | ||
$user = new User('', ['ROLE_TEST']); | ||
$this->assertEquals( | ||
['ROLE_TEST'], | ||
$user->getRoles(), | ||
'User::getRoles should return the roles' | ||
); | ||
} | ||
|
||
public function testSetAndGetPassword(): void | ||
{ | ||
$user = new User('', []); | ||
$user->setPassword('develop'); | ||
$this->assertEquals( | ||
'develop', | ||
$user->getPassword(), | ||
'User::setPassword should set the password' | ||
); | ||
} | ||
|
||
public function testSetPasswordCallback(): void | ||
{ | ||
$user = new User('', []); | ||
$user->setPasswordCallback(function () { | ||
return 'develop'; | ||
}); | ||
$this->assertEquals( | ||
'develop', | ||
$user->getPassword(), | ||
'getPassword should use the callback to get the password' | ||
); | ||
} | ||
|
||
public function testGetSalt(): void | ||
{ | ||
$user = new User('', []); | ||
$this->assertNull( | ||
$user->getSalt(), | ||
'User::getSalt should return null' | ||
); | ||
} | ||
|
||
public function testEraseCredentials(): void | ||
{ | ||
$user = new User('', []); | ||
$user->setPassword('develop'); | ||
$user->eraseCredentials(); | ||
$this->assertEquals( | ||
$user->getPassword(), | ||
'develop', | ||
'password should be the hashed password and must not be erased' | ||
); | ||
} | ||
|
||
public function testOfArray(): void | ||
{ | ||
$data = [ | ||
'username' => 'test', | ||
'password' => 'test', | ||
'roles' => ['test'] | ||
]; | ||
$user = User::ofArray($data); | ||
$expected = new User('test', ['ROLE_TEST']); | ||
$expected->setPassword('test'); | ||
|
||
$this->assertEquals( | ||
$expected, | ||
$user, | ||
'User::ofArray should return a User object with the given data' | ||
); | ||
} | ||
|
||
public function testOfArrayMissingUsername(): void | ||
{ | ||
$this->expectException(SecurityException::class); | ||
User::ofArray([ | ||
'password' => 'test', | ||
'roles' => ['test'] | ||
]); | ||
} | ||
|
||
public function testOfArrayMissingPassword(): void | ||
{ | ||
$this->expectException(SecurityException::class); | ||
User::ofArray([ | ||
'username' => 'test', | ||
'roles' => ['test'] | ||
]); | ||
} | ||
|
||
public function testOfArrayMissingRoles(): void | ||
{ | ||
$this->expectException(SecurityException::class); | ||
User::ofArray([ | ||
'username' => 'test', | ||
'password' => 'test', | ||
]); | ||
} | ||
} |
Oops, something went wrong.