-
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.
refactor ClientTest.php: replace testGetByEmail with separate tests f…
…or getByID, getByEmail, updateUser, and deleteUser, using data providers for getByID and getByEmail tests
- Loading branch information
1 parent
1859e6a
commit 6537490
Showing
1 changed file
with
105 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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("[email protected]", $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 [ | ||
['[email protected]', '[email protected]'], // Existing email | ||
['[email protected]', 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('[email protected]'); | ||
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('[email protected]'); | ||
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); | ||
} | ||
|
||
} |