120 lines
3.9 KiB
PHP
120 lines
3.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Tests\Entity;
|
||
|
|
|
||
|
|
use App\Entity\Account;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use Symfony\Component\Validator\Validation;
|
||
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||
|
|
|
||
|
|
class AccountTest extends TestCase
|
||
|
|
{
|
||
|
|
private ValidatorInterface $validator;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
// Corrected line: Use enableAttributeMapping() for PHP attributes
|
||
|
|
$this->validator = Validation::createValidatorBuilder()
|
||
|
|
->enableAttributeMapping() // Use this for PHP attributes
|
||
|
|
->getValidator();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Helper to create a valid Account instance.
|
||
|
|
*/
|
||
|
|
private function createValidAccount(): Account
|
||
|
|
{
|
||
|
|
return (new Account())
|
||
|
|
->setUsername('testuser')
|
||
|
|
->setEmail('test@example.com')
|
||
|
|
->setPassword('securepassword')
|
||
|
|
->setUuid('1b9d67fe-1b0d-40e9-a417-36e6e2978051');
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- Unit Tests for Getters and Setters ---
|
||
|
|
|
||
|
|
public function testGetId(): void
|
||
|
|
{
|
||
|
|
$account = new Account();
|
||
|
|
// ID is typically set by the ORM, so we can't directly test a generated value here.
|
||
|
|
// We'll rely on functional tests with the database for this.
|
||
|
|
$this->assertNull($account->getId());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testSetAndGetUsername(): void
|
||
|
|
{
|
||
|
|
$account = new Account();
|
||
|
|
$account->setUsername('newusername');
|
||
|
|
$this->assertSame('newusername', $account->getUsername());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testSetAndGetEmail(): void
|
||
|
|
{
|
||
|
|
$account = new Account();
|
||
|
|
$account->setEmail('newemail@example.com');
|
||
|
|
$this->assertSame('newemail@example.com', $account->getEmail());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testSetAndGetPassword(): void
|
||
|
|
{
|
||
|
|
$account = new Account();
|
||
|
|
$account->setPassword('hashedpassword');
|
||
|
|
$this->assertSame('hashedpassword', $account->getPassword());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testSetAndGetUuid(): void
|
||
|
|
{
|
||
|
|
$account = new Account();
|
||
|
|
$uuid = 'a1b2c3d4-e5f6-7890-1234-567890abcdef';
|
||
|
|
$account->setUuid($uuid);
|
||
|
|
$this->assertSame($uuid, $account->getUuid());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testSetAndGetRoles(): void
|
||
|
|
{
|
||
|
|
$account = new Account();
|
||
|
|
$account->setRoles(['ROLE_ADMIN', 'ROLE_USER']);
|
||
|
|
$this->assertContains('ROLE_ADMIN', $account->getRoles());
|
||
|
|
$this->assertContains('ROLE_USER', $account->getRoles());
|
||
|
|
$this->assertCount(2, $account->getRoles()); // Because ROLE_USER is guaranteed
|
||
|
|
}
|
||
|
|
|
||
|
|
// --- UserInterface and PasswordAuthenticatedUserInterface Tests ---
|
||
|
|
|
||
|
|
public function testGetUserIdentifier(): void
|
||
|
|
{
|
||
|
|
$account = $this->createValidAccount();
|
||
|
|
$this->assertSame('testuser', $account->getUserIdentifier());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testGetRolesAlwaysIncludesRoleUser(): void
|
||
|
|
{
|
||
|
|
$account = new Account();
|
||
|
|
$this->assertContains('ROLE_USER', $account->getRoles());
|
||
|
|
|
||
|
|
$account->setRoles(['ROLE_ADMIN']);
|
||
|
|
$this->assertContains('ROLE_ADMIN', $account->getRoles());
|
||
|
|
$this->assertContains('ROLE_USER', $account->getRoles());
|
||
|
|
$this->assertCount(2, $account->getRoles());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testEraseCredentials(): void
|
||
|
|
{
|
||
|
|
$account = $this->createValidAccount();
|
||
|
|
// eraseCredentials is deprecated and should not modify password directly in modern Symfony
|
||
|
|
// It's usually for clearing sensitive data from memory after security operations.
|
||
|
|
$account->eraseCredentials();
|
||
|
|
// Assert that password remains, as it's not actually cleared by this method (deprecated behavior)
|
||
|
|
$this->assertNotNull($account->getPassword());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testSerializeRemovesSensitiveData(): void
|
||
|
|
{
|
||
|
|
$account = $this->createValidAccount();
|
||
|
|
$serializedAccount = serialize($account);
|
||
|
|
$this->assertStringContainsString(hash('crc32c', 'securepassword'), $serializedAccount);
|
||
|
|
$this->assertStringNotContainsString('securepassword', $serializedAccount);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|