Compare commits

...

5 Commits

Author SHA1 Message Date
Paul LEVRAULT 4af6a741eb Ajout de commentaires
continuous-integration/drone/push Build is failing Details
1 year ago
Paul LEVRAULT 98e7c22cb9 Tests UserManager non fonctionnels
continuous-integration/drone/push Build is failing Details
1 year ago
Paul LEVRAULT afc107ef93 Ajout tests UserRepository
1 year ago
Paul LEVRAULT 3c8594d73c Ajout tests user
1 year ago
Paul LEVRAULT 6359969856 Ajout de tests
continuous-integration/drone/push Build is failing Details
1 year ago

@ -0,0 +1 @@
{"version":1,"defects":{"MaClasseTest::testMethode":5,"toto::testMethode":5,"AuthServiceTest::testLoginWithValidCredentials":5,"AuthServiceTest::testLoginWithInvalidUsername":5,"AuthServiceTest::testLoginWithInvalidPassword":5,"AuthServiceTest::testRegisterNewUser":5,"AuthServiceTest::testRegisterWithExistingUser":4,"AuthServiceTest::testLogoutUserMethodNotImplemented":5},"times":{"MaClasseTest::testMethode":0.03,"toto::testMethode":0.037,"AuthServiceTest::testLoginWithValidCredentials":0.088,"AuthServiceTest::testLoginWithInvalidUsername":0.005,"AuthServiceTest::testLoginWithInvalidPassword":0.004,"AuthServiceTest::testRegisterNewUser":0.084,"AuthServiceTest::testRegisterWithExistingUser":0,"AuthServiceTest::testLogoutUserMethodNotImplemented":0.005}}

@ -28,6 +28,7 @@
},
"scripts": {
"dev": "php -S localhost:8080 -t public -d display_errors=1 -d error_reporting=E_ALL",
"dev:console": "export APP_ENV=console && php public/index.php"
"dev:console": "export APP_ENV=console && php public/index.php",
"test" : "./vendor/bin/phpunit tests"
}
}

@ -3,20 +3,42 @@ namespace Model;
use Model\Role;
// Data Class
class User{
private int $id;
private string $nom;
private string $prenom;
private string $email;
private string $motDePasse;
private string $sexe;
private float $taille;
private float $poids;
private \DateTime $dateNaissance;
private Role $role;
public function __construct(
private int $id,
private string $nom,
private string $prenom,
private string $email,
private string $motDePasse,
private string $sexe,
private float $taille,
private float $poids,
private \DateTime $dateNaissance,
private Role $role,
) {}
int $id,
string $nom,
string $prenom,
string $email,
string $motDePasse,
string $sexe,
float $taille,
float $poids,
\DateTime $dateNaissance,
Role $role
) {
$this->id = $id;
$this->nom = $nom;
$this->prenom = $prenom;
$this->email = $email;
$this->motDePasse = $motDePasse;
$this->sexe = $sexe;
$this->taille = $taille;
$this->poids = $poids;
$this->dateNaissance = $dateNaissance;
$this->role = $role;
}
public function getId(): int {
return $this->id;
}

@ -1,12 +0,0 @@
<?php
use PHPUnit\Framework\TestCase;
class MaClasseTest extends TestCase
{
public function testMethode()
{
echo "TEST MEC";
}
}

@ -0,0 +1,117 @@
<?php
use PHPUnit\Framework\TestCase;
use Manager\UserManager;
use Model\User;
use Model\Coach;
use Model\Athlete;
use Network\IAuthService;
class UserManagerTest extends TestCase
{
private $authService;
private $userManager;
protected function setUp(): void
{
$this->authService = $this->createMock(IAuthService::class);
$this->userManager = new UserManager($this->authService);
}
public function testLoginSuccess()
{
$loginUser = 'john.doe@example.com';
$passwordUser = 'password123';
$mockUser = new User(1, "Doe", "John", $loginUser, $passwordUser, 'M', 1.80, 75, new \DateTime("1985-05-15"), new Coach());
$this->authService->expects($this->once())
->method('login')
->with($loginUser, $passwordUser)
->willReturn($mockUser);
$result = $this->userManager->login($loginUser, $passwordUser);
$this->assertTrue($result);
$this->assertSame($mockUser, $this->userManager->currentUser);
}
public function testLoginFailure()
//Ne fonctionne pas : Error: Typed property Manager\UserManager::$currentUser must not be accessed before initialization
{
$loginUser = 'john.doe@example.com';
$passwordUser = 'wrong_password';
// Configure authService to return null during login
$this->authService->expects($this->once())
->method('login')
->with($loginUser, $passwordUser)
->willReturn(null);
// Ensure currentUser is null before attempting to access it
$this->assertNull($this->userManager->currentUser);
$result = $this->userManager->login($loginUser, $passwordUser);
$this->assertFalse($result);
$this->assertNull($this->userManager->currentUser);
}
public function testRegisterSuccess()
{
$loginUser = 'new.user@example.com';
$passwordUser = 'newpassword123';
$data = [
'nom' => 'New',
'prenom' => 'User',
'taille' => 1.75,
'poids' => 70,
'roleName' => 'Athlete',
'sexe' => 'M',
];
$this->authService->expects($this->once())
->method('register')
->with($loginUser, $passwordUser, $data)
->willReturn(true);
$result = $this->userManager->register($loginUser, $passwordUser, $data);
$this->assertTrue($result);
}
/* Manque les Exception
public function testRegisterValidationException(){
$loginUser = 'invalid.user@example.com';
$passwordUser = 'invalidpassword123';
$data = [
'nom' => 'Invalid',
'prenom' => 'User',
'taille' => 1.75,
'poids' => 70,
'roleName' => 'Athlete',
'sexe' => 'M',
];
$this->expectException(\Exception::class);
$this->userManager->register($loginUser, $passwordUser, $data);
}
*/
/* Manque la fonction de déconnexion
public function testDeconnecterSuccess(){
$this->authService->expects($this->once())
->method('logoutUser');
$result = $this->userManager->deconnecter();
$this->assertTrue($result);
}
public function testDeconnecterFailure(){
$this->authService->expects($this->once())
->method('logoutUser');
$result = $this->userManager->deconnecter();
$this->assertFalse($result);
}
*/
}

@ -0,0 +1,115 @@
<?php
use Model\User;
use Model\Role;
use Model\Coach;
use Model\Athlete;
use PHPUnit\Framework\TestCase;
class UserTest extends TestCase
{
public function testGettersAndSetters()
{
$role = new Coach();
$user = new User(
1,
'John',
'Doe',
'john@example.com',
'hashed_password',
'M',
1.80,
75.0,
new \DateTime('1990-01-01'),
$role,
);
// Test getters
$this->assertSame(1, $user->getId());
$this->assertSame('John', $user->getNom());
$this->assertSame('Doe', $user->getPrenom());
$this->assertSame('john@example.com', $user->getEmail());
$this->assertSame('hashed_password', $user->getMotDePasse());
$this->assertSame('M', $user->getSexe());
$this->assertSame(1.80, $user->getTaille());
$this->assertSame(75.0, $user->getPoids());
$this->assertInstanceOf(\DateTime::class, $user->getDateNaissance());
$this->assertSame($role, $user->getRole());
// Test setters
$user->setId(2);
$this->assertSame(2, $user->getId());
$user->setNom('Jane');
$this->assertSame('Jane', $user->getNom());
$user->setPrenom('Doe');
$this->assertSame('Doe', $user->getPrenom());
$user->setEmail('jane@example.com');
$this->assertSame('jane@example.com', $user->getEmail());
$user->setMotDePasse('new_hashed_password');
$this->assertSame('new_hashed_password', $user->getMotDePasse());
$user->setSexe('F');
$this->assertSame('F', $user->getSexe());
$user->setTaille(1.60);
$this->assertSame(1.60, $user->getTaille());
$user->setPoids(60.0);
$this->assertSame(60.0, $user->getPoids());
$newDateNaissance = new \DateTime('1995-01-01');
$user->setDateNaissance($newDateNaissance);
$this->assertSame($newDateNaissance, $user->getDateNaissance());
$newRole = new Athlete();
$user->setRole($newRole);
$this->assertSame($newRole, $user->getRole());
}
public function testIsValidPassword()
{
$role = new Coach();
$user = new User(
1,
'John',
'Doe',
'john@example.com',
'test123',
'H',
1.80,
75.0,
new \DateTime('1990-01-01'),
$role
);
// Assuming the hashed password is 'hashed_password'
$this->assertTrue($user->isValidPassword('test123'));
$this->assertFalse($user->isValidPassword('test1234'));
}
public function testToString()
{
$role = new Coach();
$user = new User(
1,
'John',
'Doe',
'john@example.com',
'hashed_password',
'M',
1.80,
75.0,
new \DateTime('1990-01-01'),
$role
);
$expectedString = "Athlete [ID: 1, Nom: John, Prénom: Doe, Email: john@example.com]";
$this->assertSame($expectedString, (string)$user);
}
}

@ -0,0 +1,92 @@
<?php
use PHPUnit\Framework\TestCase;
use Stub\UserRepository;
use Model\User;
use Model\Coach;
use Model\Athlete;
class UserRepositoryTest extends TestCase
{
private $userRepository;
protected function setUp(): void
{
$this->userRepository = new UserRepository();
}
public function testGetItemById()
{
$user = $this->userRepository->getItemById(1);
$this->assertInstanceOf(User::class, $user);
$this->assertSame("Doe", $user->getNom());
}
public function testGetItemByEmail()
{
$user = $this->userRepository->getItemByEmail("jane.smith@example.com");
$this->assertInstanceOf(User::class, $user);
$this->assertSame("Smith", $user->getNom());
}
public function testGetNbItems()
{
$count = $this->userRepository->GetNbItems();
$this->assertSame(5, $count);
}
public function testGetItems()
{
$items = $this->userRepository->GetItems(0, 2);
$this->assertCount(2, $items);
$this->assertInstanceOf(User::class, $items[0]);
$this->assertInstanceOf(User::class, $items[1]);
}
public function testGetItemsByName()
{
$items = $this->userRepository->GetItemsByName("John", 0, 2);
$this->assertCount(1, $items);
$this->assertInstanceOf(User::class, $items[0]);
$this->assertSame("John", $items[0]->getPrenom());
}
public function testGetItemByName()
{
$item = $this->userRepository->GetItemByName("Doe", 0, 2);
$this->assertInstanceOf(User::class, $item);
$this->assertSame("Doe", $item->getNom());
}
public function testUpdateItem()
{
$oldUser = $this->userRepository->getItemById(1);
$newUser = new User(1, "UpdatedDoe", "John", "john.doe@example.com", "newpassword", 'M', 1.80, 75, new \DateTime("1985-05-15"), new Coach());
$this->userRepository->UpdateItem($oldUser, $newUser);
$updatedUser = $this->userRepository->getItemById(1);
$this->assertSame("UpdatedDoe", $updatedUser->getNom());
$this->assertSame("newpassword", $updatedUser->getMotDePasse());
}
public function testAddItem()
{
$newUser = new User(6, "NewUser", "Test", "new.user@example.com", "newpassword123", 'F', 1.70, 60, new \DateTime("1990-01-01"), new Athlete());
$this->userRepository->AddItem($newUser);
$addedUser = $this->userRepository->getItemById(6);
$this->assertInstanceOf(User::class, $addedUser);
$this->assertSame("NewUser", $addedUser->getNom());
}
public function testDeleteItem()
{
$userToDelete = $this->userRepository->getItemById(1);
$result = $this->userRepository->DeleteItem($userToDelete);
$this->assertTrue($result);
$this->assertNull($this->userRepository->getItemById(1));
}
}

@ -0,0 +1,93 @@
<?php
use PHPUnit\Framework\TestCase;
use Stub\AuthService;
use Model\Coach;
use Model\Athlete;
use Model\User;
use Shared\Exception\NotImplementedException;
use Network\IAuthService;
use Shared\HashPassword;
use Stub\UserRepository;
class AuthServiceTest extends TestCase{
public function testLoginWithValidCredentials() {
// Arrange
$userRepository = new UserRepository();
$passwordHasher = new HashPassword();
$authService = new AuthService($userRepository, $passwordHasher);
// Create a user for testing
$user = new User(6, "Truc", "Test", "truc.test@example.com", "test123", "M", 1.75, 80, new \DateTime("2000-01-01"), new Athlete);
$userRepository->addItem($user);
// Act
$result = $authService->login($user->getNom(), "test123");
// Assert
$this->assertInstanceOf(User::class, $result);
// Add more assertions if needed
}
public function testLoginWithInvalidUsername() {
// Arrange
$userRepository = new UserRepository();
$passwordHasher = new HashPassword();
$authService = new AuthService($userRepository, $passwordHasher);
// Act and Assert
$this->expectException(\Exception::class);
$authService->login("Machin", "password123");
}
public function testLoginWithInvalidPassword() {
// Arrange
$userRepository = new UserRepository();
$passwordHasher = new HashPassword();
$authService = new AuthService($userRepository, $passwordHasher);
// Create a user for testing
$user = new User(6, "Truc", "Test", "truc.test@example.com", "test123", "M", 1.75, 80, new \DateTime("2000-01-01"), new Athlete);
$userRepository->addItem($user);
// Act and Assert
$this->assertNull($authService->login($user->getNom(), "machin"));
}
public function testRegisterNewUser() {
// Arrange
$userRepository = new UserRepository();
$passwordHasher = new HashPassword();
$authService = new AuthService($userRepository, $passwordHasher);
// Act
$result = $authService->register("truc.test@example.com", "test123", array("prenom"=>"Truc", "nom"=>"Test", "email"=>"truc.test@example.com", "sexe"=>"M","poids"=>1.75, "taille"=>80,"dateNaissance"=>new \DateTime("2000-01-01"), "roleName"=>"Athlete"));
// Assert
$this->assertTrue($result);
// Add more assertions if needed
}
/*
public function testRegisterWithExistingUser() {
// Arrange
$userRepository = new UserRepository();
$passwordHasher = new HashPassword();
$authService = new AuthService($userRepository, $passwordHasher);
// Create an existing user for testing
$existingUser = new User(6, "Truc", "Test", "truc.test@example.com", "test123", "M", 1.75, 80, new \DateTime("2000-01-01"), new Athlete);
$userRepository->addItem($existingUser);
// Act and Assert
$this->expectException(\Exception::class);
$authService->register($existingUser->getNom(), "test123", array("prenom"=>"Truc", "nom"=>"Test", "email"=>"truc.test@example.com", "sexe"=>"M","poids"=>1.75, "taille"=>80,"dateNaissance"=>new \DateTime("2000-01-01"), "roleName"=>"Athlete"));
}
*/
public function testLogoutUserMethodNotImplemented() {
// Arrange
$userRepository = new UserRepository();
$passwordHasher = new HashPassword();
$authService = new AuthService($userRepository, $passwordHasher);
// Act
$this->expectOutputString('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! logout method not implemented !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
$authService->logoutUser();
}
}

@ -3,7 +3,7 @@
'name' => 'hearttrack/package',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'd4345678992503b9eb56ef4afd00ff13f5d7531a',
'reference' => '4498de7839f27be4ce639451b5d8fae4cdfd7f1a',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
@ -31,7 +31,7 @@
'hearttrack/package' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'd4345678992503b9eb56ef4afd00ff13f5d7531a',
'reference' => '4498de7839f27be4ce639451b5d8fae4cdfd7f1a',
'type' => 'library',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),

Loading…
Cancel
Save