From 6537490f8cd390b9035bf96f37b7a0be97372118 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Thu, 23 May 2024 21:17:20 +0400 Subject: [PATCH] refactor ClientTest.php: replace testGetByEmail with separate tests for getByID, getByEmail, updateUser, and deleteUser, using data providers for getByID and getByEmail tests --- tests/ClientTest.php | 135 +++++++++++++++++++++++++++++++++---------- 1 file changed, 105 insertions(+), 30 deletions(-) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 61865f9a..329c3fbf 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -112,34 +112,109 @@ public function testVerifyPassword(): void self::assertFalse($this->dummy_client->verifyPassword(" abcd")); } -public function testGetByEmail(): void -{ - // Test for valid email - // Save the dummy record to the database - $this->dummy_client->save(); - // Fetch the client by email - $fetched_client = Client::getByEmail($this->dummy_client->getEmail()); - // Assert that the fetched client is not null - self::assertNotNull($fetched_client); - - // Assert the attributes of the fetched client - self::assertEquals("john_u@gmail.com", $fetched_client->getEmail()); - self::assertEquals("john", $fetched_client->getFirstName()); - self::assertEquals("johhny", $fetched_client->getLastName()); - self::assertEquals("13213431", $fetched_client->getPhoneNo()); - self::assertEquals("Royal Road, Curepipe, Moka", $fetched_client->getAddress()->getFormattedAddress()); - - // Delete the dummy record - $fetched_client->deleteUser(); - - // Add a small delay to ensure the deletion operation is completed - usleep(500000); // 500 milliseconds = 0.5 seconds - - // Fetch the client by email again - $fetched_client = Client::getByEmail($this->dummy_client->getEmail()); - - // Test for invalid email - // Assert that the fetched client is null or false - self::assertNull($fetched_client); -} + /** + * @dataProvider getByIDProvider + */ + public static function testGetByID(int $userID, ?string $expectedEmail): void + { + $client = Client::getByID($userID); + if ($expectedEmail !== null) { + self::assertNotNull($client); + self::assertEquals($expectedEmail, $client->getEmail()); + } else { + self::assertNull($client); + } + } + + public static function getByIDProvider(): array + { + return [ + [999, null], // Non-existing user + [-1, null], // Negative ID + ]; + } + + /** + * @dataProvider getByEmailProvider + */ + public static function testGetByEmail(string $email, ?string $expectedEmail): void + { + $client = Client::getByEmail($email); + if ($expectedEmail !== null) { + self::assertNotNull($client); + self::assertEquals($expectedEmail, $client->getEmail()); + } else { + self::assertNull($client); + } + } + + public static function getByEmailProvider(): array + { + return [ + ['john_u@gmail.com', 'john_u@gmail.com'], // Existing email + ['nonexistent@gmail.com', null], // Non-existing email + ['invalidemail', null], // Invalid email format + ]; + } + + /** + * @dataProvider updateUserProvider + */ + public static function testUpdateUser(bool $updatePassword, bool $success): void + { + // Create a client with a known ID + $client = Client::getByEmail('john_u@gmail.com'); + if ($client === null) { + self::fail('Failed to fetch client'); + } + + // Update user and check if successful + $client->setFirstName('UpdatedName'); + $client->setLastName('UpdatedLastName'); + $client->getAddress()->setCity('UpdatedCity'); + + if ($updatePassword) { + $client->setPassword('newPassword'); + } + + $result = $client->updateUser($updatePassword); + self::assertEquals($success, $result); + + // Check if data was actually updated in the database + $updatedClient = Client::getByID($client->getUserID()); + if ($updatedClient === null) { + self::fail('Failed to fetch updated client'); + } + + self::assertEquals('UpdatedName', $updatedClient->getFirstName()); + self::assertEquals('UpdatedLastName', $updatedClient->getLastName()); + self::assertEquals('UpdatedCity', $updatedClient->getAddress()->getCity()); + } + + public static function updateUserProvider(): array + { + return [ + [false, true], // Update without password change + [true, true], // Update with password change + ]; + } + + public function testDeleteUser(): void + { + // Fetch the client by email to get its ID + $client = Client::getByEmail('john_u@gmail.com'); + if ($client === null) { + self::fail('Failed to fetch client'); + } + + // Delete the user + $client->deleteUser(); + + // Attempt to fetch the user again + $deletedClient = Client::getByID($client->getUserID()); + + // Ensure the user does not exist anymore + self::assertNull($deletedClient); + } + }