From 6359969856f25b89914b2276330861afb9027e41 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 21 Nov 2023 14:20:38 +0100 Subject: [PATCH] Ajout de tests --- Sources/.phpunit.cache/test-results | 1 + Sources/composer.json | 3 +- Sources/src/data/model/User.php | 46 ++++++--- Sources/tests/Tests.php | 12 --- .../tests/units/service/AuthServiceTest.php | 93 +++++++++++++++++++ Sources/vendor/composer/installed.php | 4 +- 6 files changed, 132 insertions(+), 27 deletions(-) create mode 100644 Sources/.phpunit.cache/test-results delete mode 100644 Sources/tests/Tests.php create mode 100644 Sources/tests/units/service/AuthServiceTest.php diff --git a/Sources/.phpunit.cache/test-results b/Sources/.phpunit.cache/test-results new file mode 100644 index 00000000..af32c1c8 --- /dev/null +++ b/Sources/.phpunit.cache/test-results @@ -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}} \ No newline at end of file diff --git a/Sources/composer.json b/Sources/composer.json index 7373dd27..e7efe496 100644 --- a/Sources/composer.json +++ b/Sources/composer.json @@ -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" } } \ No newline at end of file diff --git a/Sources/src/data/model/User.php b/Sources/src/data/model/User.php index 3937d77b..eee018d9 100644 --- a/Sources/src/data/model/User.php +++ b/Sources/src/data/model/User.php @@ -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; } diff --git a/Sources/tests/Tests.php b/Sources/tests/Tests.php deleted file mode 100644 index 86245680..00000000 --- a/Sources/tests/Tests.php +++ /dev/null @@ -1,12 +0,0 @@ -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(); + } +} \ No newline at end of file diff --git a/Sources/vendor/composer/installed.php b/Sources/vendor/composer/installed.php index 770e13da..2cbc9d73 100644 --- a/Sources/vendor/composer/installed.php +++ b/Sources/vendor/composer/installed.php @@ -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(),