-
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.
Merge pull request #212 from Divyesh000/newtestclient
add new client tests
- Loading branch information
Showing
1 changed file
with
195 additions
and
108 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 |
---|---|---|
|
@@ -17,129 +17,216 @@ public function setUp(): void | |
{ | ||
$address = new Location("Royal Road", "Curepipe", 1); | ||
$this->dummy_client = new Client( | ||
"[email protected]", "john", "johhny", "abcd", | ||
"13213431", $address); | ||
"[email protected]", | ||
"john", | ||
"johhny", | ||
"abcd", | ||
"13213431", | ||
$address | ||
); | ||
|
||
$success = $this->dummy_client->save(); | ||
if (!$success) { | ||
throw new Exception('Unable to save client'); | ||
} | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->dummy_client = null; | ||
public function tearDown(): void | ||
{ | ||
$this->dummy_client = null; | ||
|
||
// Clear all data from client and user tables | ||
self::query('DELETE FROM client; DELETE FROM user;'); | ||
} | ||
} | ||
|
||
public function testConstructor(): void | ||
{ | ||
// check if fields were correctly set | ||
self::assertEquals("[email protected]", $this->dummy_client->getEmail()); | ||
self::assertEquals("john", $this->dummy_client->getFirstName()); | ||
self::assertEquals("johhny", $this->dummy_client->getLastName()); | ||
self::assertEquals("13213431", $this->dummy_client->getPhoneNo()); | ||
self::assertEquals("Royal Road, Curepipe, Moka", $this->dummy_client->getAddress()->getFormattedAddress()); | ||
} | ||
public function testConstructor(): void | ||
{ | ||
// check if fields were correctly set | ||
self::assertEquals("[email protected]", $this->dummy_client->getEmail()); | ||
self::assertEquals("john", $this->dummy_client->getFirstName()); | ||
self::assertEquals("johhny", $this->dummy_client->getLastName()); | ||
self::assertEquals("13213431", $this->dummy_client->getPhoneNo()); | ||
self::assertEquals("Royal Road, Curepipe, Moka", $this->dummy_client->getAddress()->getFormattedAddress()); | ||
} | ||
|
||
public function testToArray(): void | ||
{ | ||
$result = $this->dummy_client->toArray(); | ||
|
||
// check if all required keys are present | ||
$this->assertArrayHasKey('user_id', $result); | ||
$this->assertArrayHasKey('email', $result); | ||
$this->assertArrayHasKey('first_name', $result); | ||
$this->assertArrayHasKey('last_name', $result); | ||
$this->assertArrayHasKey('phone_no', $result); | ||
$this->assertArrayHasKey('district_id', $result); | ||
$this->assertArrayHasKey('street', $result); | ||
$this->assertArrayHasKey('city', $result); | ||
$this->assertArrayHasKey('password', $result); | ||
|
||
// check if actual values are correct | ||
self::assertEquals("[email protected]", $result['email']); | ||
self::assertEquals("john", $result['first_name']); | ||
self::assertEquals("johhny", $result['last_name']); | ||
self::assertEquals("13213431", $result['phone_no']); | ||
self::assertEquals("Royal Road", $result['street']); | ||
self::assertEquals("Curepipe", $result['city']); | ||
self::assertEquals(1, $result['district_id']); | ||
} | ||
public function testToArray(): void | ||
{ | ||
$result = $this->dummy_client->toArray(); | ||
|
||
// check if all required keys are present | ||
$this->assertArrayHasKey('user_id', $result); | ||
$this->assertArrayHasKey('email', $result); | ||
$this->assertArrayHasKey('first_name', $result); | ||
$this->assertArrayHasKey('last_name', $result); | ||
$this->assertArrayHasKey('phone_no', $result); | ||
$this->assertArrayHasKey('district_id', $result); | ||
$this->assertArrayHasKey('street', $result); | ||
$this->assertArrayHasKey('city', $result); | ||
$this->assertArrayHasKey('password', $result); | ||
|
||
// check if actual values are correct | ||
self::assertEquals("[email protected]", $result['email']); | ||
self::assertEquals("john", $result['first_name']); | ||
self::assertEquals("johhny", $result['last_name']); | ||
self::assertEquals("13213431", $result['phone_no']); | ||
self::assertEquals("Royal Road", $result['street']); | ||
self::assertEquals("Curepipe", $result['city']); | ||
self::assertEquals(1, $result['district_id']); | ||
} | ||
|
||
public function testValidate(): void | ||
{ | ||
$client = new Client( | ||
"", "", "", "abcd", | ||
"", new Location(), // pass an empty Location object for testing | ||
); | ||
|
||
// Test if existence checks work | ||
self::assertEquals([ | ||
'email' => 'Invalid email format', | ||
'first_name' => 'First name must be at least 3 characters long', | ||
'last_name' => 'Last name must be at least 3 characters long', | ||
'phone_no' => 'Phone number must be at least 7 characters long', | ||
'district' => 'District does not exist' | ||
], $client->validate()); | ||
|
||
// Test for range checks | ||
$client = new Client( | ||
"[email protected]", "Jo", "Doe", "1234567", | ||
"123456", new Location(), // pass an empty Location object for testing | ||
); | ||
|
||
self::assertEquals([ | ||
'first_name' => 'First name must be at least 3 characters long', | ||
'phone_no' => 'Phone number must be at least 7 characters long', | ||
'district' => 'District does not exist' | ||
], $client->validate()); | ||
} | ||
public function testValidate(): void | ||
{ | ||
$client = new Client( | ||
"", | ||
"", | ||
"", | ||
"abcd", | ||
"", | ||
new Location(), // pass an empty Location object for testing | ||
); | ||
|
||
// Test if existence checks work | ||
self::assertEquals([ | ||
'email' => 'Invalid email format', | ||
'first_name' => 'First name must be at least 3 characters long', | ||
'last_name' => 'Last name must be at least 3 characters long', | ||
'phone_no' => 'Phone number must be at least 7 characters long', | ||
'district' => 'District does not exist' | ||
], $client->validate()); | ||
|
||
// Test for range checks | ||
$client = new Client( | ||
"[email protected]", | ||
"Jo", | ||
"Doe", | ||
"1234567", | ||
"123456", | ||
new Location(), // pass an empty Location object for testing | ||
); | ||
|
||
self::assertEquals([ | ||
'first_name' => 'First name must be at least 3 characters long', | ||
'phone_no' => 'Phone number must be at least 7 characters long', | ||
'district' => 'District does not exist' | ||
], $client->validate()); | ||
} | ||
|
||
public function testVerifyPassword(): void | ||
{ | ||
// verify true password | ||
self::assertTrue($this->dummy_client->verifyPassword("abcd")); | ||
public function testVerifyPassword(): void | ||
{ | ||
// verify true password | ||
self::assertTrue($this->dummy_client->verifyPassword("abcd")); | ||
|
||
// reject empty string | ||
self::assertFalse($this->dummy_client->verifyPassword("")); | ||
// reject empty string | ||
self::assertFalse($this->dummy_client->verifyPassword("")); | ||
|
||
// reject any other string | ||
self::assertFalse($this->dummy_client->verifyPassword("abcde")); | ||
self::assertFalse($this->dummy_client->verifyPassword("abcd ")); | ||
self::assertFalse($this->dummy_client->verifyPassword(" abcd")); | ||
} | ||
// reject any other string | ||
self::assertFalse($this->dummy_client->verifyPassword("abcde")); | ||
self::assertFalse($this->dummy_client->verifyPassword("abcd ")); | ||
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); | ||
} | ||
} |